简体   繁体   中英

Excel VBA: refer to an array table in a different workbook than the active workbook

I'm using the Find and Replace macro (see below) that refers to an array table storing the reference data.

There's NO way that the only way to reference an array table and stored values for Find/Replace macro is from within any given ActiveWorkbook I'm currently working.

I need to be able to use and refer to my Personal.xlsb default template, because it's ALWAYS open, to reference the Array Table, below.

How can I point the macro to the Array Table this way, so I don't have to copy/create it into every new Workbook that I want to run this macro?

This is the current reference from macro below and the only way I can get it to work:

'Create variable to point to your table  
Set tbl = Worksheets("Table1").ListObjects("Table1")

(Referring to a sheet inside the ActiveWorkbook I'm using.)

Here's what I've tried instead, so far, in an effort to reference another file:

Set tbl = Personal.xlsb("Sheet1").ListObjects("Table1") 
                ------ 

I thought I had it With this one For sure!! 
Set tbl = Workbooks("Personal.xlsb").Sheets("Table1").ListObjects("Table1") 
                ------- 

Set tbl = Workbooks("Personal.xlsb").Sheets(1).ListObjects("Table1")
                -------

Set wb1 = Application.Workbooks("Personal.xlsb") 
Set tbl = wb1("Table1").ListObjects("Table1") 
                -------

Set wb1 = Workbooks("Personal.xlsb").Sheets("Table1") 
Set tbl = wb1("Table1").ListObjects("Table1") 
                ------- 

Set wb1 = Workbooks("Personal.xlsb").Sheets("Table1") 
Set tbl = wb1.ListObjects("Table1") 

I've even tried the absolute file path, nothing.

As soon as I change it to try to refer to Personal.xlsb using strings above, I get Error 9 : subscript out of range

Right now my Array Table MUST be copied into every Activeworkbook on the 'Table1' Sheet (Tab), every time I want to use this macro!

Sub FindReplace_Multi_ActivesheetOnly()

Dim sht As Worksheet
Dim fndList As Integer
Dim rplcList As Integer
Dim tbl As ListObject
Dim myArray As Variant

'Create variable to point to your table
  Set tbl = Worksheets("Table1").ListObjects("Table1")

'Create an Array out of the Table's Data
  Set TempArray = tbl.DataBodyRange
  myArray = Application.Transpose(TempArray)

'Designate Columns for Find/Replace data
  fndList = 1
  rplcList = 2

'Loop through each item in Array lists
  For x = LBound(myArray, 1) To UBound(myArray, 2)
    'Loop through each worksheet in ActiveWorkbook (skip sheet with table in it)
      'For Each sht In ActiveWorkbook.Worksheets
        'If sht.name <> tbl.Parent.name Then

          ActiveSheet.Cells.Replace What:=myArray(fndList, x), Replacement:=myArray(rplcList, x), _
            LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False

        'End If
      'Next sht
  Next x

End Sub

You've almost tried everything!

The syntax is: Workbook(Index) the Index can either be a number (starting with 1 for the first open workbook) or a named Index which is the name of the Workbook (not a /path/to/file, nor is it a file.ext)

So to set your Personal Workbook to a workbook object, you would use.

Dim wb as Workbook
Set wb = Workbooks("Personal")

As for referencing sheets and tables (the rest of the code) appears correct.

If you're still having issues finding the name of the workbook, if you crack open the VBA editor you'll see on the left side (for a new, unsaved workbook) it shows VBAProject (Book1) Book1 is the name of the workbook, OR you can open up the immediate window, (CTRL+G) and type ?ActiveWorkBook.Name and press enter, and it will output the name.

FINALLY! With some help and feedback here and there, we figured it out!

The main issue, honestly, was knowing the TRUE reference NAMES. 2nd was the proper syntax. I was really close there from the beginning.

The weird part was learning that the table name (ListObject) was not what I'd thought. There's several ways to find out from highlighting the table and clicking the design tab that appears or just use (CTRL+G) like Adam mentioned. I found that the table was actually named Table13, not Table1 and without this correct none of the syntax mattered!

This was the proper vba coding syntax I needed to add/change, though I'm certain it's not the only way. This was the key: ("Table13")

Dim wb As Workbook
Set wb = Workbooks("Personal.xlsb")

'Create variable to point to your table
  Set tbl = wb.Worksheets("Table1").ListObjects("Table13")

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