我想从另一个excel工作簿

时间:2018-02-28 17:34:40

标签: excel-vba vba excel

我有这个代码,但我想设置它,如果单元格A1 =苹果,那么它将转到另一个工作簿并找到单词apple并将该值复制到我的原始工作表。 EG。

练习册(1)

A1 = APPLES
A2 =梨子

练习册(2)

A1 = APPLES __B1 = PIE

A2 = PEARS ___ B2 = JUICE

工作簿(1)将获得与工作簿(2)中的值的匹配,并带来来自列" B"的值。到工作簿(1)栏" B"

我希望能成为

If .SelectedItems.Count > 0 Then
    Application.Workbooks.Open .SelectedItems(1)

    Set xAddWb = Application.ActiveWorkbook

    Set xRng1 = Range("A4:E50")
    xWb.Activate

    Set xRng2 = Range("A1")
    xRng1.Copy xRng2

    xRng2.CurrentRegion.EntireColumn.AutoFit

    xAddWb.Close False
End If

2 个答案:

答案 0 :(得分:0)

试试此代码..与您的问题类似

Sub VLOOKUP_Closed_Workbook()
Dim wbk         As Workbook
Dim ws          As Worksheet
Dim sh          As Worksheet
Dim i           As Integer
Dim sFolder     As String
Dim fName       As String

Application.ScreenUpdating = False
    Set ws = ThisWorkbook.ActiveSheet
    sFolder = ThisWorkbook.Path & "\"
    fName = Dir(sFolder & "ClosedWB.xlsm")
    Set wbk = Workbooks.Open(sFolder & fName)
    Set sh = wbk.Worksheets("Sheet1")

    With ws.Range("B1:B" & ws.Cells(Rows.Count, 1).End(xlUp).Row)
        .Formula = "=IFERROR(VLOOKUP(A1,[ClosedWB.xlsm]Sheet1!$A:$B,2,FALSE),""Not Found"")"
        .Value = .Value
    End With

    wbk.Close False
Application.ScreenUpdating = True
End Sub

答案 1 :(得分:0)

这段代码有很多帮助,我有一个查找值,但我需要提取3列信息。但每次我使用这个代码,它一直要求我选择文件夹,但我只想这样做一次。另外,如果可能的话,我想将查找值放在数组中。

Set wsk = ThisWorkbook.ActiveSheet

With wsk.Range("B1:b" & wsk.Cells(Rows.Count, 1).End(xlUp).Row)
    .Formula = "=IFERROR(VLOOKUP(A1,[ClosedWB.xlsm]Sheet1!$A:$i,5,FALSE),""Not Found"")"
    .Value = .Value
End With


With wsk.Range("C1:c" & wsk.Cells(Rows.Count, 1).End(xlUp).Row)
    .Formula = "=IFERROR(VLOOKUP(A1,[ClosedWB.xlsm]Sheet1!$A:$i,9,FALSE),""Not Found"")"
    .Value = .Value
End With

With wsk.Range("D1:D" & wsk.Cells(Rows.Count, 1).End(xlUp).Row)
    .Formula = "=IFERROR(VLOOKUP(A1,[ClosedWB.xlsm]Sheet1!$A:$i,7,FALSE),""Not Found"")"
    .Value = .Value
End With