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

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

Related

TextJoin two columns and paste it on new worksheet using Excel VBA

From the table above, I would like to use Excel VBA with a for loop and TEXTJOIN to combine the cells so that it looks like A1, B2, C3 and paste it to a new worksheet.
Results
We can get the last row of both columns then extend the formula of textjoin till max of both... Something like this below... No need of for loop though...
' Input is in "Sheet1" & Output in "Sheet2"
Sub join()
Sheets("Sheet1").Activate
lRow1 = Cells(Rows.Count, 1).End(xlUp).Row
lRow2 = Cells(Rows.Count, 2).End(xlUp).Row
lRow = Application.WorksheetFunction.Max(lRow1, lRow2)
Sheets("Sheet2").Activate
Range("A1").Select
Range("A1").Formula = "=TEXTJOIN(,0,Sheet1!A1:B1)"
Selection.Copy
Range("A1:A" & lRow).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range("A1").Select
End Sub
Hope this Helps...

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

Autofill column until last column using macro

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

Auto-filing Formula to Last Row of a Single Column

I need to constantly start at cell R2 and auto-fill a formula down to the last row of column R. However, the number of rows is constantly changing so I need to write a macro that finds the last row and stops their. I've tried this code but keep getting errors. Any thoughts?
Sub InvoicePrice()
Dim Lastrow As Long
Lastrow = Range("R" & Rows.Count).End(xlUp).Row
Range("R2").Select
ActiveCell.FormulaR1C1 = "=RC[-2]/RC[-4]"
Selection.AutoFill Destination:=Range("R2" & Lastrow)
The error you are getting is due to this Range("R2" & Lastrow)
The range address concatenates to R21400 if the last row was 1400. So we need to add the :R to the address.
Range("R2:R" & Lastrow)
It will now concatenate to R2:R1400
There is not need to do it with three lines, one will suffice:
Sub InvoicePrice()
Dim Lastrow As Long
Lastrow = Range("P" & Rows.Count).End(xlUp).Row
Range("R2:R" & Lastrow).FormulaR1C1 = "=RC[-2]/RC[-4]"
End Sub
Not sure if you already tried this:
Sub InvoicePrice()
Dim Lastrow As Long
Lastrow = Range("P2").End(xlDown).Row
Range("R2").Select
ActiveCell.FormulaR1C1 = "=RC[-2]/RC[-4]"
Selection.AutoFill Destination:=Range("R2" & Lastrow)
It seems to me that Range("R" & Rows.Count) locates you out of current region

Find last row > merge cells > copy and paste into it - Excel VBA macros

I have no experience with VBA and it's proving to be more difficult than what I imagined...in part because I don't know the syntax, but I have the following:
Sub testMe()
LastRow = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
Worksheets("Sheet2").Range("A1").Copy Destination:=Range("A" & LastRow)
End Sub
This kinda works, but it's jamming everything into one cell in the first column. How do I merge the cells of the last row before pasting into it? The macro is supposed to find the last row of the last page, merge the cells of that row, and paste text that was copied from another cell. Thank you in advance.
This should do what you're after. You should just change the column number to reflect the column which you wish to merge cells until.
Option Explicit
Sub copy_and_paste_merge()
Dim last_row As Long
last_row = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(1, 1).Copy
Cells(last_row, 1).PasteSpecial Paste:=xlPasteValues
Range(Cells(last_row, 1), Cells(last_row, 5)).MergeCells = True 'change the column
End Sub
I ended up doing it like this...
Sub testMe()
LastRow = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
Range("A" & LastRow & ":L" & LastRow).Merge
Range("A" & LastRow) = Worksheets("Sheet2").Range("A1")
End Sub

Resources