Autofill Columns In Row1 According To Last Filled Column In Row2

GauciG

New Member
Joined
Jun 18, 2013
Messages
22
Hi

I am looking for a macro that copies the formula in cell A1 and autofills row1 till the last filled column in row2.

Sub AutofillColumnsInRow1AccordingToLastFilledColumnInRow2()
Dim lastColumn As Long
lastColumn = Range(Columns.Count & 2).End(xlToLeft).Column
Range("A1").AutoFill Destination:=Range("A1:" & lastColumn & "1")
End Sub

Any suggestions
Kind regards
George
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Code:
Sub AutofillColumnsInRow1AccordingToLastFilledColumnInRow2()
    Dim lastColumn As Long
    LastCol = Range(Columns.Count & 2).End(xlToLeft).Column

Range("A1").Copy
Range(Cells(1, 1), Cells(1, LastCol)).Select).PasteSpecial xlPasteFormulas

End Sub

Hope this helps
 
Last edited:
Upvote 0
Code:
Sub AutofillColumnsInRow1AccordingToLastFilledColumnInRow2()
    Dim lastColumn As Long
    LastCol = Range(Columns.Count & 2).End(xlToLeft).Column

Range("A1").Copy
Range(Cells(1, 1), Cells(1, LastCol)).Select).PasteSpecial xlPasteFormulas

End Sub

Hope this helps

Thanks for reply Tim

I made some changes as follows:
Dim lastColumn - Dim lastCol
I removed the bracket after 'Select' in the last row of the code as I was getting an error

'New' code as follows

Sub AutofillColumnsInRow1AccordingToLastFilledColumnInRow22()
Dim lastCol As Long
lastCol = Range(Columns.Count & 2).End(xlToLeft).Column

Range("A1").Copy
Range(Cells(1, 1), Cells(1, lastCol)).Select.PasteSpecial xlPasteFormulas

End Sub

I am getting an error in line 2 of the code:
Run-time error '1004':
Method 'Range' of object'_Global' failed

Thanks in advance
George
 
Upvote 0
You could also try this.
Code:
Sub Fill_Right()
  Range("A1", Cells(2, Columns.Count).End(xlToLeft).Offset(-1)).FillRight
End Sub
 
Upvote 0
lastCol = Range(Columns.Count & 2).End(xlToLeft).Column


I am getting an error ..
That line would need to be

LastCol = Cells(2, Columns.Count).End(xlToLeft).Column
 
Last edited:
Upvote 0
Code:
Sub AutofillColumnsInRow1AccordingToLastFilledColumnInRow22()
Dim lastCol As Long
With ThisWorkbook.Worksheets(5) 'refer to the correct sheet
lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
.Range("A1").Copy
.Range(.Cells(1, 1), .Cells(1, lastCol)).PasteSpecial xlPasteFormulas
End With
End Sub

Tested and works
 
Upvote 0
Thank you

Amended active sheet and row number as follows:

Sub AutofillColumnsInRow1AccordingToLastFilledColumnInRow22()
Dim lastCol As Long
With ActiveSheet 'refer to the correct sheet
lastCol = .Cells(2, .Columns.Count).End(xlToLeft).Column
.Range("A1").Copy
.Range(.Cells(1, 1), .Cells(1, lastCol)).PasteSpecial xlPasteFormulas
End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,128
Members
453,021
Latest member
Justyna P

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