I have recorded a macro in Excel, but when I run it with updated data, it only applies to the number of rows I had when I first had the macro recorded. I dove into the macro code and I think these are the problems:
Selection.AutoFill Destination:=Range("L2:R2"), Type:=xlFillDefault
Range("L2:R2").Select
Selection.AutoFill Destination:=Range("L2:R242")
Range("L2:R242").Select
How can I adjust the macro to have the range set up as the size of the worksheet?
Save the last row of your data into a variable and then modifiy the line to always fill the formula to that value like this:
Dim Last_Row as Long
Last_Row = Application.ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
Range("L2:R2").AutoFill Destination:=Range("L2:R" & Last_Row), Type:=xlFillDefault
(Rows.Count, 1) this 1 represents the Column A. I supposed your data is here
I recorded a macro and I tried to autofill column C with the COUNTIF function all the way down until B column has their last cell value. It doesn't work with new data so I tried to edit the macro and replace it with Range("D2:D") but that doesn't work.
Sub COUNTIF()
'
' COUNTIF Macro
'
'
Range("D2").Select
ActiveCell.FormulaR1C1 = "=COUNTIF(C[-3],RC[-3])>1"
Range("D2").Select
Selection.AutoFill Destination:=Range("D2:D")
End Sub
Better to find the last cell. Also avoid using select as it slows down your code.
Sub COUNTIF()
Dim LastRowB As Long
LastRowB = Range("B" & Rows.Count).End(xlUp).Row
Range("D2").FormulaR1C1 = "=COUNTIF(C[-3],RC[-3])>1"
Range("D2: " & "D" & LastRowB).FillDown
End Sub
Now change your "=COUNTIF(C[-3],RC[-3])>1" formula to whatever it is you really want to fill down.
I would like to dynamically fill a column. However, with the VBA code I have now, the formula fills all the way to the max row count (1.04 million). I would like it to stop at the max rows found in the column to its left (same as manual auto fill)
Sub Vlookup()
Range("C2").Select
ActiveCell.FormulaR1C1 =
"=IFERROR(VLOOKUP(RC[-2],'C:\Test.xlsx'!R2C6:R1000C15,10,FALSE),"""")"
Selection.AutoFill Destination:=Range("C2", Range("C2").End(xlDown))
End Sub
I tried to use
Selection.Autofill Destination :=
Range("C2",Selection.Range("C2").End(xldown))
But that didnt seem to work either
You always want to use Long when finding the last row. Integers only go to 32,767 and there are over 1 million rows. You should also avoid using Select.
Dim lastRow as Long
lastRow=Cells(Rows.Count,2).End(xlup).Row
Range("C2").AutoFill Destination:=Range("C2:C" & lastRow)
first you can get the last row coming from column B and use that as reference for auto fill. see my example below.
Dim lastRow As Integer
Range("B1").Select
Selection.End(xlDown).Select
lastRow = Selection.Row
Range("C2").Select
Selection.AutoFill Destination:=Range("C2:C" & lastRow)
I am trying to produce some code that will use the AutoFill Down function. I would like to fill Column B with the typical 1,2,3,4, etc. as long as there is text/values in the respective rows of Column A. Right now, my code is hardwired to fill down to cell B50 no matter what, but I don't want it to paste that far down if Column A only has data through cell A7, for example. Ideally, I would like to use the following variable -- rownum = Range("A1").End(xlDown).Row -- to count the number of cells in Column A that have text/values, and use that to replace "B50" in the row designation below. Just not sure of the appropriate syntax to make that happen. Here is the code I have so far.
ActiveCell.FormulaR1C1 = "1"
Range("B2").Select
ActiveCell.FormulaR1C1 = "2"
Range("A1").Select
Range("B1:B2").Select
Selection.AutoFill Destination:=Range("B1:B50"), Type:=xlFillDefault
Thanks in advance to anyone who helps me out! I am a new user of both Macros and VBA Code, and the amount of knowledge that so many of you have amazes me!
Econ
You're much better off if you don't select anything in your code. If you just want to number the rows in column B based on the contents of column A, this will do the trick:
Sub NumberColumn()
Dim ws As Worksheet
Dim lastRow As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
' get the last row from column A that has a value
lastRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
' use the last row to determine how far down to extend the formula
ws.Range("B1:B" & lastRow).Formula = "=row()"
End Sub
Pretty simple option to use without the need for a string:
Selection.AutoFill Destination:=Range("A2:A" & Cells(Rows.Count, "B").End(xlUp).Row)
Same goes for copying the number of rows in a column:
Range("A2:A" & Cells(Rows.Count, "B").End(xlUp).Row).Select
Selection.Copy
I had a question regarding the code below. If anyone could provide some insight, I would be greatly appreciative. Essentially this is part of a report I'm building to be run on a weekly basis. The issue I'm having is that the reference column K is variable in size of inputs and I hate putting in a constant that results in a bunch of N/A#s (K rows should never exceed 300). I'm sure it's something simple I'm overlooking, but I tried to write something to count K2:Kn and then input it in the Ranges (q2:r2:s2:t2:qrstn). Please share your wisdom =)
Sub Vlookup()
Sheets(2).Select
Range("q2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-6],table,15,FALSE)"
Range("q2").Select
Selection.AutoFill Destination:=Range("q2:q300"), Type:=xlFillDefault
Range("r2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-7],table,16,FALSE)"
Range("r2").Select
Selection.AutoFill Destination:=Range("r2:r300"), Type:=xlFillDefault
Range("s2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-8],table,17,FALSE)"
Range("s2").Select
Selection.AutoFill Destination:=Range("s2:s300"), Type:=xlFillDefault
Range("t2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-9],table,18,FALSE)"
Range("t2").Select
Selection.AutoFill Destination:=Range("t2:t300"), Type:=xlFillDefault
Hope I've understood your question properly
The following will autofill range Q2:QN where N is the last used cell in column Q
Selection.AutoFill Destination:=Range(Range("K2"),Range("K2").End(xlDown)).Offset(ColumnOffset:=6), Type:=xlFillDefault
You could also do it the other way round as you have a fixed maximum.
Selection.AutoFill Destination:=Range(Range("K2"),Range("K301").End(xlUp)).Offset(ColumnOffset:=6), Type:=xlFillDefault
The first looks down the sheet for the last row, and the 2nd looks up the sheet from the maximum to the last row.
You can replace Q with whatever column you need, as you appear do do the same for lots of columns!
Update
I've updated the example, it now finds the used range in the K column and offsets it to your desired column. The value of ColumnOffset will be the same (although positive) as the value used in your R1C1 formulas.
Also, as the K Range should remain constant, and your formulas are fairly predictable you could shorten it a little. like the following (i've not testing it, just playing around)
Sub Vlookup() ' might want to re-name, to avoid confusion with the worksheet function
Dim R As Range
Dim A As Range
Dim I As Integer
Set R = Range(Range("K2"), Range("K301").End(xlUp))
Set A = Range("K2")
For I = 6 To 9
A.Offset(ColumnOffset:=I).FormulaR1C1 = "=VLOOKUP(RC[-" & CStr(I) & "], table, " & CStr(I+9) & ", FALSE)"
A.Offset(ColumnOffset:=I).AutoFill Destination:=R.Offset(I), Type:=xlFillDefault
Next I
End Sub
You don't need to fill down when you use FormulaR1C1. You can apply the formula to the whole range. Here's an example.
Sub MakeVlookup()
Dim lRows As Long, lCols As Long
Dim rCell As Range
Dim rRef As Range
'Set the first cell of the reference column
Set rRef = Sheet1.Range("K2")
'Count the rows of the reference column
lRows = Sheet1.Range(rRef, rRef.End(xlDown)).Rows.Count
'Loop through the row 2 cells where you want the formula
For Each rCell In Sheet1.Range("Q2:T2").Cells
'Compute the offset back to the reference column
lCols = rCell.Column - rRef.Column
'Write the formula to the whole range
rCell.Resize(lRows, 1).FormulaR1C1 = _
"=VLOOKUP(RC[-" & lCols & "],Table1," & rCell.Column - 2 & ",FALSE)"
Next rCell
End Sub
No need to loop or address each column with a different formula!
Instead of these formulas:
Column Q: =VLOOKUP(K2,table,15,FALSE)
Column R: =VLOOKUP(K2,table,16,FALSE)
...
you can simply use =VLOOKUP($K2,table,COLUMN()-2,0) as one formula across all columns! $K2 (in R1C1: RC11) instead of K2 (in R1C1 RC[-6], RC[-7], ...) will keep column K. And COLUMN() will return you the column number of the actual cell, i.e. column Q = 17, R = 18, etc.. (I also replaced FALSE with 0 as this reduces typing for lazy blokes like me but results in the same. :-) ).
Therefore, you can reduce your macro to
Sub MakeVLOOKUP
Sheets(2).Range("Q2:T300").FormulaR1C1 = "=VLOOKUP(RC11,table,COLUMN()-2,0)"
End Sub
and achieve the same result.
If you want to flexibilize the range as in Dick's solution, you can do it like this:
Sub MakeVLOOKUP
Sheets(2).Range("Q2").Resize(Sheet1.Range("K2").End(xlDown)).Rows.Count-1,4).FormulaR1C1 = _
"=VLOOKUP(RC11,table,COLUMN()-2,0)"
End Sub