簡體   English   中英

如果A列中的單元格為空,則針對不同工作表中的數據集進行vlookup列B

[英]if cell in column A is blank then vlookup column B against dataset in different sheet

我正在嘗試構建一個簡單的宏,該宏將識別A列中的任何空白單元格,然后使用B列針對我在另一張工作表中創建的數據集自動運行vlookup。

我正在嘗試對2本書和記錄進行對帳。 但是,這2個文件中的唯一標識符匹配,或者該單元格為空白,因此我使用“描述”創建了一個新表,以填充有助於匹配2個記錄的唯一標識符。

Records A
`````````
Unique Identifier   |   Description     |   Units
--------------------+-------------------+---------
ADFVBC              |   Alpha Ventures  |   1234
<blank>             |   KDN holdings    |   2155
DQDW1               |   Capital ORD     |   3214

Records B
`````````
Unique Identifier   |   Description         |   Units
--------------------+-----------------------+---------
ADFVBC              |   Alpha Ventures      |   1234
<blank>             |   **KDN holdings INC  |   2155
DQDW1               |   Capital ORD         |   3214

創建的標識符

Records A description   |   Records B description   |   Created Identifiers
------------------------+---------------------------+-----------------------
KDN Holdings            |   **KDN Holdings Inc      |   IDENTIFIER1

例如,兩個文件都具有KDN Holdings,但是RecordsA和RecordsB中的唯一標識符為空。 此外,兩個描述是不同的。 我使用2種不同的描述創建了一個新表來創建標識符。 我用黃色突出顯示了該方程式,希望每當宏識別出A列為空白時,宏就會自動填充。

我想在空白單元格中輸入一個公式,對我創建的唯一標識符數據集進行查找

=VLOOKUP(B10,Identifiers!A:C,3,FALSE)

不知道這是否可能..很想聽聽反饋。

If條件缺少If IsEmpty(Cells(i,1)) Then括號: If IsEmpty(Cells(i,1)) Then IsEmpty()函數具有一組括號,而Cells()函數具有一組括號Cells()嵌入在IsEmpty()

這應該工作。 我還添加了很多注釋,以更好地解釋代碼的作用。

Sub test()

Dim i As Integer, j As Integer, k As Integer ' first create variables you might need (I always include a few integers)
Dim aRec As Worksheet, bRec As Worksheet, ident As Worksheet, wb As Workbook ' first create variables you might need

Dim aDesc As String, bDesc As String ' first create variables you might need

Set wb = Excel.ActiveWorkbook ' This assumes your currently active workbook is the one we need to use.
Set aRec = wb.Sheets("Records A") ' Assuming your worksheet names are "Records A", "Records B", and "Identifiers"
Set bRec = wb.Sheets("Records B") ' Assuming your worksheet names are "Records A", "Records B", and "Identifiers"
Set ident = wb.Sheets("Identifiers") ' Assuming your worksheet names are "Records A", "Records B", and "Identifiers"

With aRec ' Using the "Records A" worksheet . . .
    For i = 1 To .UsedRange.Rows.Count ' Loop through each row in the used range...
        If Trim(.Cells(i, 1).Value) = "" Then ' Check if the cell value is blank. I added the "trim" function to eliminate leading or trailing spaces.
            .Cells(i, 1).Value = "=VLOOKUP(" & .Cells(i, 2).Address(0, 0, xlA1) & ", Identifiers!A:C,3,FALSE)"
        End If
    Next i
End With

With bRec ' Using the "Records B" worksheet . . .
    For i = 1 To .UsedRange.Rows.Count ' Loop through each row in the used range...
        If Trim(.Cells(i, 1).Value) = "" Then ' Check if the cell value is blank. I added the "trim" function to eliminate leading or trailing spaces.
            .Cells(i, 1).Value = "=VLOOKUP(" & .Cells(i, 2).Address(0, 0, xlA1) & ", Identifiers!B:C,2,FALSE)" ' <-- notice how I offset this one a little to account for the new description location on the identifiers sheet
        End If
    Next i
End With



End Sub

將是這樣的:

For i = 1 To LastRow
    If IsEmpty(Cells(i,1) Then 'if cell in A is empty
        Cells(i,1).FormulaR1C1 = "=VLOOKUP(RC[+1]Identifiers!C1:C3,3,FALSE) 'Lookup cell in B in Identifier A:C
    End If
Next

更新:

Sub ertdfgcvb()
LastRow = Cells.Find(What:="*", After:=Cells(1, 1), Lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
MsgBox LastRow
For i = 1 To LastRow
    If IsEmpty(Cells(i, 1)) Then 'if cell in A is empty
        Cells(i, 1).FormulaR1C1 = "=VLOOKUP(RC[1],Identifiers!C1:C3,3,FALSE)" 'Lookup cell in B in Identifier A:C
    End If
Next
End Sub

對於我的測試數據集,它就像一個魅力。

暫無
暫無

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

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