Workbooks copy

GrayCoder

New Member
Joined
Mar 10, 2023
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Requesting assistance on this.

This formula works:
Workbooks(Mybook).Activate
Workbooks(Mybook).Worksheets(Worksheetname).Range("A1:AZ" & ICount).Copy _
'Workbooks("005.xlsx").Worksheets(1).Range("A1")

However it ceases when changing the "005.xlsx" to the statement: "CellA" & ".xlsx" - and CellA equals 005

Workbooks(Mybook).Activate
Workbooks(Mybook).Worksheets(Worksheetname).Range("A1:AZ" & ICount).Copy _
Workbooks("CellA" & ".xlsx").Worksheets(1).Range ("A1")

Thanks for the assistance.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
What is CellA? Is it a variable, a named range? Chances are, it should not be in quotes.
 
Upvote 0
It's a variable. It has this coding earlier in the process:

Dim CellA As String
Do While ActiveCell.Value <> ""
CellA = Range("A3").Value

In which A3 contains a company #.

Removing the quotes gave this message: Object doesn't support this property or method.

The program is copying data in one workbook to another... and at this point in the code it saves the second workbook:

Workbooks(Mybook).Activate
Workbooks(Mybook).Worksheets(Worksheetname).Range("A1:AZ" & ICount).Copy _
Workbooks("CellA" & ".xlsx").Worksheets(1).Range ("A1")

But CellA is from the first workbook. Is there another code that needs to be added? Thanks..
 
Upvote 0
However it ceases when changing the "005.xlsx" to the statement: "CellA" & ".xlsx" - and CellA equals 005
If CellA is indeed a variable, you do NOT want double-quotes around it!
Everything enclosed in double-quotes is treated as literal text.
So the workbook name you are building is literally "CellA.xlsx".

Variables always need to be outside of double-quotes, i.e.
VBA Code:
Workbooks(CellA & ".xlsx").Worksheets(1).Range("A1")
 
Upvote 0
Thanks... it was the quotes. Had to remove them from a couple of lines and it now works.
 
Upvote 0
Thanks... it was the quotes. Had to remove them from a couple of lines and it now works.
You are welcome.

The key points to remember are:
- Everything enclosed in double-quotes is treated as literal text
- All variables would need to be outside of double-quotes to be treated as a variable and not literal text
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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