VBA: Copy and paste (last row and last column) - syntax error

Showroom

New Member
Joined
Feb 13, 2017
Messages
15
Hi,
I am having the following syntax error in VBA and I am not sure what the correct syntax is:

Sheet1
Date
Com1
Com2
Com3
Com4
Com5
Date1
Date2
Date3
Date4
Date5

<tbody>
</tbody>

So I want to copy the above into sheet2 in VBA. This is straightforward if I use range (A1:A6) and range (A1:F1), but I want to do it more dynamic by using last-column and last-row.

At this moment I have the following code:

Sub test()

Dim lRow As Long
Dim lCol As Long

lRow = Cells(Rows.Count, 1).End(xlUp).Row
lCol = Cells(1, Columns.Count).End(xlToLeft).Column

Worksheets("Ark1").Range("A1:A" & lRow).Copy Worksheets("Ark2").Range("A1:A" & lRow)

Worksheets("Ark1").Range("A1:" & lCol??).Copy Worksheets("Ark2").Range("A1:" & lCol??)


End Sub

Ark1 = sheet1 and Ark2 = sheet2 (I am using a danish version of Excel)

So the code is able to copy (A1:A6) from sheet1 into (A1:A6) in sheet2, but I am not sure on how to set the columns up. What is the syntax here? The variable lcol in the example above is only F right and not F1??

Any suggestion?

Thanks!!




 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Worksheets("Ark1").Range(Cells(1, 1), Cells(lRow, lCol)).Copy Worksheets("Ark2").Range("A1")
 
Upvote 0
Thank you both of you! I think, that I am made it more difficult than it is :)

Thank you again! It is working now!!
 
Upvote 0

Forum statistics

Threads
1,217,404
Messages
6,136,419
Members
450,011
Latest member
faviles5566

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