Autofill column until last column using macro - excel

I want to autofill my formula to the last column. Here is my code:
Dim LastColumn As Long
LastColumn = Cells(5, Columns.Count).End(xlToLeft).Column
Selection.AutoFill Destination:=Range("B1:" & LastColumn & "1"), Type:=xlFillDefault
I have succeeded in counting the last column by variable LastColumn, but during the autofill, it failed. So I want to autofill starting from column B to the column Lastcolumn.
Please help. What's wrong with my code?

You have a number in LastColumn, so you can't use it directly in Range. Use this instead:
Selection.AutoFill Destination:=Range(Cells(1, 2), Cells(1, LastColumn)), Type:=xlFillDefault

Related

Copy Paste a variable range with variable row number i to another cell

The following code is working in my mind but not in vba:
Dim i As Integer
If Range("H11") = i Then Range("U" & i & ":X" & i).Select
Selection.Copy
lrij = Cells(Rows.Count, "A").End(xlUp).Row
Range("A" & lrij + 1).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Range("H11").ClearContents
Range("A1").Activate
i is a variable row number.
The purpose is if I enter "2" for example in Cell "H11", the Range ("U2:X2") will be copy pasted in Column "A", row under row.
braX already helped me with writing the variable range correctly, but I'm lacking the point here I guess. He advised me to use the "Long" data type.
Thank you in advance.

Drag formula to the last row (take first cell as A6)

I need to fill the formula till the last used row. I have data from A6:A28, however while using below code its dragging the formula till row 628. Issue is with 4th line of the code. Please help me correct.
Dim Lastrow As Long
Lastrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
ActiveSheet.Range("H6:M6").Select
Selection.AutoFill Destination:=Range("H6:M6" & Lastrow), Type:=xlFillDefault
Copy Down Formulas
A Quick Fix
Just remove the 6 after the M.
Selection.AutoFill Destination:=Range("H6:M" & LastRow), Type:=xlFillDefault
An Improvement
No Select
No variable
No AutoFill
With ActiveSheet ' improve!
With .Range("H6:M" & .Cells(.Rows.Count, "A").End(xlUp).Row)
.Formula = .Rows(1).Formula
End With
End With

I want to select the dynamic cell and autofill it till the last row

the value is in cell C and lastrow2 and want to autofill till lastrow3
Range("C" & lastrow2).Select
Selection.AutoFill Destination:=Range(.Cells(lastrow2, 3), .Cells(lastrow3, 3)).FillDown
Rather than .Select and then Autofill, you can populate the entire range in one go. Is this what you are trying?
ws.Range(ws.Cells(lastrow2, 3), ws.Cells(lastrow3, 3)).Formula = _
ws.Cells(lastrow2, 3).Formula
Where ws is your relevant sheet. For example
Dim ws As Worksheet
Set ws = Sheet1

Trying to create a macro that will drag a formula to the last line with data

ActiveCell.FormulaR1C1 = "=IF(LEFT(C[-3])>0,C[-3]*6,C[-3])"
Range("L10").Select
Selection.AutoFill Destination:=Range("L10:L32"), Type:=xlFillDefault
Range("L10:L32").Select
I need the last line with data. L32 will not always be the last line.
This will find the last used row in column A and fill the formula to that row starting in L10
With ActiveSheet
Dim lstrw As Long
lstrw = .Cells(.Rows.Count, 1).End(xlUp).Row
.Range(.Cells(10, "L"), .Cells(.lstrw, "L")).FormulaR1C1 = "=IF(LEFT(C[-3])>0,C[-3]*6,C[-3])"
End With

VBA Macro to AutoFill Right until Last Column

I have a report that is automatically pulled that requires Sums to be added at the end of certain columns. The report remains static from Column A-R but the amount of columns as of S can be as few as 1 or go up until 52. The amount of rows are never static. I already know how to find the last row and my sums need to start at column J but when I try to autofill to the right to the last column that contains data (on the same row I have summed J), it spits back an error or doesn't work. Anyone know how to do this? This is what I have for code thus far.
Sub TV_Buysheet()
'
' Macro1 Macro
'
Range("J1").Select
FIRST_ROW = ActiveCell.Row
Selection.End(xlDown).Select
LAST_ROW = ActiveCell.Row
Selection.Offset(2, 0).Select
ActiveCell.FormulaR1C1 = "=SUM(R[" & FIRST_ROW - LAST_ROW - 2 & "]C:R[-2]C)"
Dim lastCol As Long
lastCol = Cells(2, Columns.Count).End(xlToLeft).Column
ActiveCell.AutoFill Destination:=Range(ActiveCell, lastCol), Type:=xlFillDefault
End Sub
Just missing the line of code that autofills to the last column...
To find the last column, the following should work
Dim lngLastColumn As Long
lngLastColumn = Cells.Find(What:="*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
Range(Activecell, Cells(Activecell.Row, lngLastColumn)).FillRight

Resources