การอัปเดต Excel VBA เติมเซลล์ตามเกณฑ์

รหัสของฉันอยู่ใน 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 ฉันจะได้รับจำนวนรอบที่ถูกต้อง

สิ่งนี้น่าหงุดหงิดมาก สิ่งเดียวที่แปลกคือโค้ดดำเนินการเกือบจะในทันที ดังนั้นจึงไม่มีความล่าช้าเลย และแน่นอนว่าการเติมเซลล์จะไม่เปลี่ยนแปลง เพื่อทดสอบสิ่งนี้ ฉันรันโค้ดที่เติมลงในเซลล์ด้วยสีส้ม และไม่มีการเปลี่ยนแปลงในเซลล์ใดๆ

ขอบคุณสำหรับความช่วยเหลือใด ๆ ที่ฉันสามารถทำได้!


person Gregory Haegele    schedule 14.07.2015    source แหล่งที่มา
comment
ไม่รู้ว่าผ่านอะไรมา? ฉันใช้ Dim เพื่อเริ่มต้นทั้งคู่ จากนั้นฉันก็หาค่าของมัน จากนั้นฉันก็ใช้ค่าในลูปสุดท้าย   -  person Gregory Haegele    schedule 14.07.2015
comment
เหตุใดคุณจึงตั้งค่า c เป็นคอลัมน์ปัจจุบัน - 1 c = i - 1 จากนั้นจะดูผ่านคอลัมน์ต่างๆ โดยเริ่มต้นที่ c โดยไม่ทราบข้อมูลของคุณ การทดสอบของฉันไม่มีอะไรใน c ดังนั้นจึงจะได้รับการนับ = 1 เสมอ และไม่เคยเข้าไปในคำสั่ง if เพื่อระบายสีเซลล์   -  person MatthewD    schedule 14.07.2015
comment
FYI คุณสามารถรับแถวและคอลัมน์สุดท้ายเช่นนี้ สำหรับ i = 1 ถึง ws.UsedRange.Rows.count หรือ สำหรับ i = 1 ถึง ws.UsedRange.Columns.count   -  person MatthewD    schedule 14.07.2015


คำตอบ (2)


ส่วนที่รับผิดชอบในการค้นหาแผ่นงานการนับจำนวนงานการวิเคราะห์ไม่ถูกต้องสำหรับฉัน

For i = 1 To lastrow         '<---- why [lastrow]? should be [lastcolumn]
    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            '<---- why subtracting 1?
    End If
Next

หากคุณกำลังค้นหาชื่อคอลัมน์ทำไมคุณถึงใช้ตัวนับ [lastrow] เป็นขอบเขตบนสำหรับการวนซ้ำของคุณ นอกจากนี้ฉันไม่เข้าใจว่าทำไมคุณถึงลบ 1 หลังจากที่คุณพบคอลัมน์ที่มีส่วนหัวดังกล่าว

ด้วยเหตุนี้ คุณมีดัชนีคอลัมน์ที่ไม่ถูกต้อง และคุณเปรียบเทียบชุดข้อมูลที่ไม่ถูกต้อง

person mielk    schedule 14.07.2015

ที่ 1 Change: ws.Cells(Rows.count, 1) To: ws.Cells(ws.Rows.count, 1) สิ่งนี้ไม่มีผลกระทบ แต่ "ถูกต้องมากกว่า"

  1. อืม

    For i = 1 To lastrow
        If InStr(ws.Cells(1, i), "Analysis Task Count") Then
            c = i - 1
    

เปลี่ยน Lastrow เป็นคอลัมน์สุดท้าย เปลี่ยน c = i - 1 เป็น c = i

  1. เหตุใดจึงเริ่มต้นที่ 11 แต่ลูปอื่นๆ เริ่มต้นที่ 1

        For i = 11 To lastrow
    
  2. โปรดใช้ชื่อตัวแปรแบบเต็มที่มีความหมายมากกว่านี้ - มันอ่านยากเช่น

    "iCol" for a column counter
    "iRow" for a row counter
    instead of "k" use "ColumnNumWithReqByRPVC"
    etc..
    
  3. คุณใช้ instr อย่างถูกต้องหรือไม่? ดูที่นี่

    InStr([start, ]string1, string2[, compare])
    
  4. เปลี่ยน

    Cells(i, 1).Interior.Color 
    To
    ws.Cells(i, 1).Interior.Color 
    

การเปลี่ยนแปลงข้างต้นอาจทำให้ใช้งานได้ใช่ไหม แจ้งให้เราทราบ

person HarveyFrench    schedule 14.07.2015