簡體   English   中英

使用activecell值與activecell公式值VBA的結果進行比較

[英]Using activecell value to compare with the result of activecell formula value VBA

我試圖弄清楚如何查看活動單元格的值(例如單元格 F3),在同一單元格(F3)內運行 vlookup 並查看該 vlookup 的值是否與活動單元格的值匹配(F3)。

這是我正在使用的 excel 表的示例:

在此處輸入圖像描述

這是我的代碼,我已經寫了我希望它如何工作,但顯然這不是正確的方式(希望它會是這樣的,但我就是想不通:):

Sub Grade_comment_Updated2()

Application.ScreenUpdating = False

lastcolumn = Cells(2, Columns.Count).End(xlToLeft).Column 
'Fills to last column. Last column is defined by last column of row 2 that has data in it'

ActiveSheet.Cells(3, 6).Select 
Do Until ActiveCell.Column = lastcolumn + 1  
    If ActiveCell.Value <> ActiveCell.FormulaR1C1 = 
    "=VLOOKUP(R[-3]C,C1:C4,4,0)".Value Then
        ActiveCell.ClearNotes
            ActiveCell.AddCommentThreaded ("Was " & ActiveCell.Value)
                Activecell.value = ActiveCell.FormulaR1C1 = 
                "=VLOOKUP(R[-3]C,C1:C4,4,0)".Value
                    ActiveCell.Offset(0, 1).Select
    ElseIf ActiveCell.Value = ActiveCell.FormulaR1C1 = 
    "=IFNA(VLOOKUP(R[-3]C,C1:C4,4,0),R[-1]C)".Value Then
        ActiveCell.Offset(0, 1).Select
End If
Loop
'Looks in to cell F3 (This currently has a letter assigned to it), then runs a vlookup in the same cell (F3) to see if the letter matches the same letter that the vlookup is checking, if the letter is different then replace it with the different letter and put a comment saying what the previous letter was, then go to next cell to the right and repeat'

Application.ScreenUpdating = True

MsgBox "Grade Update Complete"
    
End Sub

找到我需要的代碼:

通過 vba 在單元格內運行 vlookup 並與當前單元格字符進行比較所需的是“Application.WorksheetFunction.VLookup”。

'On error resume next' 用於拾取范圍內不存在查找值的任何 vlookup 錯誤(IFERROR/IFNA 不能通過 application.worksheetfunction.vlookup 方法使用;因此它是錯誤處理的另一種形式本例):

Sub Grade_comment_Updated2()

Dim x As String
 
Range("F3").Select

Do
 
 On Error Resume Next
 
 If ActiveCell.Value = "" Then
                ActiveCell = Application.WorksheetFunction.VLookup(ActiveCell.Offset(-2, 0).Value, ActiveSheet.Range("A:D"), 4, False)
                     ActiveCell.Offset(0, 1).Activate
    Else
    If x = Application.WorksheetFunction.VLookup(ActiveCell.Offset(-2, 0).Value, ActiveSheet.Range("A:D"), 4, False) = ActiveCell Then
        ActiveCell.Offset(0, 1).Activate
    Else
    If x = Application.WorksheetFunction.VLookup(ActiveCell.Offset(-2, 0).Value, ActiveSheet.Range("A:D"), 4, False) <> ActiveCell Then
        ActiveCell.ClearNotes
            ActiveCell.AddCommentThreaded ("Was " & ActiveCell.Value)
                ActiveCell = Application.WorksheetFunction.VLookup(ActiveCell.Offset(-2, 0).Value, ActiveSheet.Range("A:D"), 4, False)
                    ActiveCell.Offset(0, 1).Activate
    
 End If
 End If
 End If
 
 Loop Until IsEmpty(ActiveCell.Offset(-2, 0).Value)
 
 End Sub

在此處輸入圖像描述

暫無
暫無

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

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