简体   繁体   English

Excel VBA:引用与活动工作簿不同的工作簿中的数组表

[英]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. 没有办法,引用数组表和查找/替换宏的存储值的唯一方法是从我正在使用的任何给定的ActiveWorkbook中。

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. 我需要能够使用并引用我的Personal.xlsb默认模板,因为它总是打开,以引用下面的数组表。

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.) (参考我正在使用的ActiveWorkbook内的工作表。)

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 一旦我改变它以尝试使用上面的字符串引用Personal.xlsb, 我得到错误9:下标超出范围

Right now my Array Table MUST be copied into every Activeworkbook on the 'Table1' Sheet (Tab), every time I want to use this macro! 现在每次我想使用这个宏时,我的数组表必须复制到'Table1'表(Tab)上的每个Activeworkbook中!

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) 语法是: Workbook(Index) Index可以是一个数字(从第一个打开的工作簿开始为1)或一个命名的Index ,它是工作簿的名称(不是/ path / to / file,也不是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. 如果您仍然在查找工作簿的名称时遇到问题,如果您打开VBA编辑器,您将在左侧看到(对于新的未保存的工作簿),它显示VBAProject (Book1) Book1是工作簿的名称,或者您可以打开即时窗口(CTRL+G)并键入?ActiveWorkBook.Name并按Enter键,它将输出名称。

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. 奇怪的部分是学习表名(ListObject)不是我想的。 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. 有几种方法可以从突出显示表格中单击出现的设计选项卡,或者像Adam提到的那样使用(CTRL + G)。 I found that the table was actually named Table13, not Table1 and without this correct none of the syntax mattered! 我发现该表实际上名为Table13,而不是Table1,没有这个正确的语法都没有!

This was the proper vba coding syntax I needed to add/change, though I'm certain it's not the only way. 这是我需要添加/更改的正确的vba编码语法,但我确信它不是唯一的方法。 This was the key: ("Table13") 这是关键:( “表13”)

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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