如果列A中的单元格为空,则在不同的表单中查找列B与数据集

我试图构build一个简单的macros,它将识别列A中的所有空白单元格,然后使用列B自动运行一个vlookup,以对照我在另一个工作表中创build的数据集。

我试图在两本书和logging之间进行和解。 但是,2个文件中的唯一标识符匹配,或者单元格为空,因此我使用“description”创build了一个新表,以填充唯一的标识符,以帮助匹配2条logging。

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 

创build标识符

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

例如,两个文件都有KDN控件,但是RecordsA和RecordsB中的唯一标识符都是空白的。 而且,两者的描述都不一样。 我创build了一个新工作表,使用2个不同的描述来创build一个标识符。 在黄色我强调了公式,我希望有一个macrosautopopulate当macros识别列A是空白的。

我想进入空白单元格的公式,查看描述对我创build的唯一标识符数据集

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

不知道这是否可能..很想听到反馈。

这应该工作。 我还包括了很多意见,以更好地解释代码在做什么…

 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 

If IsEmpty(Cells(i,1)) Then IsEmpty()函数有一组括号,而Cells()函数有一组括号,则Cells()embedded在IsEmpty()

会是这样的事情:

 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 

像我的testing数据集的魅力工作。