Unable to set a variable value

rmccafferty

Board Regular
Joined
Jul 16, 2009
Messages
60
This is an excel macro and I just cannot get one variable to set a value. I have rem'd out most of the lines of the below trying to eliminate any other issues that could cause the problem. I am trying to set two variables as worksheets (Ws1 and Ws2) to excel files. And eventually I want to copy data from one to the other.

The Ws1 variable works just fine with everything else Rem's out.

When I rem out everything but the Dim and Set for Ws2, I get an error message whatever I do. I eventually set up a file named A, with a Sheet named A. And every time I run the code, I get an error with the "set Ws2" line highlighted. The lines of code break in the below due to the short width of the window. But the Set Ws1 that is 3 lines long here is only 1 line long in my code. I believe the only two lines not rem'd out are the Dim and Set commands for Ws2.

'Dim Ws1 As Worksheet
Dim Ws2 As Worksheet


' Set Ws1 = Workbooks("tAVRHeartTeamEvaluation_Data_scsv_041011.xls").Sheets("tAVRHeartTeamEvaluation_Data")
Set Ws2 = Workbooks("A.xls").Sheets("A")
' Set Ws2 = Workbooks("Book2.xls").Sheets("Sheet1")

If anyone can tell me what is wrong, I would greatly appreciate it.
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi

What is the error message? Is it "subscript out of range"?

Do your sheets represent actual worksheets or are they Chart sheets?

What happens if you use this code:

Code:
Sub Test()
Dim oSheet As Object

Set oSheet = Workbooks("book2.xls").Sheets("Sheet1")

MsgBox TypeName(oSheet)

End Sub


DK
 
Upvote 0
Thank you for the help. I really need to learn to use message boxes. I use them a lot in Access queries, but have never attempted it in Excel.
 
Upvote 0

Forum statistics

Threads
1,224,516
Messages
6,179,231
Members
452,898
Latest member
Capolavoro009

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top