首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果A列中的单元格为空,则vlookup列B针对不同工作表中的数据集

如果A列中的单元格为空,则vlookup列B针对不同工作表中的数据集
EN

Stack Overflow用户
提问于 2015-03-10 14:50:23
回答 3查看 1.8K关注 0票数 0

我正在尝试构建一个简单的宏,它将识别A列中的任何空白单元格,然后根据我在另一个工作表中创建的数据集,使用B列自动运行vlookup。

我正试着在两本书和两本唱片之间做一次核对。但是,两个文件中的唯一标识符要么匹配,要么单元格为空,因此我创建了一个新的表,使用"description“填充一个唯一标识符,该标识符将有助于匹配这2条记录。

代码语言:javascript
运行
复制
Records A

唯一标识符_

--------------------+-------------------+

ADFVBC -阿尔法创业公司

KDN持有量

资本DQDW1 ORD 3214

记录B

代码语言:javascript
运行
复制
Unique Identifier   |   Description         |   Units
--------------------+-----------------------+---------
ADFVBC              |   Alpha Ventures      |   1234
<blank>             |   **KDN holdings INC  |   2155
DQDW1               |   Capital ORD         |   3214
```

创建标识符

代码语言:javascript
运行
复制
Records A description   |   Records B description   |   Created Identifiers
------------------------+---------------------------+-----------------------
KDN Holdings            |   **KDN Holdings Inc      |   IDENTIFIER1

例如,这两个文件都有KDN,但是RecordsA & RecordsB中的唯一标识符是空的。此外,这两种描述是不同的。我创建了一个新的工作表来使用这两个不同的描述创建一个标识符。在黄色中,我突出显示了我希望在宏识别A列为空白时有一个宏自动操作的等式。

我想在空白单元格中输入一个公式,vlookup对我创建的唯一标识符数据集的描述

代码语言:javascript
运行
复制
=VLOOKUP(B10,Identifiers!A:C,3,FALSE)

不确定这是否可能..。很想听听反馈。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-03-10 17:13:01

这应该能行。我还包括了许多评论,以更好地解释代码正在做什么..。

代码语言:javascript
运行
复制
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
票数 1
EN

Stack Overflow用户

发布于 2015-03-10 16:45:48

If条件缺少一个结束括号:If IsEmpty(Cells(i,1)) ThenIsEmpty()函数有一组括号,Cells()函数有一组括号,Cells()嵌入到IsEmpty()中。

票数 2
EN

Stack Overflow用户

发布于 2015-03-10 16:03:37

会是这样的:

代码语言:javascript
运行
复制
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

更新:

代码语言:javascript
运行
复制
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

对我的测试数据集来说很有魅力。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28966860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档