Xldown Function - excel

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])"

Related

Select.Autofill Destination:=Range NOT autofilling

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

How to make a dynamic range in Excel, depending on the rows of data on another column

I am trying to make an Excel Macro that among other things autofills an array formula in a dynamic range let's say P2:P(x) where (x) is the number of rows that have data in another column, let's say O. I am using the COUNTA function but either that's not the appropriate approach, or my syntax is off, cause I keep getting a syntax error.
Below is the aforementioned code, any help would be greatly appreciated:
Range("E1").Select
ActiveCell.FormulaR1C1 = "custom id"
Range("E2").Select
Selection.FormulaArray = _
"=TEXTJOIN("""",TRUE,IFERROR((MID(RC[1],ROW(INDIRECT(""1:""&LEN(RC[1]))),1)*1),""""))"
ActiveWindow.ScrollColumn = 2
Range("P1").Select
ActiveCell.FormulaR1C1 = "amount"
Range("P2").Select
ActiveCell.FormulaR1C1 = "=NUMBERVALUE(RC[-1],"","",""."")"
Range("P3").Select
Range("P2").Select
Selection.AutoFill Destination:=Range("P2:P" & COUNTA(C[-1])), Type:=xlFillDefault
Range("P2:P" & COUNTA(C[-1])).Select
Range("E2").Select
Selection.AutoFill Destination:=Range("E2:E" & COUNTA(C[1])), Type:=xlFillDefault
Range("E2:E" & COUNTA(C[1]).Select
Use LastRow to find the last row of data in another column, in your case X, enter the formula into first column (E) and then autofill formula down to LastRow :
Repeat the below for the other columns.
Dim LastRow as Long
LastRow = Range("X" & Rows.Count).End(xlUp).Row
Range("E2").FormulaArray":"" 'Enter your formula between quotation marks, if not array then change to Range("E2").Formula = ""
Range("E2").AutoFill Range("E2:E" & LastRow)

Moved VBA function and macro to new workbook off network drive but now returning #Name? instead of proper value

I have an Excel macro that uses a function. Currently, the file is on a network drive. Data is pasted to the Excel file (new tab each time) and the macro is run with no problems (account number successfully pulled from larger data set).
In my effort to move the macro from a network drive to the cloud and run the macro from a separate Excel file (the file that contains the initial data dump i.e. no pasting data to file where macro lives), the portion of the macro that uses the function is now returning #NAME? instead of the proper numerical values. I am totally stumped as to why this is happening.
Literally any help at all is appreciated!
Function
Function onlynumbers(ByVal ref As String)
Dim rx As Object
Set rx = CreateObject("VBScript.RegExp")
With rx
.Pattern = "\D"
.Global = True
onlynumbers = .Replace(ref, "")
End With
End Function
Macro (see 'Fix Account Number' section)
Sub FORMAT()
'Remove extra rows on TOP
Rows("1:6").Select
Selection.Delete Shift:=xlUp
'Remove Extra Columns
Columns("A:H").Select
Selection.Delete Shift:=xlToLeft
Columns("B:O").Select
Selection.Delete Shift:=xlToLeft
Columns("C:J").Select
Selection.Delete Shift:=xlToLeft
'Extract Account Number from OBI Field
Range("C2").Select
ActiveCell.Formula = "=MID(B2,SEARCH(""79*"",B2),8)"
'Autofill Formula
Dim lastRow As Long
lastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("C2").AutoFill Destination:=Range("C2:C" & lastRow)
'Fix Account Number
Range("D2").Select
ActiveCell.Formula = "=onlynumbers(C2)"
Range("D2").AutoFill Destination:=Range("D2:D" & lastRow)
Range("E2").Select
ActiveCell.Formula = "=IF(LEN(D2)<6,"""",D2)"
Range("E2").AutoFill Destination:=Range("E2:E" & lastRow)
Range("E2:E" & lastRow).Select
Selection.Copy
Range("B2:B" & lastRow).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("C:D").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
'Column Headers
Range("A1:G1").Value = Array("Amount", "Account", "Transaction Type", "Description", "Cash Type", "Post Date", "Tax Year")
'Switch Amount and Account Columns
Columns("B:B").Select
Selection.Cut
Columns("A:A").Select
Selection.Insert Shift:=xlToRight
'Set Transaction Type
Range("C2").Value = "Deposit to Account - Wire received"
Range("C2").AutoFill Destination:=Range("C2:C" & lastRow)
'Set Description
Range("D2").Value = "$TranDesc$ of $CashAmount$"
Range("D2").AutoFill Destination:=Range("D2:D" & lastRow)
'Set Cash Type
Range("E2").Value = "Principal"
Range("E2").AutoFill Destination:=Range("E2:E" & lastRow)
'Set Post Date
Range("F2").Select
ActiveCell.Formula = "=TODAY()"
Range("F2").AutoFill Destination:=Range("F2:F" & lastRow)
'Set Tax Year
Range("G2").Select
ActiveCell.Formula = "=YEAR(TODAY())"
Range("G2").AutoFill Destination:=Range("G2:G" & lastRow)
'Final Formatting
Range("C1:G1").Select
Selection.Font.Bold = True
Columns("C:G").Select
Columns("C:G").EntireColumn.AutoFit
'Reset Selection
Range("A1").Select
End Sub
Did you also move your function, or just the macro?
If not, putting the function in the same place as the macro (just after End Sub on a new line) should fix things if you need the macro to refer to the function.
However, to use the function in a worksheet (which it looks like you are doing), the workbook containing the cell with the fixed account number needs to also contain the function. That way, the account number cell can use the function.
The error NAME is because the cell formula cannot find a defined name onlynumbers.
You could also try defining the function with the Name Manager instead of in VBA, but the best way might be to just change the formula in that cell to do what you want it to do using the built-in Excel functions. You could then update the macro to input this new formula at that line.

Why does this display the values in the row above instead of the formula specified if there is only one row of data?

This is a snippet from my macro and its supposed to input the formula specified from Row 4 down to the end of the dataset. However, when the dataset provided only has one row of data (data starts in row 4) instead of the formula, it just picks up the value in the cell one row above. IE: R4 will show R3 instead of "=IF(RC[-11]>RC[-10],(RC[-8]*RC[-11])-(RC[-8]*RC[-10]),0)"
Lastrow = Cells(Rows.Count, 2).End(xlUp).Row
Range("I4").Select
ActiveCell.FormulaR1C1 = "=IF(ISBLANK(RC[-5]),RC[14],RC[1])": Range("I4:I" & Lastrow).FillDown
Range("R4").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-11]>RC[-10],(RC[-8]*RC[-11])-(RC[-8]*RC[-10]),0)": Range("R4:R" & Lastrow).FillDown
Range("AA4").Select
ActiveCell.FormulaR1C1 = "=RC[-19]*RC[-18]": Range("AA4:AA" & Lastrow).FillDown
I came up with a work around, however I'm curious as to why this happened in the first place.
If Lastrow = 4 Then
Range("I4").Select
ActiveCell.FormulaR1C1 = "=IF(ISBLANK(RC[-5]),RC[14],RC[1])"
Range("R4").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-11]>RC[-10],(RC[-8]*RC[-11])-(RC[-8]*RC[-10]),0)"
Range("AA4").Select
ActiveCell.FormulaR1C1 = "=RC[-19]*RC[-18]"
Else
Range("I4").Select
ActiveCell.FormulaR1C1 = "=IF(ISBLANK(RC[-5]),RC[14],RC[1])": Range("I4:I" & Lastrow).FillDown
Range("R4").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-11]>RC[-10],(RC[-8]*RC[-11])-(RC[-8]*RC[-10]),0)": Range("R4:R" & Lastrow).FillDown
Range("AA4").Select
ActiveCell.FormulaR1C1 = "=RC[-19]*RC[-18]": Range("AA4:AA" & Lastrow).FillDown
End If

Macros to copy and paste in loop

For many of you this might be simple. I need to copy and paste data from 2 specific columns to a new spread sheet; but the macros need to stop when it reaches the end of data or when a cell is blank.
So far the code looks like this:
Workbooks.Add
ActiveCell.FormulaR1C1 = _
"='[Muhanad_Reset_import-TEMPLATE.xlsx]Reset_import-Raw Data'!R1C1"
Range("A1").Select
ActiveCell.FormulaR1C1 = _
"='[Muhanad_Reset_import-TEMPLATE.xlsx]Reset_import-Raw Data'!RC1"
Range("B1").Select
ActiveCell.FormulaR1C1 = _
"='[Muhanad_Reset_import-TEMPLATE.xlsx]Reset_import-Raw Data'!R1C3"
Range("B1").Select
ActiveCell.FormulaR1C1 = _
"='[Muhanad_Reset_import-TEMPLATE.xlsx]Reset_import-Raw Data'!RC3"
Range("A1:B1").Select
Selection.Copy
Range("A2:A101").Select
ActiveSheet.Paste
Range("A1").Select
Windows("Muhanad_Reset_import-TEMPLATE.xlsx").Activate
I know the code for copy and paste code does not work for a good result.
Do you mean code like this:
Sub Test()
Dim lr As Long
Dim i As Long
lr = ThisWorkbook.Worksheets("Reset_import-Raw Data").Cells(Rows.Count, 1).End(xlUp).Row
Workbooks.Add
With ActiveSheet.Range("A1:A" & lr)
.Formula = "='[Muhanad_Reset_import-TEMPLATE.xlsx]Reset_import-Raw Data'!$A1"
.Offset(, 1).Formula = "='[Muhanad_Reset_import-TEMPLATE.xlsx]Reset_import-Raw Data'!$C1"
End With
Windows("Muhanad_Reset_import-TEMPLATE.xlsm").Activate
End Sub

Resources