ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   set a workbook variable (https://www.excelbanter.com/excel-programming/433153-set-workbook-variable.html)

dstiefe

set a workbook variable
 
I would like to set my variable "wb" to a workbook object

so something like this

dim wb as workbook

set wb = ????

I would like to set the variable "wb" to a workbook that is not open and in
a different folder...can i do that?

Gary''s Student

set a workbook variable
 
I would set the path/filename to a string and then set the workbook name
after it is opened:

Sub Macro1()
Dim s As String
Dim wb As Workbook
s = "C:\test\beta.xls"
Workbooks.Open Filename:=s
Set wb = ActiveWorkbook
MsgBox (wb.Name)
MsgBox (wb.Path)
End Sub
--
Gary''s Student - gsnu200902


"dstiefe" wrote:

I would like to set my variable "wb" to a workbook object

so something like this

dim wb as workbook

set wb = ????

I would like to set the variable "wb" to a workbook that is not open and in
a different folder...can i do that?


dstiefe

set a workbook variable
 
so you have to first open the workbook to do it?

"Gary''s Student" wrote:

I would set the path/filename to a string and then set the workbook name
after it is opened:

Sub Macro1()
Dim s As String
Dim wb As Workbook
s = "C:\test\beta.xls"
Workbooks.Open Filename:=s
Set wb = ActiveWorkbook
MsgBox (wb.Name)
MsgBox (wb.Path)
End Sub
--
Gary''s Student - gsnu200902


"dstiefe" wrote:

I would like to set my variable "wb" to a workbook object

so something like this

dim wb as workbook

set wb = ????

I would like to set the variable "wb" to a workbook that is not open and in
a different folder...can i do that?


Gary''s Student

set a workbook variable
 
That is what I usually do. That way I have both the path and filename in a
single variable. In my experience, I never have to set the WorkBook Object
before I open it.
--
Gary''s Student - gsnu200902


"dstiefe" wrote:

so you have to first open the workbook to do it?

"Gary''s Student" wrote:

I would set the path/filename to a string and then set the workbook name
after it is opened:

Sub Macro1()
Dim s As String
Dim wb As Workbook
s = "C:\test\beta.xls"
Workbooks.Open Filename:=s
Set wb = ActiveWorkbook
MsgBox (wb.Name)
MsgBox (wb.Path)
End Sub
--
Gary''s Student - gsnu200902


"dstiefe" wrote:

I would like to set my variable "wb" to a workbook object

so something like this

dim wb as workbook

set wb = ????

I would like to set the variable "wb" to a workbook that is not open and in
a different folder...can i do that?


Dave Peterson

set a workbook variable
 
You can do the assignment when you open the workbook. That makes it nice if
there are no visible windows (a hidden workbook or an addin).

Sub Macro1()
Dim s As String
Dim wb As Workbook
s = "C:\test\beta.xls"
set wb = Workbooks.Open(Filename:=s)
MsgBox wb.Name & vblf & wb.path & vblf & wb.fullname
End Sub

But the workbook does have to be open to be part of the workbooks collection.

dstiefe wrote:

so you have to first open the workbook to do it?

"Gary''s Student" wrote:

I would set the path/filename to a string and then set the workbook name
after it is opened:

Sub Macro1()
Dim s As String
Dim wb As Workbook
s = "C:\test\beta.xls"
Workbooks.Open Filename:=s
Set wb = ActiveWorkbook
MsgBox (wb.Name)
MsgBox (wb.Path)
End Sub
--
Gary''s Student - gsnu200902


"dstiefe" wrote:

I would like to set my variable "wb" to a workbook object

so something like this

dim wb as workbook

set wb = ????

I would like to set the variable "wb" to a workbook that is not open and in
a different folder...can i do that?


--

Dave Peterson


All times are GMT +1. The time now is 01:31 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com