I'm getting a "Compile error: Expected Function or variable" from this code
Cells(3, 2).Select
ActiveCell.FormulaR1C1 = Formula 'Inserts formula
'Copies formula across the row
Cells(3, 2).Copy
Range(Cells(3, 2), Cells(3, 10)).PasteSpecial xlPasteFormulas
Range(Cells(3, 2), Cells(3, 10)).Select
'Copies the formula down to end of list
selection.Copy '<---- Here it says it can't compile
Range(selection, selection.End(xlDown)).FillDown
Using "selection" here gives the error, yet works in another workbook. I'm trying to insert a formula into a cell, copy it across a range, and fill down to the end of the block of data. How do I either get it to work again? Or, is there another way to populate a range of cells with a formula (which changes often hence putting it in a variable.)
Thank you for your help,
Samuel Smith
Related
I'd like to autofill cells across and above a dynamic range.
I have a line of numbers in row 3 and would like to put the word "Customer No." in the cell above each one.
I do this by copying A2 and pasting into C2 then dragging across
Via VBA macro recorder the code I get looks like this
Selection.Copy
Range("C2").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("C2:E2"), Type:=xlFillDefault
Range("C2:E2").Select
I was wondering if there's a way to create an autofill across a dynamic range as the number of cells in row 3 will change from time to time?
Thanks
You could do it like this:
With ActiveSheet
.Range("C3", .Cells(3, Columns.Count).End(xlToLeft)).Offset(-1).Value = .Range("A2").Value
End With
I am unable to copy all data in a column. I would only want to copy data that does not include the first row, (headers), and paste it to another sheet. My current code gives me a partial result as there are some blanks in between the data.
Please help me out as I am new to VBA. Thanks!
Below are two codes that I have tried. One is through the xldown method and the other is the lastrow. I found that using the lastrow, I am not even able to copy anything at all.
This is the xldown method, which gives me partial data (wsRawT and wsDetI are my defined worksheets):
wsRawT.Select
range("AI1").Select
ActiveCell.Offset(1, 0).range("A1").Select
range(Selection, Selection.End(xlDown)).Select
Selection.copy
wsDetI.Select
range("B1").Select
ActiveCell.Offset(1, -1).range("B1").Select
ActiveSheet.Paste
This is the lastrow method, which does not even allow me to copy anything:
Dim lastRowTD As Long
lastRowTD = wsRawT.Cells(Rows.Count, "A").End(xlUp).Row
wsRawT.range("AU2" & lastRowRD).copy
wsDetI.range("A2").PasteSpecial xlPasteValues
wsDetS.range("A2").PasteSpecial xlPasteValues
There are only small mistakes in your code, try this:
Dim lastRowTD As Long
lastRowTD = wsRawT.Cells(Rows.Count, "AU").End(xlUp).Row
wsRawT.Range("AU2:AU" & lastRowTD).Copy
wsDetI.Range("A2").PasteSpecial xlPasteValues
To explain: This code checks how large the dataset in column A of your sheet wsRawT is. Then it copys everything from cell A2 down to the last row with data. Then all the values are pasted to column A of sheet wsDetI. If you don't specifically need the values only, you could also use this simpler version of .Copy:
wsRawT.Range("AU2:AU" & lastRowTD).Copy wsDetI.Range("A2")
I can see the question has been asked a couple of times in the forum, but as someone who doesn't really know macro, I cannot implement the code provided in the answers.
So I have recorded the below macro.
Macro6 Macro
'
'
Range("C2").Select
ActiveCell.FormulaR1C1 = _
"=INDEX('Account codes'!C[-1],MATCH(DATA!RC[4],'Account codes'!C[-2],0))"
Range("C2").Select
ActiveCell.FormulaR1C1 = _
"=INDEX('Account codes'!C2,MATCH(DATA!RC[4],'Account codes'!C1,0))"
Range("C2").Select
Selection.Copy
Range("E2").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = _
"=INDEX('Account codes'!C2,MATCH(DATA!RC[4],'Account codes'!C1,0))"
Range("E2").Select
ActiveCell.FormulaR1C1 = _
"=INDEX('Account codes'!C2,MATCH(DATA!RC[3],'Account codes'!C1,0))"
Range("E2").Select
Selection.AutoFill Destination:=Range("E2:E4")
Range("E2:E4").Select
Range("C2").Select
Selection.AutoFill Destination:=Range("C2:C4")
Range("C2:C4").Select
End Sub
In simple terms, I want Selection.AutoFill Destination:=Range("E2:E4") to fill down to the last row of data, and not just to E4. That is because the data can change in size.
How do I do this?
You seem to be overwriting some of the formulas with different relative/absolute references. It really doesn't seem to matter which is used as far as relative/absolute addressing is concerned but you have two distinct formulas for column E which reference two different cells on the data worksheet. I'll use the latter of the two.
Put all of the formulas into all of the cells at once. AutoFill is overrated and functionally redundant in this case.
The formulas for column C seem to be using a relative reference to a cell in column F. You can use that column to determine the last row to put formulas into.
sub buildFormulas()
dim lr as long
with worksheets("data")
lr = .cells(.rows.count, "F").end(xlup).row
.range(.cells(2, "C"), .cells(lr, "C")).formular1c1 = _
"=INDEX('Account codes'!C2, MATCH(RC7, 'Account codes'!C1, 0))"
.range(.cells(2, "E"), .cells(lr, "E")).formular1c1 = _
"=INDEX('Account codes'!C2, MATCH(RC8, 'Account codes'!C1, 0))"
end with
end sub
My problem is following. I am recording macro for for sheet that´s range is dynamic.
The formula is =CountIF(A:A;A2) and recorded it:
Range("M2").Select
ActiveCell.FormulaR1C1 = "=COUNTIF(C[-12],RC[-12])"
Range("M2").Select
Selection.AutoFill Destination:=Range("M2:M4333"), Type:=xlFillDefault
Range("M2:M4333").Select
Since the rows are dynamic, I want to end every formula to the lastrow. And I searched here for answer and copied this fuction to the beginning of the macro.
Function GetLastRow(sht As Worksheet, col As String) As Integer
GetLastRow = sht.Range(col & CStr(sht.Rows.Count)).End(xlUp).row
Now I am trying to call this to the orginal recorded macro
Range("M2").Select
Range(Selection, Selection.End(xlDown)).Select
Formula1 = "=COUNTIF(A:A" & GetLastRow(Sheets("Sheet1"), "A") & ",A2))"
I changed it looking like the formula that is copied in worksheet and not like it´s recorded. I would appreciate some help to this. It would be better if the lastrow function is used with recorded formula since I have lots other formulas to come with same problem.
You want to fill column M from row 2 down to the row in column M that meets the last populated cell in column A. Use the Range.Offset property and Range.Address property to get the correct address to use in the COUNTIF function.
With Worksheets("Sheet1") 'you should know what worksheet you are on!
With .Range("M2:M" & .Cells(.Rows.Count, "A").End(xlUp).Row)
.FormulaR1C1 = "=COUNTIF(" & .Offset(0, -12).Address(ReferenceStyle:=xlR1C1) & ", RC1)"
End With
End With
I am trying to use macro recorder in Excel to record a macro to fill down a column of cells, however because the fill down each time is a different number of cells it either fills down to short or too long and this seems to be because the macro identifies the cell range and its fixed.
Is there anyway that I can get it to fill down to the last populated neighbouring cell. E.g. AutoFill down column E until it reaches the last populated row in column D. I have looked at some examples on here but the code all looks very different so not sure if it can be done with macro recorder or I have to get someone to write some code or is it something that has to be done manually?
This is the code that I have in the macro.
ActiveCell.FormulaR1C1 = _
"=IF(MONTH(RC[-1])>3,"" ""&YEAR(RC[-1])&""-""&RIGHT(YEAR(RC[-1])+1,2),"" ""&YEAR(RC[-1])-1&""-""&RIGHT(YEAR(RC[-1]),2))"
Selection.AutoFill Destination:=Range("E2:E1344")
'Selection.AutoFill Destination:=Range("E2:E1344")
Range("E2:E1344").Select
If anyone can help i'd be extremely grateful
Untested....but should work.
Dim lastrow as long
lastrow = range("D65000").end(xlup).Row
ActiveCell.FormulaR1C1 = _
"=IF(MONTH(RC[-1])>3,"" ""&YEAR(RC[-1])&""-""&RIGHT(YEAR(RC[-1])+1,2),"" ""&YEAR(RC[-1])-1&""-""&RIGHT(YEAR(RC[-1]),2))"
Selection.AutoFill Destination:=Range("E2:E" & lastrow)
'Selection.AutoFill Destination:=Range("E2:E"& lastrow)
Range("E2:E1344").Select
Only exception being are you sure your Autofill code is perfect...
This example shows you how to fill column B based on the the volume of data in Column A. Adjust "A1" accordingly to your needs. It will fill in column B based on the formula in B1.
Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, 1).Select
Range(Selection, Selection.End(xlUp)).Select
Selection.FillDown
ActiveCell.Offset(0, -1).Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, 1).Select
Range(Selection, Selection.End(xlUp)).Select
Selection.FillDown