Macro - find last used col, add one, and paste data

macfam929

New Member
Joined
Jul 28, 2010
Messages
13
I am writing a macro to find the last used column in a worksheet, add one to it, select row 1 in that column, and then paste a bunch of data from another worksheet into the empty column. However it wont work. :( It successfully finds the last column but will not paste into it.

I have the following code:

Dim LastCol As Integer
Sheets(LastSheetName).Select
Range("T1:T69").Select
Selection.Copy
Sheets("Summary").Select
LastCol = Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByColumns).Column
Range(Cells(1, LastCol + 1)).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=True, Transpose:=False

any help would be greatly appreicated! Thank you.

FYI: If I replace "Range(Cells(1, LastCol + 1)).Select" with a hardcoded value like "Range("F1").Select", it works no problem.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
You don't need Range in there, since you're only pasting to a single cell. Also, you don't need to select sheets or cells to perform actions on them. Something like this:

Code:
Sub a()
Dim LastCol As Integer
Dim LastSheetName As String
Sheets(LastSheetName).Range("T1:T69").Copy
With Sheets("Summary")
    LastCol = .Cells.Find(What:="*", _
    SearchDirection:=xlPrevious, _
    SearchOrder:=xlByColumns).Column
    .Cells(1, LastCol + 1).PasteSpecial xlValues
End With

End Sub
 
Upvote 0
Does this work?

Code:
Dim LastCol As Integer
Sheets(LastSheetName).Range("T1:T69").Copy
With Sheets("Summary")
    LastCol = .Cells.Find(What:="*", searchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
    .Cells(1, LastCol + 1).PasteSpecial Paste:=xlPasteValues
End With
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,726
Members
452,939
Latest member
WCrawford

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