Struggling With Reference

MikeG

Well-known Member
Joined
Jul 4, 2004
Messages
845
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I always seem to struggle with what I think should be simple references.

I am in a workbook called "Test".

I also have open a workbook called "Operations Report" which has a sheet called "SoCal".

I have a macro command in Test that I want to use to copy a range from SoCal. I am trying the following but get an error:

Workbooks("Operations Report").Worksheets("SoCal").Range(Cells(1, 1), Cells(1, 80)).Copy

Can anyone help me on what I am doing wrong?

Also, if in the macro, I set a variable f="SoCal" then how can I use this so that rather than hard-coding Worksheets("SoCal"), I can use something like Worksheets(f)?

Thanks,

MikeG
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Unqualified, Cells refers to the active sheet. So, you could do this

Code:
With Workbooks("Operations Report").Worksheets("SoCal")
    Range(.Cells(1, 1), .Cells(1, 80)).Copy
End with
... or more simply:
Code:
Workbooks("Operations Report").Worksheets("SoCal").Range("A1:CB1").Copy
 
Upvote 0
Unqualified, Cells refers to the active sheet. So, you could do this

Code:
With Workbooks("Operations Report").Worksheets("SoCal")
    Range(.Cells(1, 1), .Cells(1, 80)).Copy
End with
... or more simply:
Code:
Workbooks("Operations Report").Worksheets("SoCal").Range("A1:CB1").Copy

Thanks shg! Where did you learn all this!

The first solution works best for me, because I will be using variables to define the range x and y's e.g. x1=2, y1=7 x2=50, y1=7 and then Range(.Cells( x1, y1), .Cells(x2, y2))

In your code, if I let f="Socal" then can I use this variable f in the references instead of the hard-coded "SoCal"?

MikeG
 
Upvote 0
You're welcome!

Experimenting.

Experiment!

Ha! Thanks. I will - although one reason this board is so good is we can get answers from experts so we don't have to re-invent the wheel!
 
Upvote 0

Forum statistics

Threads
1,224,505
Messages
6,179,153
Members
452,891
Latest member
JUSTOUTOFMYREACH

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