:D snasui.com ยินดีต้อนรับ :D
ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย :thup: สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ :arrow: ระบุ Version ของ Excel
:!: โปรดทราบ :!:
  1. กรุณาอ่านกฎการใช้บอร์ด (Forum rules) ในตำแหน่งด้านบนของแต่ละบอร์ด
  2. การสมัครสมาชิก การ Login การกู้คืนรหัสผ่าน
    1. สมัครสมาชิกดูขั้นตอนได้ที่ :arrow: สมัครสมาชิก
    2. Login เข้าระบบโดยคลิกปุ่ม Login ตรงมุมขวาบนของหน้านี้ :roll:
    3. การ Login ผ่าน Facebook ดูวิธีที่ :arrow: Login ผ่าน Facebook
    4. ลืมรหัสผ่านสามารถรับรหัสใหม่ได้ที่ :arrow: Reset รหัสผ่าน
  3. มีปัญหาการใช้งาน แจ้งผู้ดูแลระบบได้ที่ :arrow: ติดต่อผู้ดูแลระบบ
  4. กำหนดการตั้งค่าส่วนตัว เช่นตั้งค่าภาษาเป็นไทยหรืออังกฤษดูได้ที่ :arrow: ตั้งค่าส่วนตัว
  5. การตั้งและตอบกระทู้ดูได้ที่ :arrow: วิธีการตั้งและตอบกระทู้
  6. การจัดรูปแบบตัวอักษรด้วย bbcode ในช่องแสดงความคิดเห็นดูได้ที่ :arrow: จัดรูปแบบตัวอักษร
  7. กำหนดขนาดตัวอักษรใน Browser ดูได้ที่ :arrow: กำหนดขนาดตัวอักษรใน Browser

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

ฟอรัมถาม-ตอบปัญหาการใช้งาน Macro และ VBA
Forum rules
  1. ไม่อนุญาตให้ใช้ภาษาแชทในการถามและตอบปัญหา ไม่ใช้คำว่า "คับ" หรือ "อ่ะครับ" แทนคำว่า "ครับ" ไม่ใช้คำว่า "เด๋ว" แทนคำว่า "เดี๋ยว" เป็นต้น เนื่องจากเมื่อแปลเป็นภาษาต่างประเทศแล้วจะให้ความหมายผิดไปจากที่ควรจะเป็น
  2. ห้ามถามโดยระบุชื่อผู้ตอบและต้องตั้งชื่อกระทู้ให้สื่อถึงปัญหาที่จะถาม ไม่ตั้งชื่อว่า ช่วยด้วยครับ, มีปัญหามาปรึกษาครับ เป็นต้น
  3. กรุณาอธิบายปัญหาและระบุคำตอบที่ต้องการมาในกระทู้ด้วยเสมอถึงแม้จะอธิบายไว้ในไฟล์แนบแล้วก็ตาม ทั้งนี้เพื่ออำนวยความสะดวกแก่เพื่อนสมาชิกในการค้นหาข้อมูล
  4. กรุณาแนบไฟล์ตัวอย่างพร้อมแสดงคำตอบที่ถูกต้องมาในไฟล์ด้วยเพื่อให้ง่ายต่อการทำความเข้าใจและสะดวกต่อการตอบคำถาม (ขนาดไฟล์ไม่เกิน 500Kb ขนาดภาพไม่เกิน 800*600 Pixel) ไม่แนบเป็น Link มาจากแหล่งอื่นที่อาจจะถูกลบทิ้งไปโดยต้นทางในภายหลัง นอกจากนี้ไม่ควรแนบไฟล์ที่มีข้อมูลสำคัญอันก่อให้เกิดความเสียหายกับตนเองและผู้อื่น
  5. กรณีเป็นคำถามเกี่ยวกับ Programming เช่น VBA, VB.Net, C#, SQL ฯลฯ ต้องลองเขียนมาเองก่อนเสมอ ถามเฉพาะที่ติดปัญหา ระบุ Module, Procedure ที่ติดปัญหาให้ชัดเจน กรุณาโพสต์ Code ให้แสดงเป็น Code คือเปิดด้วย [code] และปิดด้วย [/code] ตัวอย่างเช่น [code]dim r as range[/code] เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)
  6. กรุณาแจ้งผลการใช้งานเมื่อได้รับคำตอบว่าตรงกับความต้องการหรือไม่
yangkodza
Bronze
Bronze
Posts: 372
Joined: Tue Feb 10, 2015 10:37 am
Excel Ver: 2021

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

#1

Post 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 อย่างไร
User avatar
puriwutpokin
Guru
Guru
Posts: 3801
Joined: Fri Jan 04, 2013 9:49 pm
Location: Bangkok
Excel Ver: MS.365

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

#2

Post by puriwutpokin »

ต้องเขียนคำสั่งให้ลูป ค่า Condition format ไปทุกชีท ครับ ถ้าเป็น Array ไม่สามารถ ทำได้ครับ
ลองแนนไฟล์ตัวอย่างที่ ต้องการ ทำมาดูพร้อมโคัดที่ต้องการ Condition format สำหรับ 1 ชีทมาดูครับ :)
:shock: :roll: :D
yangkodza
Bronze
Bronze
Posts: 372
Joined: Tue Feb 10, 2015 10:37 am
Excel Ver: 2021

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

#3

Post 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
รบกวนชี้แนะด้วยครับ
User avatar
snasui
Site Admin
Site Admin
Posts: 31205
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

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

#4

Post 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
yangkodza
Bronze
Bronze
Posts: 372
Joined: Tue Feb 10, 2015 10:37 am
Excel Ver: 2021

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

#5

Post 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:
Post Reply