简体   繁体   中英

Macro : edit macro to work in worksheet outside of workbook

I am currently using a macro which looks up from one sheet of the workbook to another sheet of the workbook and shows the result in the first sheet. This macro is present in all of my workbooks and basically I am copy pasting the same data in the second worksheet for all the files. This is becoming a time consuming activity as all these datas are huge and takes a lot of time to be opened and pasted.

I want the same macro to use a common file outside of the workbook the macro is present instead of the tab within the same file.

I am pasting the macro used below. Basically it looks up Product Type from the tab" Product Mapping" based on a particular ID and paste this in "Syn Data" tab. The macro is run from Syn Data Tab. I want this macro to look up from a file stored in a separate location so that I can remove the Product Mapping tab from the file. Please can anyone helpme with this.

Dim a, b, i As Long

a = Sheets("Product mapping").Range("A1:E" & LastRowMap).Value
b = Sheets("Product mapping").Range("B1:E" & LastRowMap).Value

'For lookup using ACID
Dim ProdMapDict As Object
Set ProdMapDict = CreateObject("Scripting.Dictionary")      

'For lookup using ID
Dim ProdMapDict2 As Object
Set ProdMapDict2 = CreateObject("Scripting.Dictionary")

For i = 2 To UBound(a, 1)
    ProdMapDict.Item(a(i, 1)) = a(i, 5)
Next

For i = 2 To UBound(b, 1)
    ProdMapDict2.Item(b(i, 1)) = b(i, 4)
Next

'Lookup manual product type using ACID then ID

a = Sheets("Syn Data").Range("R1:T" & LastRow).Value

a(1, 3) = "Manual Product Type"

For i = 2 To UBound(a, 1)
    If ProdMapDict.exists(a(i, 1)) And a(i, 1) <> "" Then
        a(i, 3) = ProdMapDict.Item(a(i, 1))
    ElseIf ProdMapDict2.exists(a(i, 2)) And a(i, 2) <> "" Then
        a(i, 3) = ProdMapDict2.Item(a(i, 2))        
    Else
        a(i, 3) = "#N/A"        
    End If
Next

'Extract relevant columns from array into spreadsheet
Sheets("Syn Data").Range("DI1:DK" & LastRow).Value = a
Sheets("Syn Data").Range("BQ2:BQ" & LastRow).Value = Range("DK2:DK" & LastRow).Value
Sheets("Syn Data").Range("DI1:DK" & LastRow).Clear

MsgBox "Manual product type updated"
  1. Put the worksheet "Product Mapping" into its own workbook, together with the macro .

  2. Instead of Sheets("Product Mapping") say ThisWorkbook.Sheets("Product Mapping") .

    Explanation: ThisWorkbook is always a reference to the workbook containing the macro. That's why the macro should be in the same book as "Product Mapping".

  3. Have a variable to hold the reference to the workbook containing "Syn Data", and a variable to hold the reference to worksheet "Syn Data":

     Dim synDataBook as Workbook Dim synDataSheet as Worksheet For Each synDataBook In Workbooks Do On Error Resume Next set synDataSheet = synDataBook.Worksheets("Syn Data") If Not synDataSheet Is Nothing Then Exit For On Error Goto 0 Next synDataBook

    Explanation: The macro runs from the workbook containing "Product Mapping". It must either look for the workbook containing "Syn Data", or else the user must take care that the workbook containing "Syn Data" is the ActiveWorkbook .

    If you are sure that the user can be certain that "Syn Data" is the ActiveSheet when the macro is invoked then you can say set synDataSheet = ActiveSheet instead of looking it up.

  4. Use synDataSheet instead of Sheets("Syn Data").

  5. You may want to check for errors by making sure that synDataBook is what is expected:

     If synDataBook Is Nothing Then MsgBox "Cannot find worksheet ""Syn Data""." Stop End If

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