เรียน ทุกท่าน
ดิฉันได้นำ code vba เกี่ยวกับการเปลี่ยน formatting จาก web แห่งหนึ่งมาประยุกต์ใช้เพื่อเปลี่ยนสีตัวอักษร โดยมีเงื่อนไขว่า
ถ้าข้อมูลของ cell ตั้งแต่ B2:B3133 มีคำที่ตรงกันกับ cell A1 ให้เปลียนข้อมูลของ cell ตั้งแต่ B2:B3133 เป็นสีแดง
ถ้าข้อมูลของ cell ตั้งแต่ B2:B3133 มีคำที่ตรงกันกับ cell A2 ให้เปลียนข้อมูลของ cell ตั้งแต่ B2:B3133 เป็นสีเขียว
Code: Select all
Dim strString1, x&
For i = 0 To 3133
strString1 = Range("A1")
With Range("B2").Offset(0, i)
.Font.ColorIndex = 1
For x = 1 To Len(.Text) Step 1
If Mid(.Text, x, Len(strString1)) = strString1 Then .Characters(x, Len(strString1)).Font.ColorIndex = 3
Next x
End With
Next i
Dim strString2, y&
For t = 0 To 3133
strString2 = Range("A2")
With Range("B2").Offset(0, t)
.Font.ColorIndex = 1
For y = 1 To Len(.Text) Step 1
If Mid(.Text, y, Len(strString2)) = strString2 Then .Characters(y, Len(strString2)).Font.ColorIndex = 4
Next y
End With
Next t
ตอนแรกดิฉันลองแยก code ออกเป็น 2 module แล้วลอง run ทีละ module แต่พอ run module ที่1 ก็จะได้เฉพาะสีแดง " Font.ColorIndex = 3" ,run module ที่2 ก็จะได้เฉพาะสีเขียว " Font.ColorIndex = 4" แต่พอเอา code มารวมกันแล้วลอง run ดูก็จะได้ เฉพาะสีเขียวเท่านั้น
ดิฉันลองเอาทั้ง 2 module มารวมกันแล้วใช้สุตร if....then ก็เกิด error (code นี้ทดลองเขียนมา 5 ชั่วโมงแล้วคะ)
ดิฉันจึงขอรบกวนท่านผู้รู้แนะนำด้วยคะ ขอขอบคุณล่วงหน้าคะ