I am somewhat of a noob when it comes to VBA programming. I am running a macro script daily on each day's distinct tab title MM-DD-YY. I want to do a vlookup on today's date using the lookup range from the prior business day. For instance, if today is 07/31/20 I want the vlookup to be VLOOKUP(Q7, '07-30-20'!$P$1:$Q$29, 2, FALSE). How do you code this to automatically populate the vlookup using the prior business day? Below is the noob code I have at this point.
Range("Q2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(Q7, '07-29-20'!$P$1:$Q$29, 2, FALSE)"
Selection.AutoFill Destination:=Range("Q2:Q" & Range("E" & Rows.Count).End(xlUp).Row)
Try:
ActiveCell.FormulaR1C1 = "=VLOOKUP(Q7,'" & Right("0" & Month(Date), 2) & "-" & Right("0" & Day(Date), 2) - 1 & "-" & Right(Year(Date), 2) & "'!$P$1:$Q$29, 2, FALSE)"
You could do something like:
Dim dprevDate As Date, sprevDate As String
dprevDate = WorksheetFunction.WorkDay(Date, -1)
sprevDate = Format(dprevDate, "mm-dd-yy")
ActiveCell.Formula = "=VLOOKUP(Q7, '" & sprevDate & "'!$P$1:$Q$29, 2, FALSE)"
Related
I was hoping you could help me with a coding issue. I have quite a long concatenated formula that is generated via a userform in VBA. The only problem that I have with it is the Textbox6. The value entered by the user should be only 1 or 0 (that’s not the issue). The issue is when the formula is generated, the result relating to Textbox6 is “1=1” or “0=0” followed the rest of the formula. I would like the formula to identify if the Textbox6 has either a 1 or 0 and then apply the condition assigned to that value (something like: Textbox6.Value = 1 Then …). I just can’t seem to get this to work. Any help would be much appreciated.
Thanks.
ActiveCell.Formula = "=""BOX ""&IF(OR(Base_Point=1,Base_Point=2,Base_Point=3,Base_Point=4),X_Origin+" & TextBox1 & ",X_Origin-" & TextBox1 & ")&"",""&IF(OR(Base_Point=3,Base_Point=4,Base_Point=5,Base_Point=6),Y_Origin-" & TextBox2 & ",Y_Origin+" & TextBox2 & ")& "",""&IF(" & TextBox6 & "=1,Z_End,Z_Origin)&"" ""&IF(OR(Base_Point=1,Base_Point=2,Base_Point=3,Base_Point=4),X_Origin+" & TextBox3 & ",X_Origin-" & TextBox3 & ")&"",""&IF(OR(Base_Point=3,Base_Point=4,Base_Point=5,Base_Point=6),Y_Origin-" & TextBox4 & ",Y_Origin+" & TextBox4 & ")&"",""&IF(AND(" & TextBox6 & "=1,OR(Base_Point=1,Base_Point=3,Base_Point=5,Base_Point=7)),Z_End-" & TextBox5 & ",IF(AND(" & TextBox6 & "=0,OR(Base_Point=1,Base_Point=3,Base_Point=5,Base_Point=7)),Z_Origin+" & TextBox5 & ",IF(AND(" & TextBox6 & "=1,OR(Base_Point=2,Base_Point=4,Base_Point=6,Base_Point=8)),Z_End+" & TextBox5 & ",IF(AND(" & TextBox6 & "=0,OR(Base_Point=2,Base_Point=4,Base_Point=6,Base_Point=8)),Z_Origin-" & TextBox5 & "))))"
According to you comment, does this solution work for you (just replace the TextBox6 related part of your code)?
IIf(TextBox6.Value = 1, "1", "0")
You can also evaluate the "0" scenario using nested IIf function:
IIf(TextBox6.Value = 1, "1", IIf(TextBox6.Value = 0, "0", "NOT_ALLOWED_VALUE"))
How to use IIf function
I'm trying to run the below code and it gives me
Run-Time Error "1004"
Application-defined or Object-defined error
Every Single Time!!
Attached is a snippet of the code, any suggestions what's wrong? (The numbers in the Range(Cells( * ) sections are actually mostly variables in my overall macro, it's pretty complex but I've taken them out for simplicity here)
Code:
'Declare variables
Dim CriteriaRng As String
Dim SumRng As String
Dim Criteria As String
'Set a variable for each of the 3 parts of the SUMIF Formula
CriteriaRng = "'" & Sheets(1).Name & "'!" & Range(Cells(2, 4), Cells(88, 4)).Address
SumRng = "'" & Sheets(1).Name & "'!" & Range(Cells(2, 3), Cells(88, 3)).Address
Criteria = Chr(34) & "=" & Chr(34) & " & RC[-1]"
'Here goes the SUMIF Formula
With Sheets(2).Range(Cells(4, 13), Cells(9, 13))
Debug.Print "So the Whole Formula Should be:" & Chr(13) & "= SUMIF(" & CriteriaRng & ", " & Criteria & ", " & SumRng & ")"
'That was a vain attempt to find out what was wrong with the formula; didn't work though.
.FormulaR1C1 = "= SUMIF(" & CriteriaRng & ", " & Criteria & "," & SumRng & ")"
'Then adds NumberFormat and stuff here, but that isn't relevant to this question.
End With
The error always hits on the line where it's putting in the .FormulaR1C1 = .
Yes, I know I could get the same result using a nested loop, but that would return just the value without the SUMIF formula - I need that formula so the sheet updates when edited (without needing a macro - I'm sending the sheet on to other people who won't have or want any macros, but might need to edit the data).
Can anyone point out to me what is wrong? I'm prepared for it to be something pretty basic - only last week I spent 2 hours figuring out a problem from misspelling 'Columns' !!!
Any and all advice welcome - Many Thanks in advance.
I have the following formula in excel (created by me in the past),
=IFERROR(INDEX(Tasks!$H$2:$H$65536;SMALL(IF(A2=Tasks!$A$2:$A$65536;ROW(Tasks!$A$2:$A$65536)-ROW(Tasks!$A$2)+1);ROW($1:$1)));"")
The formula is working as I wanted, but in each and every month the a columns are changing, so for example what was Column "A" here can be Column "C" next month and so the number of rows are changing. I'm trying to modify the formula that I don't have to adjust month by month.
For this first I found a VBA script online:
Function Col_Letter(lngCol As Long) As String
Dim vArr
vArr = Split(Cells(1, lngCol).Address(True, False), "$")
Col_Letter = vArr(0)
End Function
So after if I type for example:
=Col_Letter(COLUMN(Table8[[#Headers];[ID]])) it will give me Letter "B" as this header can be found in "B" Column.
So using the advantage of this VBA script I was trying to create a formula what will adjust the column letter in the original formula every time.
So this formula:
=("Tasks!" & "$" & Col_Letter(COLUMN(Table32[[#Headers];[Owned By]])) & "$" & "2" & ":" & "$" & Col_Letter(COLUMN(Table32[[#Headers];[Owned By]])) & "$" & ROW(INDEX(Table32;1;1))+ROWS(Table32)-1) is giving me this as the result Tasks!$H$2:$H$65536.
what is part of the formula so I think it's good.
This is the whole formula I was trying to re-create in this manner:
=Index((("Tasks!" & "$" & Col_Letter(COLUMN(Table32[[#Headers];[Owned By]])) & "$" & ROW() & ":" & "$" & Col_Letter(COLUMN(Table32[[#Headers];[Owned By]])) & "$" & ROW(INDEX(Table32;1;1))+ROWS(Table32)-1);SMALL(IF((((Col_Letter(COLUMN(Table8[[#Headers];[ID]])) & ROW()) & "=" & (("Tasks!" & "$" & Col_Letter(COLUMN(Table32[[#Headers];[ID]])) & "$" & "2"& ":" & "$" & Col_Letter(COLUMN(Table32[[#Headers];[ID]])) & "$" & ROW(INDEX(Table32;1;1))+ROWS(Table32)-1))));Row((("Tasks!" & "$" & Col_Letter(COLUMN(Table32[[#Headers];[ID]])) & "$" & "2"& ":" & "$" & Col_Letter(COLUMN(Table32[[#Headers];[ID]])) & "$" & ROW(INDEX(Table32;1;1))+ROWS(Table32)-1)))-ROW(("Tasks!" & "$" & Col_Letter(COLUMN(Table32[[#Headers];[ID]])) & "$" & "2"))+1);ROWS($1:$1))))
Excel Tables have the potential of resolving such questions easily. Since the Columns can be referred by their names, you do not have to worry where they are located. And likewise, since you reference the data by column name but not row number, your reference will be covering all of the rows within that Table, no matter how many rows are added or deleted.
So as a start, try converting your range to Table by selecting it and use Insert / Table. After this you should convert your formula to contain Table references rather than cell references.
I am trying to count how many dates in the G column (or even better: column with the header "document date") are:
3 workdays old or less
between 3 and 30 workdays
over 30 workdays
These are formulas that I use in excel but I would like to do it with just one click because the excel file is a download from SAP and each day new download is performed.
=COUNTIF(G:G, ">=" & WORKDAY(TODAY(),-1)-2)
=COUNTIF(G:G,"<=" & WORKDAY(TODAY(),-1)-3) - COUNTIF(G:G, "<=" & WORKDAY(TODAY(),-1)-30)
=COUNTIF(G:G, "<=" & WORKDAY(TODAY(),-1)-30)
I am new with VBA and can't really translate it to vba code.
Hope you can help me out.
Thanks!
in VBA the formulas should be
=COUNTIF(G:G, "">="" & WORKDAY(TODAY(),-1)-2)
=COUNTIF(G:G,""<="" & WORKDAY(TODAY(),-1)-3) - COUNTIF(G:G, ""<="" & WORKDAY(TODAY(),-1)-30)
=COUNTIF(G:G, ""<="" & WORKDAY(TODAY(),-1)-30)
For example if you want the formulas to be in H1, H2 & H3 cells then VBA code will be
Cells(1, 8).Value = "=COUNTIF(G:G, "">="" & WORKDAY(TODAY(),-1)-2)"
Cells(2, 8).Value = "=COUNTIF(G:G,""<="" & WORKDAY(TODAY(),-1)-3) - COUNTIF(G:G, ""<="" & WORKDAY(TODAY(),-1)-30)"
Cells(3, 8).Value = "=COUNTIF(G:G, ""<="" & WORKDAY(TODAY(),-1)-30)"
I am very new to VBA programming and have an issue.
I have a base file called liquidity forecast in which I fill data from other documents in. The documents I collect data from is updated every day so the date in the name changes every day.
The Name goes "(Date, "YYMMDD") & "SE_Laizy.xlsx" so an example would be, 160229SE_Laizy.xlsx
When I collect data I use Index match formula. My problem is trying to update the link within the formula by using a date value.
Currently I write it like this,
ActiveCell.Range((Cells(1, 1)), (Cells(1, 1))).FormulaR1C1 = _
"=INDEX('[" & Format(Date, "YYMMDD") & "SE_Laizy.xlsx"]Visa'!R1:R1048576,MATCH(R2C,'[" & Format(Date, "YYMMDD") & "SE_Laizy.xlsx"]Visa'!C1,0),MATCH(""Ub perioden"",'[" & Format(Date, "YYMMDD") & "SE_Laizy.xlsx"]Visa'!R2,0))"
All I get from this is a NA. Any help would be appreciated!
I've added a parent worksheet reference and broken the INDEX and
MATCH functions into the three primary sections.
With Worksheets("Sheet2")
.Cells(1, 1).FormulaR1C1 = _
"=INDEX('[" & Format(Date, "YYMMDD") & "SE_Laizy.xlsx]Visa'!C1:C16384, " & _
"MATCH(R2C,'[" & Format(Date, "YYMMDD") & "SE_Laizy.xlsx]Visa'!C1, 0), " & _
"MATCH(""Ub perioden"", '[" & Format(Date, "YYMMDD") & "SE_Laizy.xlsx]Visa'!R2, 0))"
End With
As noted, there were some misplaced quotes in the external workbook name. I'm not sure what to do with your Range object definition. For all intents and purposes, the one supplied simply resolves down to [A1].