Copy dynamic range based on last row and column with data

Josu

New Member
Joined
Mar 2, 2021
Messages
39
Office Version
  1. 2010
Platform
  1. Windows
Hello, I am trying to copy range based on number of rows and columns with data, starting with A2
So far I just can copy based on number of rows, but in case if i have more columns, my plan is not working
Can you advice please?

VBA Code:
Sub CopyTemplate()
Dim ws As Worksheet
Dim wk As Workbook
Set wk = Workbooks("XXX")
Set ws = Worksheets("Test")
ws.Activate
With ThisWorkbook.Worksheets("Test")
Dim lr As Long
'   Find last row in column A with data
    lr = Cells(Rows.Count, "A").End(xlUp).Row
'   Copy range

Range("A2:E" & lr).Copy

End With

End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Gets you last Column that Data is in. Counts blanks.
VBA Code:
Dim wb as Workbook, sht as Worksheet, lstCol as Long
Set wb = Workbooks("Workbook_Name.xlsx")
Set sht = wb.Worksheets("Sheet_Name")
lstCol = sht.UsedRange.Columns.Count
Debug.Print lstCol
 
Upvote 0
VBA Code:
Range("A2:E" & lr & lstCol).Copy
 
Last edited:
Upvote 0
@Josu , I'm sorry. I just noticed you're using 2010. I don't think that has the UsedRange property.
 
Upvote 0
This should work
VBA Code:
Dim wb as Workbook, sht as Worksheet, lstCol as Long
Set wb = Workbooks("Workbook_Name.xlsx")
Set sht = wb.Worksheets("Sheet_Name")
lstCol = sht.Columns.End(xlToRight).Column
Debug.Print lstCol
 
Upvote 0
This should work
VBA Code:
Dim wb as Workbook, sht as Worksheet, lstCol as Long
Set wb = Workbooks("Workbook_Name.xlsx")
Set sht = wb.Worksheets("Sheet_Name")
lstCol = sht.Columns.End(xlToRight).Column
Debug.Print lstCol
Thank you for response
This doesn't work, it give me result of 16384 which doesn't help me
I need to copy range which starts on A2 and can go all the way down and all the way left, this is why I am trying to understand how I can copy based on last used row and last used column
 
Upvote 0
Try this
VBA Code:
Sub CopyTemplate()
Dim ws As Worksheet
Dim wk As Workbook
Set wk = Workbooks("XXX")
Set ws = wk.Worksheets("Test")
ws.Activate
'With ThisWorkbook.Worksheets("Test")
Dim lr As Long
Dim lc As Long
'   Find last row in column A with data
    lr = ws.Rows.End(xlDown).Row
    lc = ws.Columns.End(xlToRight).Column
'   Copy range

Range(ws.Cells(2, 1), ws.Cells(lr, lc)).Copy

'End With

End Sub
 
Upvote 0
Try this
VBA Code:
Sub CopyTemplate()
Dim ws As Worksheet
Dim wk As Workbook
Set wk = Workbooks("XXX")
Set ws = wk.Worksheets("Test")
ws.Activate
'With ThisWorkbook.Worksheets("Test")
Dim lr As Long
Dim lc As Long
'   Find last row in column A with data
    lr = ws.Rows.End(xlDown).Row
    lc = ws.Columns.End(xlToRight).Column
'   Copy range

Range(ws.Cells(2, 1), ws.Cells(lr, lc)).Copy

'End With

End Sub
Thanks again, this copies 3 rows and all columns
I changed a bit code, now I managed to get things copied how I want

VBA Code:
Sub CopyTemplate()
Dim ws As Worksheet
Dim wk As Workbook
Set wk = Workbooks("Total Capped")
Set ws = wk.Worksheets("Data")
ws.Activate
'With ThisWorkbook.Worksheets("Test")
Dim lr As Long
Dim lc As Long
'   Find last row in column A with data
    lr = ws.Cells(Rows.Count, "A").End(xlUp).Row
    Debug.Print lr
'   Find last column in row 2 with data
    lc = ws.Cells(2, Columns.Count).End(xlToLeft).Column
    Debug.Print lc
'   Copy range

Range(ws.Cells(2, 1), ws.Cells(lr, lc)).Copy

'End With

End Sub
 
Last edited:
Upvote 0
Solution
Thanks again, this copies 3 rows and all columns
I changed a bit code, now I managed to get things copied how I want

VBA Code:
Sub CopyTemplate()
Dim ws As Worksheet
Dim wk As Workbook
Set wk = Workbooks("Total Capped")
Set ws = wk.Worksheets("Data")
ws.Activate
'With ThisWorkbook.Worksheets("Test")
Dim lr As Long
Dim lc As Long
'   Find last row in column A with data
    lr = ws.Cells(Rows.Count, "A").End(xlUp).Row
    Debug.Print lr
'   Find last column in row 2 with data
    lc = ws.Cells(2, Columns.Count).End(xlToLeft).Column
    Debug.Print lc
'   Copy range

Range(ws.Cells(2, 1), ws.Cells(lr, lc)).Copy

'End With

End Sub
Awesome!
 
Upvote 1

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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