声明活动工作簿变量 Excel Vba

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17596857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:07:35  来源:igfitidea点击:

Declaring an active workbook variable Excel Vba

excelvbavariables

提问by user2569803

(Excel VBA)

(Excel VBA)

How would i declare an active workbook variable. or a workbook variable in general.

我将如何声明一个活动的工作簿变量。或一般的工作簿变量。

I have a program flipping back and forth between 2 excel workbooks, and currently we have it just reopen that workbook. But can I just declare it as a variable so i can reference it without re-opening. It would make my life a lot easier .

我有一个程序在 2 个 excel 工作簿之间来回翻转,目前我们只是重新打开该工作簿。但是我可以将它声明为一个变量,这样我就可以在不重新打开的情况下引用它。这会让我的生活轻松很多。

Thanks in advance!

提前致谢!

My current example:

我目前的例子:

Dim xlsMatrixUp As String
fileToOpen = Application.GetOpenFilename("Excel Files (*.xls), *.xls")
xlsMatrixUp = fileToOpen
Workbooks.Open xlsMatrixUp
ActiveWorkbook.Worksheets(4).Activate 'Grabs matrix file

'Goes back to sheet with macro
ThisWorkbook.Worksheets(4).Activate
    ActiveSheet.range("A1").Interior.Color = RGB(128, 128, 128)
'This is off a working if statement 
Workbooks.Open xlsMatrixUp
ActiveWorkbook.Worksheets(4).Activate`
'Returns to matrix and repeats. Any suggestions?

回答by Julien Marrec

Dim wB as Workbook
Set wB = Workbooks.Open(xlsMatrixUp)

then you can refer to it as wB.worksheets(4).Activatefor example

那么你可以参考它作为wB.worksheets(4).Activate例如