我正在尝试构建一个简单的宏来识别A列中的任何空白单元格,然后使用B列自动运行vlookup来对照我在另一个工作表中创建的数据集。
我试图在两本书和记录之间进行对帐。但是,2个文件中的唯一标识符匹配或单元格为空,因此我使用" description"创建了一个新表。填充有助于匹配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和amp;中都有唯一标识符。 RecordsB是空白的。此外,两种描述都不同。我使用2种不同的描述创建了一个新工作表来创建标识符。在黄色中,我强调了这个等式,我希望只要宏识别出A列是空白的,就会有一个宏自动填充。
我想在空白单元格中输入一个公式,根据我创建的唯一标识符数据集查找描述
=VLOOKUP(B10,Identifiers!A:C,3,FALSE)
不确定这是否可行..很想听到反馈。
答案 0 :(得分:2)
If
条件缺少结束括号:
If IsEmpty(Cells(i,1)) Then
IsEmpty()
函数有一组括号和
Cells()
函数有一组括号
Cells()
IsEmpty()
答案 1 :(得分:1)
这应该有效。我还包含了很多评论,以便更好地解释代码正在做什么......
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
答案 2 :(得分:0)
会是这样的:
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
就像我的测试数据集的魅力一样。