I have the code already set up to return the row number of a row that contains "Car" in column A, "Fiat" in column B, and "Yellow" in column G.
Set ws1 = Sheets("Database")
Set ws2 = Sheets("Insert")
Set ws3 = Sheets("Sheet1")
1_cell = "Car"
2_cell = "Fiat"
3_cell = "Yellow"
row_num2 = Evaluate("MATCH(1,('" & ws1.Name & "'!A:A=""" & 1_cell & """)*('" & ws1.Name & "'!B:B=""" & 2_cell & """)*('" & ws1.Name & "'!G:G=""" & 3_cell & """),0)")
But I'm trying to figure out how I can make it so it looks for a row that contains "Car", "Fiat" and in column G it either has "Yellow" OR "Green".
I don't want it to only produce matches that contain "Yellow", I want it to be able to match if it has "Green" instead in the same column.
Any ideas? Thanks for the help!
Untested. But should do the job assuming your code was working
1_cell = "Car"
2_cell = "Fiat"
3_cell = "Yellow"
4_cell = "Green"
row_num_1 = Evaluate("MATCH(1,('" & ws1.Name & "'!A:A=""" & 1_cell & """)*('" & ws1.Name & "'!B:B=""" & 2_cell & """)*('" & ws1.Name & "'!G:G=""" & 3_cell & """),0)")
row_num_2 = Evaluate("MATCH(1,('" & ws1.Name & "'!A:A=""" & 1_cell & """)*('" & ws1.Name & "'!B:B=""" & 2_cell & """)*('" & ws1.Name & "'!G:G=""" & 4_cell & """),0)")
row_num2 = WorksheetFunction.Min(row_num_1, row_num_2)
Related
I currently have this code that works to insert full rows. However, I was hoping to limit the inserting of the row to columns A:J.
Bonus points to see who can help me figure out that if I delete column C why my current code doesn't work?
I tried to add .resize(1,10) but for some reason I keep getting errors, maybe the location in which I try to add this function. Any guidance/help is appreciated as always!
Here is my current code:
VBA Code:
Sub Add_Job()
Dim act As Worksheet
Set act = ThisWorkbook.ActiveSheet
bot_row = act.Range("Z1")
act.Rows(bot_row & ":" & bot_row + (5)).Insert Shift:=x1ShiftDown
act.Range("A3:J8").Copy
act.Range("A" & bot_row & ":J" & bot_row + (5)).PasteSpecial xlPasteFormats
act.Range("A" & bot_row & ":J" & bot_row + (5)).PasteSpecial xlPasteFormulas
Range("B" & bot_row & ":B" & bot_row + (5)).ClearContents
Application.CutCopyMode = False
End Sub
Edit: Updated Code that works now. However, I still don't understand if I delete column C, why it throws an error?
Dim act As Worksheet
Set act = ThisWorkbook.ActiveSheet
bot_row = act.Range("Z1")
act.Range("A" & bot_row & ":J" & bot_row + (5)).Insert Shift:=xlShiftDown
act.Range("A3:J8").Copy
act.Range("A" & bot_row & ":J" & bot_row + (5)).PasteSpecial xlPasteFormats
act.Range("A" & bot_row & ":J" & bot_row + (5)).PasteSpecial xlPasteFormulas
Range("B" & bot_row & ":B" & bot_row + (5)).ClearContents
Application.CutCopyMode = False
I have a workbook in which I track stats on projects. I have a function attached to a button that copies the 'Template' worksheet and gives it a name I enter.
I have a worksheet within the same workbook named 'Statistics', where I track totals and averages of certain cells within all the worksheets for each project.
No problem adding the new worksheet name to the formulas which only add values together.
However, I don't know how to format something which will add the new worksheet name (and cell name) to the AVERAGE() function.
Here is the Macro so far:
Sub btnAdd_Click()
Dim strFormula As String
strBookName = ""
frmAddBook.Show
If Len(strBookName) <> 0 Then
'Add the sheet...
ActiveWorkbook.Sheets("Template").Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = strBookName
ActiveSheet.Cells(1, 2) = strBookName
ActiveSheet.Cells(1, 1).Activate
'Modify the statisics totals to include those from the new sheet
With Sheets("Statistics")
'These are only adding to a formula which totals the cell values from each worksheet and this works
.Cells(6, 4).Formula = .Cells(6, 4).Formula & " + " & Chr(39) & strBookName & Chr(39) & "!I4"
.Cells(7, 4).Formula = .Cells(7, 4).Formula & " + " & Chr(39) & strBookName & Chr(39) & "!I3"
.Cells(8, 4).Formula = .Cells(8, 4).Formula & " + " & Chr(39) & strBookName & Chr(39) & "!B5"
.Cells(9, 4).Formula = .Cells(9, 4).Formula & " + " & Chr(39) & strBookName & Chr(39) & "!B4"
.Cells(13, 4).Formula = .Cells(13, 4).Formula & " + " & Chr(39) & strBookName & Chr(39) & "!B7"
'THESE ARE THE CELLS THAT I WANT TO USE THE AVERAGE FUNCTION AND APPEND WITH EACH NEW WORKSHEET NAME
.Cells(5, 10).Formula = .Cells(5, 10).Formula & " + " & Chr(39) & strBookName & Chr(39) & "!L1"
.Cells(6, 10).Formula = .Cells(6, 10).Formula & " + " & Chr(39) & strBookName & Chr(39) & "!L5"
.Cells(7, 10).Formula = .Cells(7, 10).Formula & " + " & Chr(39) & strBookName & Chr(39) & "!L6"
End With
End If
End Sub
I'd really appreciate any help. Thanks!
Might be easier to instead have an "updateStats" method which builds the formulas from scratch, using any worksheet in the workbook not named "Template" or "Statistics": that way you can also delete a project and refresh the formulas.
Eg: call this after adding or removing a worksheet
Sub UpdateStats()
Dim ws As Worksheet, nm, arrSkip, frm, sep
arrSkip = Array("Template", "Statistics") 'add other sheet here
frm = ""
sep = ""
For Each ws In ThisWorkbook.Worksheets
nm = ws.Name
'check name before processing
If IsError(Application.Match(nm, arrSkip, 0)) Then
frm = frm & sep & "'" & nm & "'!<C>"
sep = ","
End If
Next ws
With ThisWorkbook.Sheets("Statistics")
If Len(frm) > 0 Then 'any sheets to summarize?
.Cells(6, 4).Formula = "=SUM(" & Replace(frm, "<C>", "I4") & ")"
.Cells(7, 4).Formula = "=SUM(" & Replace(frm, "<C>", "I3") & ")"
.Cells(8, 4).Formula = "=SUM(" & Replace(frm, "<C>", "B5") & ")"
.Cells(9, 4).Formula = "=SUM(" & Replace(frm, "<C>", "B4") & ")"
.Cells(5, 10).Formula = "=AVERAGE(" & Replace(frm, "<C>", "L1") & ")"
.Cells(6, 10).Formula = "=AVERAGE(" & Replace(frm, "<C>", "L5") & ")"
.Cells(7, 10).Formula = "=AVERAGE(" & Replace(frm, "<C>", "L6") & ")"
Else
.Cells(6, 4).Resize(4, 1).Clear
.Cells(5, 10).Resize(3, 1).Clear
End If
End With
End Sub
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'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.