Page 1 of 1

VBA กำหนดการจัดรูปแบบตามเงื่อนไขหลาย sheet

Posted: Mon Apr 12, 2021 1:26 pm
by yangkodza

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 อย่างไร

Re: VBA กำหนดการจัดรูปแบบตามเงื่อนไขหลาย sheet

Posted: Mon Apr 12, 2021 2:50 pm
by puriwutpokin
ต้องเขียนคำสั่งให้ลูป ค่า Condition format ไปทุกชีท ครับ ถ้าเป็น Array ไม่สามารถ ทำได้ครับ
ลองแนนไฟล์ตัวอย่างที่ ต้องการ ทำมาดูพร้อมโคัดที่ต้องการ Condition format สำหรับ 1 ชีทมาดูครับ :)

Re: VBA กำหนดการจัดรูปแบบตามเงื่อนไขหลาย sheet

Posted: Mon Apr 12, 2021 5:38 pm
by yangkodza

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
รบกวนชี้แนะด้วยครับ

Re: VBA กำหนดการจัดรูปแบบตามเงื่อนไขหลาย sheet

Posted: Mon Apr 12, 2021 8:27 pm
by snasui
:D ตัวอย่างการปรับ 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

Re: VBA กำหนดการจัดรูปแบบตามเงื่อนไขหลาย sheet

Posted: Mon Apr 12, 2021 8:42 pm
by yangkodza
snasui wrote: Mon Apr 12, 2021 8:27 pm :D ตัวอย่างการปรับ 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 จุด :thup: