รหัสของฉันอยู่ใน VBA และอัปเดตค่าของเซลล์ตามเกณฑ์สองข้อ ฉันแสดงความคิดเห็นเกี่ยวกับโค้ดของฉันค่อนข้างมาก ดังนั้นฉันจะวางมันไว้ด้านล่างก่อน ฉันได้เปลี่ยนความคิดเห็นโดยเพิ่มเครื่องหมายอะพอสทรอฟี่อีกสองตัวเพื่อให้บอกความแตกต่างระหว่างความคิดเห็นและโค้ดบนแพลตฟอร์มนี้ได้ง่ายขึ้นเล็กน้อย
Sub HighlightValues()
'''Shortcut key: ctrl + w
'''Highlights values of corresponding left-most cell, if two conditions are met:
'''The part is in "L" class and all of the rightmost cells are empty
Dim ws As Worksheet
Dim i As Long, lastrow As Long, lastcolumn As Long, c As Long, d As Long, j As Long, count As Long, k As Long, report As Long
Set ws = Sheets("QAP")
lastrow = ws.Cells(Rows.count, 1).End(xlUp).Row
lastcolumn = ws.Cells(1, ws.Columns.count).End(xlToLeft).Column
'''Searches column titles for "Analysis Task Count"
'''where it will start searching for filled boxes on each row
For i = 1 To lastrow
If InStr(ws.Cells(1, i), "Analysis Task Count") Then
'''c is the number of column that Analysis Task Count is in
c = i - 1
End If
Next
'''Searches column titles for "Required by RPC"
'''where it will search for whether the part is "L' or not
For d = 1 To lastcolumn
If InStr(ws.Cells(1, d), "Required by RPC") Then
'''k is the number of column that Required by RPC is in
k = d
End If
Next d
'''For each row that part data is in
For i = 11 To lastrow
count = 0
'''If any cells past Analysis Task Count are filled, remember that
For j = c To lastcolumn
If Not IsEmpty(ws.Cells(i, j)) Then
count = 1
End If
Next j
'''If the stage is L and all the cells are empty for that row
If Cells(i, k).Value = ("L") And count = 0 Then
'''Highlight the first box in green
Cells(i, 1).Interior.Color = RGB(102, 255, 102)
Else
'''Or if thats not true, then make that box clear
Cells(i, 1).Interior.Color = xlNone
report = report + 1
End If
Next i
MsgBox (report)
End Sub
หากต้องการแนะนำคุณเกี่ยวกับโค้ด (ยกเว้นการเว้นวรรค การจัดรูปแบบบางอย่างไม่ได้ใช้) โค้ดจะค้นหาหมายเลขคอลัมน์ของคอลัมน์ที่สำคัญสองคอลัมน์ก่อน จากนั้นในลูปสุดท้ายสำหรับแต่ละแถว จะค้นหาว่ามี 2 คอลัมน์หรือไม่ เป็นไปตามเกณฑ์ หากตรงตามเกณฑ์ เซลล์จะเปลี่ยนเป็นสีเขียว หากไม่ตรง เซลล์จะถูกเติมในช่องว่าง ฉันใช้เวลาประมาณหนึ่งชั่วโมงในการยืนยันว่าค่าที่ส่งคืนโดย k และ c เป็นทั้งหมายเลขคอลัมน์ที่ถูกต้อง: ฉันไม่คิดว่านั่นคือปัญหา นอกจากนี้ เมื่อฉันตั้งค่าการนับเพื่อดูว่าโค้ดผ่านไปกี่ครั้งใน for loop สุดท้าย และแต่ละคำสั่ง if ฉันจะได้รับจำนวนรอบที่ถูกต้อง
สิ่งนี้น่าหงุดหงิดมาก สิ่งเดียวที่แปลกคือโค้ดดำเนินการเกือบจะในทันที ดังนั้นจึงไม่มีความล่าช้าเลย และแน่นอนว่าการเติมเซลล์จะไม่เปลี่ยนแปลง เพื่อทดสอบสิ่งนี้ ฉันรันโค้ดที่เติมลงในเซลล์ด้วยสีส้ม และไม่มีการเปลี่ยนแปลงในเซลล์ใดๆ
ขอบคุณสำหรับความช่วยเหลือใด ๆ ที่ฉันสามารถทำได้!