我正在尝试构建一个简单的宏,它将识别A列中的任何空白单元格,然后根据我在另一个工作表中创建的数据集,使用B列自动运行vlookup。
我正试着在两本书和两本唱片之间做一次核对。但是,两个文件中的唯一标识符要么匹配,要么单元格为空,因此我创建了一个新的表,使用"description“填充一个唯一标识符,该标识符将有助于匹配这2条记录。
Records A唯一标识符_
--------------------+-------------------+
ADFVBC -阿尔法创业公司
KDN持有量
资本DQDW1 ORD 3214
记录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,但是RecordsA & RecordsB中的唯一标识符是空的。此外,这两种描述是不同的。我创建了一个新的工作表来使用这两个不同的描述创建一个标识符。在黄色中,我突出显示了我希望在宏识别A列为空白时有一个宏自动操作的等式。
我想在空白单元格中输入一个公式,vlookup对我创建的唯一标识符数据集的描述
=VLOOKUP(B10,Identifiers!A:C,3,FALSE)不确定这是否可能..。很想听听反馈。
发布于 2015-03-10 17:13:01
这应该能行。我还包括了许多评论,以更好地解释代码正在做什么..。
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发布于 2015-03-10 16:45:48
If条件缺少一个结束括号:If IsEmpty(Cells(i,1)) Then,IsEmpty()函数有一组括号,Cells()函数有一组括号,Cells()嵌入到IsEmpty()中。
发布于 2015-03-10 16:03:37
会是这样的:
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对我的测试数据集来说很有魅力。
https://stackoverflow.com/questions/28966860
复制相似问题