Use variables inside Range Object

ORoxo

Board Regular
Joined
Oct 30, 2016
Messages
149
Guys, I'm trying to select all the cells on my worksheet. In order to do so, I was thinking about trying to use range and the variables lr and lc which stand for last row and last column. However, currently it's not working.

This is the code I have:

Code:
Sub Graphs()
Dim lr As Integer, lc As Integer, r As Integer


Application.ScreenUpdating = False


lr = data.Cells(Rows.Count, 1).End(xlUp).Row
lc = data.Cells(1, Columns.Count).End(xlToRight).Column
lc2 = Columns(lc)


For r = 1 To lr


    data.Rows(r).Copy
    data_pivot.Select
    data_pivot.Cells(r, 1).Select
    data_pivot.Paste
    
Next


Application.ScreenUpdating = True


MsgBox "Copy done"


data_pivot.Range(Cells(1, 1), Cells(lr, lc)).Select


Range("A1:H540").Select


End Sub

Any hint?
Thanks!
 
Last edited:

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
What do you mean by "not working" exactly?

I will say that you should qualify the Cell calls with the worksheet too:

Code:
data_pivot.Range(data_pivot.Cells(1, 1), data_pivot.Cells(lr, lc)).Select
 
Upvote 0
What do you mean by "not working" exactly?

I will say that you should qualify the Cell calls with the worksheet too:

Code:
data_pivot.Range(data_pivot.Cells(1, 1), data_pivot.Cells(lr, lc)).Select

I get Error 1004: Application-defined or object-defined error on the second part of the Cells Object

Code:
data_pivot.Cells(lr, lc)

EDIT 1: Nevermind, I think I understood why. Will be back in 5
EDIT 2: Yup, got it, thanks!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,206
Members
452,618
Latest member
Tam84

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