简体   繁体   中英

Macro to pull data from one book to another

when i run the macro, a prompt appears saying "Compile Error: Type Mismatch" and I don't know if the Application.index is right. What do you think is/are the problems with the macro? thanks.

Sub IndexMatch()    
    Application.ScreenUpdating = False

    Dim A As Workbook
    Dim B As Workbook

    Dim Aa As Worksheet
    Dim Bb As Worksheet

    Dim SearchThis As Range
    Dim LookAtE As Range
    Dim LookAtG As Range
    Dim LookAtQ As Range

    Set A = ThisWorkbook
    Set B = "Tables.xlsm"
    Set Aa = A.Sheets("Sheet24")
    Set Bb = B.Sheets("Sheet1")
    Set SearchThis = Aa.Range("A2")
    Set LookAtE = Bb.Range("E")
    Set LookAtG = Bb.Range("G")
    Set LookAtQ = Bb.Range("Q")

    Workbooks.Open ("C:\Users\admin\Documents\Tables.xlsm")
            ThisWorkbook.Activate

    Aa.Range("A4").Value = Application.Index(LookAtG, Match(SearchThis, LookAtE))
    Aa.Range("A5").Value = Application.Index(LookAtQ, Match(SearchThis, LookAtE))

  Workbooks("Tables.xlsm").Close SaveChanges:=True
 Application.ScreenUpdating = True
End Sub

Try setting it up like so:

Set B = Workbooks("Tables.xlsm")

This should work if the other table is also open.


If it's not open, try this:

Set B = Workbooks.Open("C:\...\Tables.xlsm")
Sub IndexMatch()
    Application.ScreenUpdating = False

    Workbooks.Open ("C:\Users\admin\Documents\Tables.xlsm")
            ThisWorkbook.Activate

    Dim A As Workbook
    Dim B As Workbook

    Dim Aa As Worksheet
    Dim Bb As Worksheet

    Dim SearchThis As Range
    Dim LookAtE As Range
    Dim LookAtG As Range
    Dim LookAtQ As Range

    Set A = Workbooks("PT9.xlsm")
    Set B = Workbooks("Tables.xlsm")
    Set Aa = A.Worksheets("Sheet2")
    Set Bb = B.Worksheets("Documents")
    Set SearchThis = Aa.Range("A2")
    Set LookAtE = Bb.Range("E:E")
    Set LookAtG = Bb.Range("G:G")
    Set LookAtQ = Bb.Range("Q:Q")


    Aa.Range("A4").Value = Application.Index(LookAtG, WorksheetFunction.Match(SearchThis, LookAtE))
    Aa.Range("A5").Value = Application.Index(LookAtQ, WorksheetFunction.Match(SearchThis, LookAtE))


  Workbooks("Tables.xlsm").Close SaveChanges:=True
 Application.ScreenUpdating = True
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM