I have been working with VBA for about two years now, and I am having a problem I haven't had before. I am trying to autofill columns A, B, D, E and G with formulas based on the number of cells that are populated in column L. In the example shown below, cells L2-L5 are populated. With my autofill formulas, this should mean that cells A2-A5 populate, cells B2-B5 populate etc. But they don't. Only column G populates correctly, and I am stumped as to why this is happening. Any brilliant ideas?
Here is my code:
Application.DisplayAlerts = False
Sheets("QBTimecard").Select
Range("DP2").Select
ActiveCell.FormulaR1C1 = _
"=RC[-119]&""_""&RC[-107]&""_""&IF(RC[-2]<>""Client Transportation"",""Z"",""XY"")"
Range("DQ2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-13]<=RC[-8],""Pass"",""MMV"")"
On Error Resume Next
Selection.Autofill Destination:=Range("dp2:ds" & Cells(Rows.Count, "A").End(xlUp).Row)
Sheets("QBTimecard").Select
Columns("DP:DP").Select
Selection.Copy
Sheets("PunchEntryImport").Select
Columns("L:L").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("L1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "Blank"
Range("L2").Select
Sheets("PunchEntryImport").Select
Range("A2").Select
ActiveCell.FormulaR1C1 = _
"=INDEX(QBTimecard!C,MATCH(PunchEntryImport!RC[11],QBTimecard!C[119],0))"
Range("B2").Select
ActiveCell.FormulaR1C1 = _
"=SUMIF(QBTimecard!C[118],PunchEntryImport!RC[10],QBTimecard!C[13])/COUNTIF(QBTimecard!C[118],PunchEntryImport!RC[10])"
Range("D2").Select
ActiveCell.FormulaR1C1 = _
"=INDEX(Lookup!C[-2],MATCH(INDEX(QBTimecard!C[9],MATCH(PunchEntryImport!RC[8],QBTimecard!C[116],0)),Lookup!C[-3],0))&""/1/""&INDEX(QBTimecard!C[10],MATCH(PunchEntryImport!RC[8],QBTimecard!C[116],0))&"" ""&TEXT(INDEX(QBTimecard!C[12],MATCH(PunchEntryImport!RC[8],QBTimecard!C[116],0)),""HHMM"")"
Range("E2").Select
ActiveCell.FormulaR1C1 = _
"=INDEX(Lookup!C[-3],MATCH(INDEX(QBTimecard!C[8],MATCH(PunchEntryImport!RC[7],QBTimecard!C[115],0)),Lookup!C[-4],0))&""/1/""&INDEX(QBTimecard!C[9],MATCH(PunchEntryImport!RC[7],QBTimecard!C[115],0))&"" ""&TEXT(INDEX(QBTimecard!C[12],MATCH(PunchEntryImport!RC[7],QBTimecard!C[115],0)),""HHMM"")"
Range("G2").Select
ActiveCell.FormulaR1C1 = _
"=INDEX(QBTimecard!C[103],MATCH(PunchEntryImport!RC[5],QBTimecard!C[113],0))"
On Error Resume Next
'This is the portion that is not autofilling
Selection.Autofill Destination:=Range("a2:a" & Cells(Rows.Count, "L").End(xlUp).Row)
Selection.Autofill Destination:=Range("b2:b" & Cells(Rows.Count, "L").End(xlUp).Row)
Selection.Autofill Destination:=Range("d2:d" & Cells(Rows.Count, "L").End(xlUp).Row)
Selection.Autofill Destination:=Range("e2:e" & Cells(Rows.Count, "L").End(xlUp).Row)
Selection.Autofill Destination:=Range("g2:g" & Cells(Rows.Count, "L").End(xlUp).Row)
Without re-writing the whole thing, this is a more-approachable way to handle this, without the Select/ Activate/ Autofill:
Dim wsQBTC As Worksheet, wsPE As Worksheet, lr As Long
Set wsQBTC = Worksheets("QBTimecard")
Set wsPEI = Worksheets("PunchEntryImport")
lr = wsQBTC.Cells(Rows.Count, "A").End(xlUp).Row
With wsQBTC
.Range("DP2:DP" & lr).FormulaR1C1 = "=RC[-119]&""_""&RC[-107]&""_""&IF(RC[-2]<>""Client Transportation"",""Z"",""XY"")"
.Range("DQ2:DQ" & lr).FormulaR1C1 = "=IF(RC[-13]<=RC[-8],""Pass"",""MMV"")"
End With
From the help for Range.AutoFill
The destination must include the source range.
Because you are using Selection.AutoFill and G2 is the active cell, the autofill will only work when G2 is part of the range being autofilled.
To fix this, replace Selection.AutoFill with Range("A2").AutoFill, Range("B2").AutoFill and so on
Related
I recorded a macro to format some data, however the range changes on a bi-weekly basis. Hence, I am trying to incorporate the xldown function to select all of the data until the last row. However, I'm not sure how to incorporate this feature into my current VBA, any help would be greatly appreciated as I have had this function appear multiple times. Thank you!
Range("C2").Select
ActiveCell.FormulaR1C1 = "=RC[-1]"
Range("C3").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-2]=R[-1]C[-2],R[-1]C&"",""&RC[-1],RC[-1])"
Range("C3").Select
Selection.AutoFill Destination:=Range("C3:C161")
Range("C3:C161").Select
Range("D2").Select
ActiveCell.FormulaR1C1 = "=COUNTIF(R2C1:RC[-3],RC[-3])"
Range("D2").Select
Selection.AutoFill Destination:=Range("D2:D161")
Ran
Dim LastRow As Long
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
Range("C2").FormulaR1C1 = "=RC[-1]"
Range("C3:C" & LastRow).FormulaR1C1 = "=IF(RC[-2]=R[-1]C[-2],R[-1]C&"",""&RC[-1],RC[-1])"
Range("D2:D" & LastRow).FormulaR1C1 = "=COUNTIF(R2C1:RC[-3],RC[-3])"
I have following code
First it will insert two columns one by one
Then count rows in C column
Then Copy ActiveCell.FormulaR1C1 = "=R[-5]C[18]" to B7 to rows count
Similarly, Copy ActiveCell.FormulaR1C1 = "=R[-5]C[13]" to A7 to rows count
But I am getting error when the Active Row is only 01 (One), if it is more than one then it works ok.
I am struggling with this. If anyone can please help.
Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Dim LastRow As Long
LastRow = Range("C" & Rows.Count).End(xlUp).Row
Range("B7").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=R[-5]C[18]"
Range("B7").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("B7:B" & LastRow), Type:=xlFillCopy
Range("B7:B" & LastRow).Select
Range("A7").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=R[-5]C[13]"
Range("A7").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("A7:A" & LastRow), Type:=xlFillCopy
Range("A7:A" & LastRow).Select
To close the question out:
Instead of using AutoFill, maybe do Range("B7:B" & LastRow).Value = Range("B7").Value, and similarly for column A.
Or just in one line for both columns: Range("A7:B" & LastRow).Value = Range("A7:B7").Value.
First off, the Selection object is not necessarily the best option. You can insert the columns with Range("A:B").Insert xlShiftToRight.
Next, you can set the formula for an entire range at once rather than copying and pasting. Since you're putting the formulas only starting at row 7, you'll need to offset and resize the range that is defined by what is in Column C (after the insert).
The Resize method will need to reduce the row count by 6 since the formulas don't start until row 7:.Resize(LastRow - 6). Since the original range is only one column wide, the column count in the Resize method can be omitted (otherwise, it'd be .Resize(LastRow - 6, 1)).
The Offset method will need to shift down 6 rows and left 1 (or 2) columns: .Offset(6,-1) and .Offset(6,-2)).
Your code would then be greatly simplified to:
Dim LastRow As Long
ActiveSheet.Range("A:B").Insert xlShiftToRight
LastRow = Range("C" & Rows.Count).End(xlUp).Row
With Range("C:C").Resize(LastRow-6)
.Offset(6,-1).FormulaR1C1="=R[-5]C[18]"
.Offset(6,-2).FormulaR1C1="=R[-5]C[13]"
End With
Of course, even easier would be to convert your sheet to a Table ("ListObject" in VBA) and let Excel do the heavy lifting. But that's way outside what you asked.
I believe I'm having an issue with either appropriately identifying a fixed cell or order of operations. I've spent an hour an a half researching and can't find the answer. The issue is only with the Concatenation row: I can't get VBA to recognize the insertion of a fixed cell into the text of the formula (I can only get it R the cell). It's for a daily exported excel report from a database that inserts the date into C2. I'm concatenating the file names in column B with the folder location they'll be in at the end of the day, the day's date and the unique file group identifier in each matching cell in column C. I've replaced the text of the folder name with FOLDER for confidentiality purposes. I can concatenate and autofill it manually, but I'd rather just type the formula in once! Any assistance would be helpful.
Thanks! - John
Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C2").Select
ActiveCell.FormulaR1C1 = "=TEXT(RC[-1],""yyyy mm dd"")"
Range("C2").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveSheet.Paste
Application.CutCopyMode = False
Range("A5").Select
ActiveCell.FormulaR1C1 =
"=CONCATENATE(""FOLDER,("$C$2"), FOLDER"",RC[1])"
Dim lastRow As Long
lastRow = Range("B" & Rows.Count).End(xlUp).Row
Range("A5").Select
Selection.AutoFill Destination:=Range("A5:A" & lastRow), Type:=xlFillDefault
Range("A5:A" & lastRow).Select
Selection.Copy
Range("B5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Rows("1:2").Select
Selection.Delete Shift:=xlUp
Range("B3").Select
I don't know if this will work since the formula is expecting references via the RC notation, but you could try:
ActiveCell.FormulaR1C1 =
"=CONCATENATE(""FOLDER,(" & "$C$2" & "), FOLDER"",RC[1])"
As an alternative to inserting a fixed cell, an option would be to assign that cell's value to a string variable and than insert that.
Dim dateVal as string
dateVal = Range("C2").Value2
Range("A5").FormulaR1C1 =
"=CONCATENATE(""FOLDER,(" & dateVal & "), FOLDER"",RC[1])"
I need to use a vlookup inside vba but the worksheetname constantly changes but can always be referenced as ActiveWorkbook.Worksheet(1) and ActiveWorkbook.Worksheet(2). So I used dim so all selection can refer to ws1 and ws2 but of course inside a vlookup formula that kind of script doesn't work. Hope anyone can help rewrite those vlookup formula lines.
It regards the last 5 lines with the ActiveCell.FormulaR1C1 lines where i need a solution for 'ws2'!
Thanks for the help.
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = ActiveWorkbook.Worksheets(1)
Set ws2 = ActiveWorkbook.Worksheets(2)
ws1.Select
Selection.AutoFilter
Range("G2").Select
ActiveCell.FormulaR1C1 = "Web sales"
Range("H2").Select
ActiveCell.FormulaR1C1 = "Web stock"
Range("I2").Select
ActiveCell.FormulaR1C1 = "Total Sales"
Range("J2").Select
ActiveCell.FormulaR1C1 = "Total Stock"
Range("F2:F71").Select
Selection.Copy
ActiveWindow.SmallScroll Down:=-102
Range("G2:J71").Select
ActiveWindow.SmallScroll Down:=-66
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Columns("H:H").EntireColumn.AutoFit
Range("I5").Select
Application.CutCopyMode = False
Range("D3:D150").Select
ws2.Select
Range("D3:D150").Select
ws1.Select
ActiveWindow.SmallScroll Down:=-66
Range("G3").Select
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[-3],'ws2'!RC[-3]:R[42]C[-1],3,0)"
ActiveWindow.SmallScroll Down:=-30
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[-3],ws2!R3C4:R45C6,3,0)"
Below are two examples from your original code showing how to include the name of worksheet into formula:
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-3]," & ws2.Name & "!RC[-3]:R[42]C[-1],3,0)"
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-3]," & ws2.Name & "!R3C4:R45C6,3,0)"
I am a bit new to the macro's in excel and I am trying to find a way to adjust one of the macros I currently have in an excel file. I have a calculation that takes the columns D and E then subtracts D from E and adds it to the value of column B. here is the current code and also the sheet being used.
Sub InvAdj()
'
' InvAdj Macro
'
' Keyboard Shortcut: Ctrl+Shift+I
'
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "Quality"
Range("C2").Select
ActiveCell.FormulaR1C1 = "=RC[-1]-RC[2]+RC[3]"
Range("C2").Select
Selection.AutoFill Destination:=Range("C2:C33")
Range("C2:C33").Select
Columns("C:C").Select
Selection.Copy
Columns("B:B").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=True, Transpose:=False
Columns("C:C").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Range("D2:E33").Select
Selection.ClearContents
Range("F1").Select
End Sub
Not sure if this is what you are trying?
Sub InvAdj()
Dim ws As Worksheet
'~~> Change this to the relevant sheet
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
.Range("B1").Value = "Quality"
For i = 2 To 33
'~~> Check if all cells have data
If Len(Trim(.Range("B" & i).Value)) <> 0 And _
en(Trim(.Range("D" & i).Value)) <> 0 And _
en(Trim(.Range("E" & i).Value)) <> 0 Then
'B = B + (E - D)
.Range("B" & i).Value = .Range("B" & i).Value + _
(.Range("E" & i).Value - .Range("D" & i).Value)
End If
Next i
End With
End Sub