Find First and Last Rows to Determine Range

vvSTRIDEvv

Board Regular
Joined
Jun 17, 2010
Messages
74
I am trying to copy/paste some data from one sheet to another.

The ranges are always different, so I'd need VBA to determine the range to copy as well as the paste area.
Here is what I have so far, and it is not working. I don't get any error's, just nothing happens...

Please have a look, and let me know where I've gone wrong...

Code:
Dim firstrow As String
Dim lastrow As String
Dim wsh1 As Worksheet
Dim wsh2 As Worksheet

wsh1 = Worksheets("strsiteid")
wsh2 = Worksheets("Dashboard")

    
firstrow = wsh1.UsedRange.Rows(1).Row
lastrow = wsh1.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row

Range(firstrow, lastrow).Copy _
Destination:=wsh2.Range("C" & wsh2.Rows.Count).End(xlUp).Offset(1, 0)
Thanks
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
You need an ADDRESS rather than just the first and last row numbers.
Code:
Sub GetRange()
Dim firstrow As String
Dim lastrow As String
Dim wsh1 As Worksheet
Dim wsh2 As Worksheet

Set wsh1 = Worksheets("strsiteid")
Set wsh2 = Worksheets("Dashboard")

'Get Address of Used Range
    URAddy = wsh1.UsedRange.Address
'Copy to other WorkSheet
    wsh1.Range(URAddy).Copy _
    Destination:=wsh2.Range("C" & wsh2.Rows.Count).End(xlUp).Offset(1, 0)
'Clear Memory
    Set wsh1 = Nothing
    Set wsh2 = Nothing
End Sub
 
Upvote 0
Change the Copy section like this:
Code:
Sub GetRange2()
Dim firstrow As String
Dim lastrow As String
Dim wsh1 As Worksheet
Dim wsh2 As Worksheet

Set wsh1 = Worksheets("strsiteid")
Set wsh2 = Worksheets("Dashboard")

'Get Address of Used Range
    URAddy = wsh1.UsedRange.Address
'Copy to other WorkSheet
    wsh1.Range(URAddy).Copy
    wsh2.Range("C" & wsh2.Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlValues
'Clear Memory
    Set wsh1 = Nothing
    Set wsh2 = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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