Page 1 of 1

การเขียน Code VBA หาตำแหน่งที่ Hilight

Posted: Fri Jun 10, 2016 1:52 pm
by Erk
รบกวนสอบถาม Function VBA ที่คืนค่าออกมาเป็นตำแหน่งแรกที่พบสี Hilight ตรงกับสีที่กำหนด
เช่น A1:E1 มี ้hilight เหลืองที่ C1 คำตอบคือ 3 ครับ

Function MatchByColor(CellColor As Range, SumRange As Range)
Application.Volatile
Dim ICol As Range
Dim TCell As Range
ICol = CellColor.Interior.ColorIndex
For Each TCell In SumRange
If ICol = TCell.Interior.ColorIndex Then
MatchByColor = ????
End If
Next TCell
End Function

Re: การเขียน Code VBA หาตำแหน่งที่ Hilight

Posted: Fri Jun 10, 2016 5:44 pm
by DhitiBank
ควรโพสต์โค้ดให้เป็นโค้ดจริงๆ จะดูง่ายครับ

ลองเปลี่ยนประเภทตัวแปร ICol และเพิ่มตัวแปรเพื่อเอาไว้นับอีกตัวครับ เช่น

Code: Select all

Dim ICol As Long, m as long
แล้วเอา l ไปใส่ในลูปครับ

Code: Select all

For Each ...
          m = m + 1
          if ICol = ...
               MatchByColor = m
               Exit For
               ...
แต่หลังจากใส่สูตรนี้ในชีทแล้ว เวลาเปลี่ยนสีเซลล์อ้างอิง (CellColor) สูตรมันไม่อัพเดทตาม ตรงนี้ผมทำไม่เป็นครับ T_T