簡體   English   中英

如何根據 if 語句更新單元格?

[英]How do you update a cell based on an if statement?

我正在嘗試將單元格值(AA 中的單元格)更新為“已關閉 - 重復”,只要列 G = True(重復名稱)並取消描述(AA 列),如“已關閉”。 因此,基本上,如果重復名稱相同並且其中一行在 AA 列下具有“已關閉”值,則將該單元格更新為新值。 這是我到目前為止得到的,但由於某種原因,它給了我一個錯誤,我不知道為什么(參見圖片作為參考):

這行代碼出現錯誤: If d = True And d.Offset(0, -7).Value = " Closed " Then

並且錯誤顯示“運行時錯誤 1004 - 應用程序定義或 object 定義錯誤

在此處輸入圖像描述

Public Sub HighlightDuplicates()
Application.ScreenUpdating = False

Dim Mwb As Workbook
Dim ws As Worksheet
Dim rngVis As Range
Dim rngVis2 As Range
Dim c As Range
Dim d As Range
Dim Table As ListObject
Set Mwb = ThisWorkbook
Set ws = Mwb.Worksheets("Commission")
Set Table = ws.ListObjects("Comm_Table")
    
LR = ws.cells(ws.Rows.Count, 1).End(xlUp).Row

Table.ListColumns.Add 2
Table.HeaderRowRange(2) = "Duplicate ESIID"
ws.Range("B2:B" & LR).Value = "=SUMPRODUCT(--($A2=A:A))>1"
        
Set rngVis = ws.Range("B2:B" & LR).SpecialCells(xlCellTypeVisible)

    For Each c In rngVis.cells
        If c = True Then
        c.EntireRow.Columns("A").Interior.ColorIndex = 36
        End If
    Next c

Table.ListColumns(2).Delete

Table.ListColumns.Add 7
Table.HeaderRowRange(7) = "Duplicate Name"
ws.Range("G2:G" & LR).Value = "=SUMPRODUCT(--($F2=F:F))>1"

''This is where im having trouble:below''
Set rngVis2 = ws.Range("G2:G" & LR).SpecialCells(xlCellTypeVisible)
For Each d In rngVis2.cells
        If d = True And d.Offset(0, -7).Value = "*Closed*" Then
        d.EntireRow.Columns("AA").Value = "Closed - Duplicate"
        End If
    Next d**
    
Application.ScreenUpdating = True

End Sub

更新,而不是使用我的舊方法。 我決定用這個 for 循環來 go ,這似乎更容易閱讀,更容易理解,它做我所要求的。

    For i = 2 To Lr
     If ws.cells(i, "B").Value = "True" And ws.cells(i, "H").Value = "True" And 
      ws.cells(i, "AB").Value Like "*CLOSED*" Then
      ws.cells(i, "AB").Value = "CLOSED-DUPLICATE"
     End If
    Next i

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM