snasui.com ยินดีต้อนรับ
ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ ระบุ Version ของ Excel
ฟอรัมถาม-ตอบปัญหาการใช้งาน Macro และ VBA
Forum rules
ไม่อนุญาตให้ใช้ภาษาแชทในการถามและตอบปัญหา ไม่ใช้คำว่า "คับ" หรือ "อ่ะครับ" แทนคำว่า "ครับ" ไม่ใช้คำว่า "เด๋ว" แทนคำว่า "เดี๋ยว" เป็นต้น เนื่องจากเมื่อแปลเป็นภาษาต่างประเทศแล้วจะให้ความหมายผิดไปจากที่ควรจะเป็น
ห้ามถามโดยระบุชื่อผู้ตอบและต้องตั้งชื่อกระทู้ให้สื่อถึงปัญหาที่จะถาม ไม่ตั้งชื่อว่า ช่วยด้วยครับ, มีปัญหามาปรึกษาครับ เป็นต้น
กรุณาอธิบายปัญหาและระบุคำตอบที่ต้องการมาในกระทู้ด้วยเสมอถึงแม้จะอธิบายไว้ในไฟล์แนบแล้วก็ตาม ทั้งนี้เพื่ออำนวยความสะดวกแก่เพื่อนสมาชิกในการค้นหาข้อมูล
กรุณาแนบไฟล์ตัวอย่างพร้อมแสดงคำตอบที่ถูกต้องมาในไฟล์ด้วยเพื่อให้ง่ายต่อการทำความเข้าใจและสะดวกต่อการตอบคำถาม (ขนาดไฟล์ไม่เกิน 500Kb ขนาดภาพไม่เกิน 800*600 Pixel) ไม่แนบเป็น Link มาจากแหล่งอื่นที่อาจจะถูกลบทิ้งไปโดยต้นทางในภายหลัง นอกจากนี้ไม่ควรแนบไฟล์ที่มีข้อมูลสำคัญอันก่อให้เกิดความเสียหายกับตนเองและผู้อื่น
กรณีเป็นคำถามเกี่ยวกับ Programming เช่น VBA, VB.Net, C#, SQL ฯลฯ ต้องลองเขียนมาเองก่อนเสมอ ถามเฉพาะที่ติดปัญหา ระบุ Module, Procedure ที่ติดปัญหาให้ชัดเจน กรุณาโพสต์ Code ให้แสดงเป็น Code คือเปิดด้วย [code] และปิดด้วย [/code] ตัวอย่างเช่น [code]dim r as range[/code] เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)
กรุณาแจ้งผลการใช้งานเมื่อได้รับคำตอบว่าตรงกับความต้องการหรือไม่
yangkodza
Bronze
Posts: 372 Joined: Tue Feb 10, 2015 10:37 am
Excel Ver: 2021
#1
Post
by yangkodza » Mon Apr 12, 2021 1:26 pm
Code: Select all
Sub ปรับรูปแบบตาราง()
Application.ScreenUpdating = False
Call Module5.UnProtectAll2
Sheets(Array("ไทย1", "คณิต1", "วิทย์1", "สังคม1", "ประวัติ1", "หน้าที่1", "ไทย2", "คณิต2", "วิทย์2", "สังคม2", "ประวัติ2", "หน้าที่2")).Select
Sheets("ไทย1").Activate
'********************* ตีตาราง ปรับฟอนต์ จัดกึ่งกลาง
Range("E7:X57, AB8:AC57, AJ8:AK57").Select
Selection.FormatConditions.Delete
Selection.Font.Name = "AngsanaUPC"
Selection.Font.Size = 14
Selection.Font.Bold = False
With Selection
.HorizontalAlignment = xlCenter
Selection.Borders(xlEdgeLeft).LineStyle = xlContinuous
Selection.Borders(xlEdgeTop).LineStyle = xlContinuous
Selection.Borders(xlEdgeBottom).LineStyle = xlContinuous
Selection.Borders(xlEdgeRight).LineStyle = xlContinuous
Selection.Borders(xlInsideVertical).LineStyle = xlContinuous
Selection.Borders(xlInsideHorizontal).LineStyle = xlContinuous
'********************* ใส่เช็ค คะแนนต่ำกว่าครึ่งแดง และคะแนนเกินพื้นแดง
Application.Goto Reference:=Range("E8:E57").Resize(, 33)
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLessEqual, _
Formula1:="=E$7/2"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16776961
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = True
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:="=E$7"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16711681
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = True
Application.ScreenUpdating = True
End With
Sheets("หน้าแรก").Select
Range("E57").Select
Range("E11").Select
End Sub
จาก Code ดังกล่าว ใช้ Sheets Array จะมาสามารถทำรูปแบบตารางได้ แต่พอมาถึงการจัดการตามเงื่อนไขไม่สามารถทำได้ครับ รบกวนชี้แนะทีครับ ว่าต้องปรับ code อย่างไร
puriwutpokin
Guru
Posts: 3801 Joined: Fri Jan 04, 2013 9:49 pm
Location: Bangkok
Excel Ver: MS.365
#2
Post
by puriwutpokin » Mon Apr 12, 2021 2:50 pm
ต้องเขียนคำสั่งให้ลูป ค่า Condition format ไปทุกชีท ครับ ถ้าเป็น Array ไม่สามารถ ทำได้ครับ
ลองแนนไฟล์ตัวอย่างที่ ต้องการ ทำมาดูพร้อมโคัดที่ต้องการ Condition format สำหรับ 1 ชีทมาดูครับ
yangkodza
Bronze
Posts: 372 Joined: Tue Feb 10, 2015 10:37 am
Excel Ver: 2021
#3
Post
by yangkodza » Mon Apr 12, 2021 5:38 pm
Code: Select all
Sub ปรับตาราง()
Application.ScreenUpdating = False
Sheets(Array("2", "3", "4", "5", "6", "8", "9", "10", "11", "12")).Select
Sheets("2").Activate
'********************* ??????? ????????? ???????????
Range("E7:X57, AB8:AC57, AJ8:AK57").Select
Selection.FormatConditions.Delete
Selection.Font.Name = "AngsanaUPC"
Selection.Font.Size = 14
Selection.Font.Bold = False
With Selection
.HorizontalAlignment = xlCenter
Selection.Borders(xlEdgeLeft).LineStyle = xlContinuous
Selection.Borders(xlEdgeTop).LineStyle = xlContinuous
Selection.Borders(xlEdgeBottom).LineStyle = xlContinuous
Selection.Borders(xlEdgeRight).LineStyle = xlContinuous
Selection.Borders(xlInsideVertical).LineStyle = xlContinuous
Selection.Borders(xlInsideHorizontal).LineStyle = xlContinuous
'********************* ??????? ???????????????????? ???????????????????
Application.Goto Reference:=Range("E8:E57").Resize(, 33)
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLessEqual, _
Formula1:="=E$7/2"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16776961
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = True
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:="=E$7"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16711681
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = True
Application.ScreenUpdating = True
End With
Sheets("2").Select
Range("E7").Select
End Sub
ต้องการกำหนดรูปแบบตามเงื่อนไขที่ Sheet 2-6 และ Sheet 8-12
รบกวนชี้แนะด้วยครับ
snasui
Site Admin
Posts: 31205 Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:
#4
Post
by snasui » Mon Apr 12, 2021 8:27 pm
ตัวอย่างการปรับ Code ครับ
Code: Select all
'Other code
'********************* ??????? ???????????????????? ???????????????????
Sheets(1).Select
For Each sh In Sheets(Array("2", "3", "4", "5", "6", "8", "9", "10", "11", "12"))
Application.Goto Reference:=sh.Range("E8:E57").Resize(, 33)
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLessEqual, _
Formula1:="=E$7/2"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16776961
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = True
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:="=E$7"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16711681
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = True
Next sh
Application.ScreenUpdating = True
'Other code
yangkodza
Bronze
Posts: 372 Joined: Tue Feb 10, 2015 10:37 am
Excel Ver: 2021
#5
Post
by yangkodza » Mon Apr 12, 2021 8:42 pm
snasui wrote: Mon Apr 12, 2021 8:27 pm
ตัวอย่างการปรับ Code ครับ
Code: Select all
'Other code
'********************* ??????? ???????????????????? ???????????????????
Sheets(1).Select
For Each sh In Sheets(Array("2", "3", "4", "5", "6", "8", "9", "10", "11", "12"))
Application.Goto Reference:=sh.Range("E8:E57").Resize(, 33)
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLessEqual, _
Formula1:="=E$7/2"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16776961
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = True
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:="=E$7"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16711681
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = True
Next sh
Application.ScreenUpdating = True
'Other code
ขอบคุณมากครับ สำเร็จรุล่วงไปอีก 1 จุด