I'm pretty new to VBA and I've been trying to use a macro to paste a few formulas in cells. Problem is, everytime I do so, I get a 1004 error on the very first formula.
I strongly suspect it's because of quotes but I can't for the life of me understand what goes wrong. Would you guys have any idea ?
Sub PREF()
Dim lastrow As Long
lastrow = Range("'Extract WIN'!A" & Rows.Count).End(xlUp).Row
Dim P2 As Worksheet
Set P2 = Sheets("PRO")
Select Case MsgBox("Do you want to proceed with" & P2.[C2].Value & " ?", vbYesNo, "as datepref")
Case vbYes
Sheets("Extract WIN").Select
Range("W2" & ":" & "W" & lastrow).FormulaR1C1 = "=IFERROR(DATEVALUE(CONCATENATE(MID(RC[-10];7;2);""/"";MID(RC[-10];5;2);""/"";MID(RC[-10];1;4)));TEXT(,))"
Range("Y2" & ":" & "Y" & lastrow).FormulaR1C1 = "=IFERROR(DATEVALUE(CONCATENATE(MID(RC[-10];7;2);""/"";MID(RC[-10];5;2);""/"";MID(RC[-10];1;4)));TEXT(,))"
Range("AA2" & ":" & "AA" & lastrow).FormulaR1C1 = "=IFERROR(IF(AND(Provision!R2C3-RC[-4]<366;RC[-18]>0);RC[-18];0);TEXT(,))"
Range("AB2" & ":" & "AB" & lastrow).FormulaR1C1 = "=IFERROR(RC[-1]*RC[-18];TEXT(,))"
Range("AC2" & ":" & "AC" & lastrow).FormulaR1C1 = "=IF(AND(RC[-2]=0;RC[-20]>0;RC[-4]>Provision!R2C6;ISNA(VLOOKUP(RIGHT(TEXT(RC[-25];""000#####"");4);Provision!R7C17:R101C18;1;FAUX))=FAUX);1;0)"
Range("AD2" & ":" & "AD" & lastrow).FormulaR1C1 = "=RC[-1]*RC[-20]"
Range("AE2" & ":" & "AE" & lastrow).FormulaR1C1 = "=IFERROR(RC[-22]-RC[-4]-RC[-2];TEXT(,))"
Range("AF2" & ":" & "AF" & lastrow).FormulaR1C1 = "=IF(AND(RC[-20]>0;RC[-1]>0);ROUND(MIN(RC[-20]*12;RC[-1]);0);0)"
Range("AG2" & ":" & "AG" & lastrow).FormulaR1C1 = "=RC[-1]*RC[-23]"
Range("AH2" & ":" & "AH" & lastrow).FormulaR1C1 = "=RC[-2]-RC[-18]"
Range("AI2" & ":" & "AI" & lastrow).FormulaR1C1 = "=IFERROR(RC[-4]-RC[-3];TEXT(,))"
Range("AJ2" & ":" & "AJ" & lastrow).FormulaR1C1 = "=IF(RC[-24]>0;ROUND(MIN(RC[-24]*12;RC[-1]);0);0)"
Range("AK2" & ":" & "AK" & lastrow).FormulaR1C1 = "=RC[-1]*RC[-27]"
Range("AL2" & ":" & "AL" & lastrow).FormulaR1C1 = "=RC[-2]-RC[-21]"
Range("AM2" & ":" & "AM" & lastrow).FormulaR1C1 = "=IFERROR(RC[-4]-RC[-3];TEXT(,))"
Range("AN2" & ":" & "AN" & lastrow).FormulaR1C1 = "=IF(AND(RC[-16]>Provision!R2C7;RC[-28]>=0);RC[-1];0)"
Range("AO2" & ":" & "AO" & lastrow).FormulaR1C1 = "=IFERROR(RC[-1]*RC[-31];TEXT(,))"
Range("AP2" & ":" & "AP" & lastrow).FormulaR1C1 = "=IF(RC[-18]=TEXT(,);0;IF(AND(X2<Provision!R2C7;RC[-3]>0);RC[-3];0))"
Range("AQ2" & ":" & "AQ" & lastrow).FormulaR1C1 = "=RC[-1]*RC[-33]"
Range("AR2" & ":" & "AR" & lastrow).FormulaR1C1 = "=IF(RC[-20]="""";RC[-5];0)"
Range("AS2" & ":" & "AS" & lastrow).FormulaR1C1 = "=IFERROR(RC[-1]*RC[-35];TEXT(,))"
Range("AT2" & ":" & "AT" & lastrow).FormulaR1C1 = "=IFERROR(RC[-6]+RC[-4]+RC[-2];TEXT(,))"
Range("AU2" & ":" & "AU" & lastrow).FormulaR1C1 = "=IFERROR(RC[-6]+RC[-4]+RC[-2];TEXT(,))"
Range("AV2" & ":" & "AV" & lastrow).FormulaR1C1 = "=IFERROR(RC[-2]-RC[-30];TEXT(,))"
Range("AX2" & ":" & "AX" & lastrow).FormulaR1C1 = "=IFERROR(RC[-13]*0,5;TEXT(,))"
Range("AY2" & ":" & "AY" & lastrow).FormulaR1C1 = "=IFERROR(RC[-4]*0,9;TEXT(,))"
Range("AZ2" & ":" & "AZ" & lastrow).FormulaR1C1 = "=IFERROR(RC[-2]+RC[-1];TEXT(,))"
Range("BA2" & ":" & "BA" & lastrow).FormulaR1C1 = "=IFERROR(RANK(RC[-1];RC:RC;0);TEXT(,))"
Columns("AA:AZ").NumberFormat = "#,##0"
Columns("W:BA").EntireColumn.AutoFit
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.AutoFilter
Range("AI2").Select
Sheets("PRO").Select
Case vbNo
P2.[C2].Select
End Select
End Sub
I'm sorry for the wall of code but I have no idea where I did something wrong, and was advised to give you guys full context
You need to replace the ";" with regular ",".
For example, take this line of code:
Range("W2:" & "W" & LastRow).FormulaR1C1 = "=IFERROR(DATEVALUE(CONCATENATE(MID(RC[-10];7;2);""/"";MID(RC[-10];5;2);""/"";MID(RC[-10];1;4)));TEXT(,))"
And change it to this:
Range(Cells(2, 1), Cells(2, LastRow)).FormulaR1C1 = "=IFERROR(DATEVALUE(CONCATENATE(MID(RC[-10],7,2),""/"",MID(RC[-10],5,2),""/"",MID(RC[-10],1,4))),TEXT(,))"
Also, keep in mind I made a few changes to this line. I changed it so that it is using Cells instead of range. I find Cells are easier to work with and provide better readability because we don't have to join any strings.
Either way is fine, it just boils down to preference.
Related
all:
I feel like this is something painfully obvious but I have been banging my head up against the wall for a while and can't seem to find a solution.
When entering the below formula into "M3"
=IFERROR(IF(ISBLANK(VLOOKUP(A3&"|"&E3,CHOOSE({1,2},OLD_PR!$A$3:$A$374&"|"&OLD_PR!$E$3:$E$374,OLD_PR!$O$3:$O$374),2,FALSE)),"",VLOOKUP(A3&"|"&E3,CHOOSE({1,2},OLD_PR!$A$3:$A$374&"|"&OLD_PR!$E$3:$E$374,OLD_PR!$O$3:$O$374),2,FALSE)),"")
with this code (ActiveCell = "M3")
ActiveCell.FormulaArray = _
"=IFERROR(IF(ISBLANK(VLOOKUP(A3&" & Chr(34) & "|" & Chr(34) & "&E3,CHOOSE({1,2},OLD_PR!$A$3:$A$" & OldPRLastRow & "&" & Chr(34) & "|" & Chr(34) & "&OLD_PR!$E$3:$E$" & OldPRLastRow & ",OLD_PR!$O$3:$O$" & OldPRLastRow & "),2,FALSE)),""""," _
& "VLOOKUP(A3&" & Chr(34) & "|" & Chr(34) & "&E3,CHOOSE({1,2},OLD_PR!$A$3:$A$" & OldPRLastRow & "&" & Chr(34) & "|" & Chr(34) & "&OLD_PR!$E$3:$E$" & OldPRLastRow & ",OLD_PR!$O$3:$O$" & OldPRLastRow & "),2,FALSE))," _
& Chr(34) & Chr(34) & ")"
I am getting the Run-time error in the title.
When I manually enter the formula, it works.
The formula is less than 255 characters without the replace work around, so I'm not making any mistakes there
I'm only entering the formula into a single cell
The above are the most common mistakes I found via google searches, but do not apply to me. I'm sure this is something silly.
Any insight would be greatly appreciated!
With reference to this try
ActiveCell.FormulaArray = _
"=IFERROR(IF(ISBLANK(VLOOKUP(D3&" & Chr(34) & "|" & Chr(34) & "&E3,""ChoosePart"",2,FALSE)),""""," _
& "VLOOKUP(D3&" & Chr(34) & "|" & Chr(34) & "&E3,""ChoosePart"",2,FALSE))," _
& Chr(34) & Chr(34) & ")"
ActiveCell.Replace """ChoosePart""", "CHOOSE({1,2},OLD_PR!$A$3:$A$" & OldPRLastRow & "&" & Chr(34) & "|" & Chr(34) & "&OLD_PR!$E$3:$E$" & OldPRLastRow & ",OLD_PR!$O$3:$O$" & OldPRLastRow & ")"
Since Column O contains text values, here's an alternative...
ActiveCell.FormulaArray = "=LOOKUP(REPT(""z"",255),CHOOSE({1,2},"""",INDEX(OLD_PR!$O$3:$O$" & OldPRLastRow & ",MATCH(A3&""|""&E3,OLD_PR!$A$3:$A$" & OldPRLastRow & "&""|""&OLD_PR!$E$3:$E$" & OldPRLastRow & ",0))))"
My variable found_match will return "A7:A45" when I use debug.print, this range can change depending on other factors, but currently it returns this.
I have a Match function set up which currently works if I type in "A7:A45" as it's range, but if I replace it with the variable it doesn't work.
row_num3 = Evaluate("MATCH(1,('" & ws1.Name & "'!A7:A45=""" & condition_1 & """)*('" & ws1.Name & "'!B7:B45=""" & condition_2 & """)*('" & ws1.Name & "'!F7:F45=""" & condition_3 & """),0)")
Debug.Print row_num3
The above works, but if I replace the range with the variable it returns an error when I use debug.print:
row_num3 = Evaluate("MATCH(1,('" & ws1.Name & "'found_match=""" & condition_1 & """)*('" & ws1.Name & "'found_match2=""" & condition_2 & """)*('" & ws1.Name & "'found_match3=""" & condition_3 & """),0)")
Debug.Print row_num3
This code is running on ws3 (worksheet 3), but it's looking for it's match on ws1 (worksheet)
How can I format the Match function to work with variables as the range rather than specifically typing the range (eg. A7:A45)
Thanks!
Try:
row_num3 = Evaluate("MATCH(1,('" & WS1.Name & "'!" & found_match & "=""" & condition_1 & """)*('" & WS1.Name & "'!" & found_match2 & "=""" & condition_2 & """)*('" & WS1.Name & "'!" & found_match3 & "=""" & condition_3 & """),0)")
Debug.Print row_num3
My final goal is to print my cells pipe delimited so in order to do so I am trying to print everything on each row into cell AB on each row. I am trying to loop through each row to do so however I am currently getting the top row of code repeated in all my rows instead of each row individually being printed.
Sub print_misc()
Dim cell As Range
Dim lastRow As Long
Sheets("1099-Misc_Form_Template").Select
lastRow = Range("B" & Rows.Count).End(xlUp).row
For Each cell In Range("AB2:" & "AB" & lastRow)
cell.Value = Range("B2") & "|" & Range("C2") & "|" & Range("D2") & "|" & Range("E2") & "|" & Range("F2") & "|" & Range("G2") & "|" & Range("H2") & "|" & Range("I2") & "|" & Range("J2") & "|" & Range("L2") & "|" & Range("M2") & "|" & Range("N2") & "|" & Range("O2") & "|" & Range("P2") & "|" & Range("Q2") & "|" & Range("R2") & "|" & Range("S2") & "|" & Range("U2") & "|" & Range("V2") & "|" & Range("W2") & "|" & Range("X2") & "|" & Range("Y2") & "|" & Range("Z2") & "|" & Range("AA2")
Next
End Sub
Each cell in AB shows the result of the combined cells in that row (pipe delimited).
Current output:
Expected output:
You aren't incrementing the value of the row for each iteration of cell. You are point at row 2 for each one.
You also shouldn't use Select it is unnecessary just directly reference the sheet object.
Sub print_misc()
Dim cell As Range
Dim lastRow As Long
dim iter as long
with Sheets("1099-Misc_Form_Template")
lastRow = .Range("B" & Rows.Count).End(xlUp).row
iter = 2
For Each cell In .Range("AB2:" & "AB" & lastRow)
cell.Value = .Range("B" & iter) & "|" & .Range("C" & iter) & "|" & _
.Range("D" & iter) & "|" & .Range("E" & iter) & "|" & _
.Range("F" & iter) & "|" & .Range("G" & iter) & "|" & _
.Range("H" & iter) & "|" & .Range("I" & iter) & "|" & _
.Range("J" & iter) & "|" & .Range("L" & iter) & "|" & _
.Range("M" & iter) & "|" & .Range("N" & iter) & "|" & _
.Range("O" & iter) & "|" & .Range("P" & iter) & "|" & _
.Range("Q" & iter) & "|" & .Range("R" & iter) & "|" & _
.Range("S" & iter) & "|" & .Range("U" & iter) & "|" & _
.Range("V" & iter) & "|" & .Range("W" & iter) & "|" & _
.Range("X" & iter) & "|" & .Range("Y" & iter) & "|" & _
.Range("Z" & iter) & "|" & .Range("AA" & iter)
iter = iter + 1
Next
end with
End Sub
I have a sumif statement that uses 11 criterias. It works with all criterias that have text in them, but it fails to work when I add a field that has blanks and/or numbers in it. I can't figure out how to overcome this problem and would like some help. Here is my code:
Dim GS As Worksheet
Set GS = Worksheets("Grant Spreadsheet")
Dim LastRow As Long
With GS
LastRow = GS.Range("W1000").End(xlUp).Row
Me.TGATxtBx.Value = WorksheetFunction.SumIfs(.Range("w2:w" & LastRow),_
.Range("e2:e" & LastRow), "*" & Me.GrantNameCmboBx.Value & "*",_
.Range("n2:n" & LastRow), "*" & Me.GrantAgencyCmboBx.Value & "*",_
.Range("o2:o" & LastRow), "*" & Me.GrantDeptCmboBx.Value & "*",_
.Range("p2:p" & LastRow), "*" & Me.ProgramCmboBx.Value & "*",_
.Range("d2:d" & LastRow), "*" & Me.ProjTypeCmboBx.Value & "*",_
.Range("r2:r" & LastRow), "*" & Me.CatalogNoCmboBx.Value & "*",_
.Range("q2:q" & LastRow), "*" & Me.PassthroughCmboBx.Value & "*",_
.Range("s2:s" & LastRow), "*" & Me.GrantIDCmboBx.Value & "*",_
.Range("h2:h" & LastRow), "*" & Me.FundNoCmboBx.Value & "*"_
.Range("i2:i" & LastRow), "*" & Me.OrgTxtBx.Value & "*",_
.Range("k2:k" & LastRow), "*" & Me.AccountTxtBx.Value & "*")
Me.PassthroughCmboBx.Value has blanks and the three fields after that have numbers. If I move things around and bring these fields in earlier between the criterias that do work, it fails. I could really use your help.
I have a VBA function that returns a lot of information about the computer it's running on. I'd like to format the output neatly, with the titles left-justified and the results all tabbed on a neat line to the right.
Here's what I thought would work, and this is the solution on many sites I've consulted:
AllInfo = _
"Date/Time Opened:" & Space(33 - Len("Date/Time Opened:")) & Now() & vbCrLf & _
"Filename:" & Space(33 - Len("Filename:")) & Application.ActiveWorkbook.Name & vbCrLf & _
"Name in Cell B2:" & Space(33 - Len("Name in Cell B2:")) & Cells(2, 2).Value & vbCrLf & _
But the output, in both a message box and the body of an email, is jagged. The titles are all lined up on the left, of course, but the results aren't lined up nicely.
Can anyone see what I'm doing wrong?
This works:
Sub djfsdf()
allinfo = _
"Date/Time Opened:" & Space(33 - Len("Date/Time Opened:")) & Now() & vbCrLf & _
"Filename:" & Space(33 - Len("Filename:")) & Application.ActiveWorkbook.Name & vbCrLf & _
"Name in Cell B2:" & Space(33 - Len("Name in Cell B2:")) & Cells(2, 2).Value
Cells(3, 3) = allinfo
End Sub
with the proper font:
You are not including the string vars in the calculation for the number of spaces to add.
AllInfo = _
"Date/Time Opened:" & Space(33 - Len("Date/Time Opened:" & Now())) & Now() & vbLf & _
"Filename:" & Space(33 - Len("Filename:" & Application.ActiveWorkbook.Name)) & Application.ActiveWorkbook.Name & vbLf & _
"Name in Cell B2:" & Space(33 - Len("Name in Cell B2:" & Cells(2, 2).Value)) & Cells(2, 2).Value & vbLf & _
(etc)
I've also change your vbCRLF to just vbLF; that may or may not be correct.