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

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

Related

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

VBA Finding Last Row

I'm trying to copy down a formula down to the last row. I've got code for doing so but it won't work.
My code to find the last row is:
lRow = ws.Range("G" & ws.Rows.Count).End(xlUp).Row
The rest of my code is as follows:
Sub Inventory()
Dim lRow As Integer
lRow = ws.Range("G" & ws.Rows.Count).End(xlUp).Row
'Inventory Macro
Range("G2").Select
' Selects cell G2
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RC[-5]-RC[-2]"
' Calculates Inventory for the first month by subtracting Production Units from Demand
Range("G3").Select
' Selects cell G3
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RC[-5]-RC[-2]+R[-1]C"
' Calculates Inventory for the rest of the months by subtracting Production Units from Demand plus the previous month's Inventory
Range("G3").Select
Selection.AutoFill Destination:=Range("G3:G" & lRow)
Range("G3:G" & lRow).Select
End Sub
Your problem probably stems from the column in which you determine the last row. Here is another version of your code that avoids selecting anything.
Sub Inventory2()
'Inventory Macro
' Calculates Inventory for the rest of the months by subtracting
' Production Units from Demand plus the previous month's Inventory
Dim Ws As Worksheet
Dim lRow As Long
Set Ws = ActiveSheet ' better: name the sheet: Worksheets("Sheet1")
With Ws
lRow = .Cells(.Rows.Count, "A").End(xlUp).Row ' this can't be column G
.Cells(2, "G").FormulaR1C1 = "=RC[-5]-RC[-2]"
.Range(.Cells(3, "G"), .Cells(lRow, "G")).FormulaR1C1 = "=RC[-5]-RC[-2]+R[-1]C"
End With
End Sub

VBA sorting a range with no hardcoding

I am trying to sort a range of cells without hardcoding the starting and stopping points. My range will always start in column A and span to column M, however the rows it stops and starts on will vary. I figured out how to set a variable and incorporate it into the Range function for the last-row, but I can't figure out how to do that for the row the range should start on. Here is my code so far:
Range("A6:M" & lastRow).Sort Key1:=Range("A6"), Order1:=xlAscending, Header:=xlNo
This works only if I start my range on row 6. What I can't figure out is how to tell excel to start my range on the first row that is not blank in column A.
Thanks!
Determine if A1 is blank. Use A1 if not blank and the [ctrl]+[down] if it is blank.
dim startRow as long, lastRow as long
With worksheets("sheet1")
lastRow = .cells(.rows.count, "M").end(xlup).row
if isempty(.cells(1, "A") then
startrow = .cells(1, "A").end(xldown).row
else
startRow = 1
end if
with .range(.cells(startrow, "A"), .cells(lastrow, "M"))
.cells.Sort Key1:=.cells(1), Order1:=xlAscending, Header:=xlNo
end with
end with

Excel Macro to add COUNTA to new top row

I have an Excel 2013 worksheet where each column has a header row, and then the word "DIRECT" in some or all of the cells. No other data exists in the columns, just "DIRECT" or blanks. No columns are blank, they all have "DIRECT" at least once.
I'm looking for a macro that does the following:
Adds a new top row
Ignores the original header row, but gets a count of the cells with "DIRECT" in them
Puts that number in the corresponding new top cell for each column
Does the above actions for each column in the worksheet
Works regardless of the last column or row with data (I have to run this on several different-sized worksheets)
I recorded a macro that gets close, but it has two problems:
It adds the COUNTA data out to the last row of the workbook, which isn't needed (the populated columns will be a couple hundred, not thousands)
It references a specific cell range, so could cut off data for sheets with more rows
Sub AddColumnCountsRecorded()
'
' AddColumnCounts Macro
'
'
Rows("1:1").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("J1").Select
ActiveCell.FormulaR1C1 = "=COUNTA(R[2]C:R[15]C)"
Range("J1").Select
Selection.Copy
Range(Selection, Selection.End(xlToRight)).Select
ActiveSheet.Paste
End Sub
If it helps:
Column "A" can determine the last row where data could be (that's the "username" column", so no blanks there) - although this last row will also change from sheet to sheet.
Row 2 (the header row) can determine the last column where data could be - it has no blank columns; in each column, at least one cell will have the word "DIRECT".
Any advice on editing the existing macro or concocting a new one from scratch would be greatly appreciated!
Thanks!
UPDATE:
Much thanks to Scott, here's what I ended up with - this adds the non-blank cell count to the active worksheet and stops at the last row with data in it. I just call it directly, without the 2nd section of code proposed below:
Sub AddColumnCountsRecorded()
With ActiveSheet
.Rows("1:1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Dim lRow As Long, lCol As Long
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
lCol = .Cells(2, .Columns.Count).End(xlToLeft).Column
.Range(.Cells(1, 2), .Cells(1, lCol)).FormulaR1C1 = "=COUNTA(R[2]C:R[" & lRow & "]C)"
End With
End Sub
Give this a shot. I made a separate sub that you can pass the worksheet reference too.
Sub AddColumnCountsRecorded(ws As Worksheet)
With ws
.Rows("1:1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Dim lRow As Long, lCol As Long
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
lCol = .Cells(2, .Columns.Count).End(xlToLeft).Column
.Range(.Cells(1, 2), .Cells(1, lCol)).FormulaR1C1 = "=COUNTA(R[2]C:R[" & lRow & "]C)"
End With
End Sub
Call it like this:
Sub ColumnCount()
AddColumnCountsRecorded Worksheets("Sheet1")
End Sub

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