excel vlookup多个值和工作簿

时间:2018-04-09 10:48:31

标签: excel excel-vba vlookup vba

我知道之前已经问过这个话题,但没有什么能够涵盖我需要的东西。所以这就是......

我有两本工作簿。一个是从另一个程序导出的,该程序显示了一个工作人员的姓氏,名字,电子邮件和他们工作的病房。

[工作簿1示例]

enter image description here

第二个是完整的工作人员名单,它具有相同的细节,但也有一个核对清单栏。

[Workbook2示例]

enter image description here

我需要的是一个宏(可能是一个vlookup),它从工作簿1获取信息,检查工作簿2上的姓氏,名字和病房,以确保它是正确的员工成员,将电子邮件复制到工作簿2和还将工作簿2上的核对表列填充到"是"。

我担心如何将所有这些结合在一起我感到茫然。请帮忙。

这是我迄今为止所拥有的,但我的知识有限,不知道如何继续。

Private Sub UpdateTraining_Click()

Dim I As Integer
Dim Ws1 As Worksheet
Dim Ws2 As Worksheet
Dim Ws3 As Worksheet
Dim Wb As Workbook
Dim CopyData As String
Dim RwCnt As Long
Dim RwCnt2 As Long
Dim Rw As Long
Dim Clm As Long
Dim SName As String
Dim FName As String
Dim Wrd As String
Dim vArr
Dim ClmLet As String

Set Ws1 = Workbooks("Nursing Docs Training Record.xlsm").Worksheets("Staff Training Record")
Set Ws2 = Workbooks("Nursing Docs Training Record.xlsm").Worksheets("Do Not Use")

Workbooks.Open ("C:\TypeformNursingDocumentation.xlsx")
Set Ws3 = Workbooks("TypeformNursingDocumentation.xlsx").Worksheets("tWeXNp")

RwCnt = Ws3.Cells(Rows.Count, 1).End(xlUp).Row
RwCnt2 = Ws1.Cells(Rows.Count, 1).End(xlUp).Row

Rw = Ws3.Range("F2").Row
Clm = Ws3.Range("F2").Column

Table1 = Ws3.Range("F2:F" & RwCnt)

vArr = Split(Cells(1, Clm).Address(True, False), "$")
ClmLet = vArr(0)

For Each cl In Table1
    Ws3.Range(ClmLet & Rw).Select

    SName = ActiveCell.Value
    FName = ActiveCell.Offset(0, -1).Value
    Wrd = ActiveCell.Offset(0, -4).Value    
    Rw = Rw + 1

Next cl
End Sub

2 个答案:

答案 0 :(得分:0)

您可以使用公式实现此目的,但是您必须打开Workbook1才能使公式在Workbook2中有效。因此以下方法使用VBA来实现结果

将以下UDF复制到 Workbook2

中的模块中
Sub UpdateMyList()
    Dim oSourceWB As Workbook
    Dim oSourceR As Variant
    Dim iTotSRows&, iTotCRows&, iCC&, iSC&
    Dim oCurR As Variant

    Application.ScreenUpdating = False

    ' First lets get source data
    Set oSourceWB = Workbooks.Open("C:\Temp\EmpLookup.xlsx", ReadOnly:=True)    ' Change the source file name

    With oSourceWB.Worksheets("Sheet1")                                         ' Change the source sheet name
        iTotSRows = .Range("A" & .Rows.count).End(xlUp).Row
        oSourceR = .Range("A2:G" & iTotSRows)
    End With
    oSourceWB.Close False

    ' We now need the data from the sheet in this workbook to compare against
    With ThisWorkbook.Worksheets("Sheet8")                                      ' Change the sheet name to the sheet in your workbook
        iTotCRows = .Range("A" & .Rows.count).End(xlUp).Row
        oCurR = .Range("A2:H" & iTotCRows)
    End With

    ' Next, lets compare and update fields
    For iCC = 1 To UBound(oCurR)
        For iSC = 1 To UBound(oSourceR)
            If (oCurR(iCC, 1) = oSourceR(iSC, 6)) And (oCurR(iCC, 2) = oSourceR(iSC, 5)) And (oCurR(iCC, 5) = oSourceR(iSC, 2)) Then
                oCurR(iCC, 7) = oSourceR(iSC, 7)
                oCurR(iCC, 8) = "Yes"
                Exit For
            End If
        Next
    Next

    Application.ScreenUpdating = True

    ' Finally, lets update the sheet
    ThisWorkbook.Worksheets("Sheet8").Range("A2:H" & iTotCRows) = oCurR

End Sub

我评论了您需要更改对工作簿或工作表的引用的行。只要您更新了工作簿和工作表引用,就可以获得所需的结果

  

我根据您在问题中提供的列构建了上面的UDF。如果列发生更改,则必须修改UDF或动态获取列

答案 1 :(得分:0)

您可以使用和If(Countif())样式函数,其中countif检查您的值是否存在,如果匹配则if将返回true,那么您可以相应地使用if true / false值。如果您需要更多详细信息,请告诉我,但它可能看起来像这样= IF(COUNTIF(所选单元格在选定范围内),"是","否")。然后将其记录为宏并将代码复制到您的。