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)"
Related
I have two formulas that I need to transfer to VBA.
On Excel, my formula would be =countif(A$2:A2,A2) so I transferred that using this formula but everything is returning to 1. The rows didn't become dynamic and I want only the values to be displayed.
For a = 2 To lrow
ws.Range("T" & a).Formula = "=CountIf(A$2&"":""&A2)"",""&A2)"
Next a
Next formula that I use in Excel is
=IF(COUNTIF(A:A,A2)>Q2,"Check","Ok")
I tried this formula in VBA:
For i = 2 to lrow
If Countif(ws.Range("A2:A" & lrow), "A2") > ws.Range("Q2:Q", & lrow) Then
ws.Range("T" & i).Value = "Check"
Else
ws.Range("T" & i).Value = "Ok"
End If
Next i
You could populate column T with your first formula with this line of code:
ws.Range("T2:T" & lrow).FormulaR1C1 = "=COUNTIF(R2C[-19]:RC[-19],RC[-19])"
I can't advise on your second formula unless you clarify where you want to write it...
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)"
I want to put an if-formula into a cell.
I get an error 1004 when I try to run the code (relevant part below).
n = 1.2
.Cells(2, 8).Formula = "=if(" & .Cells(2, 3).Address(False, False) & "=1," & n & ",na())"
If I put the formula into a variable, I can see that VBA "saves" the 1.2 as "1,2" which is interpreted as if there are too many arguments in the formula.
So instead of
=if(C2=1,1.2,na())
I get
=if(C2=1,1,2,na())
If I manually enter
.Cells(2, 8).Formula = "=if(" & .Cells(2, 3).Address(False, False) & "=0,1.2" & ",na())"
The formula comes out right in Excel, but as I need to loop over a data, that is not a sustainable solution.
As I am based in Europe, I suspect regional settings being a part of the problem, but as those are company-wide, I can't fiddle with that without causing a different set of problems.
I tried
n = 1.2
.Cells(2, 8).Formula = "=if(" & .Cells(2, 3).Address(False, False) & "=1," & CDbl(n) & ",na())"
I am trying to insert a SUMIF formula into a cell using VBA. VBA is not allowing my code to run as it is. Any suggestions are appreciated.
ws and na are properly set earlier on in the code. If I simply change the SUMIF formula to a random value "x" , it appears in the desired cell. The error is occurring within the SUMIF formula that I am trying to insert into a cell.
ws.Range("B" & na.Row + 2).Value = "=SUMIF(OFFSET(B1,,,ROW()-1,1),"<>#N/A"))"
The purpose of this formula is to SUM a column of numbers while ignoring any cells that contain "#N/A".
When using quotes in a formula, you need to "double up":
ws.Range("B" & na.Row + 2).Formula = "=SUMIF(OFFSET(B1,,,ROW()-1,1),""<>#N/A"")"
You can use AGGREGATE and remove the OFFSET which is volatile
ws.Range("B" & na.Row + 2).Formula= "=AGGREGATE(9,6,B1:B" & na.Row + 1 & ")"
Try using 'Chr(34)':
ws.Range("B" & na.Row + 2).Formula = "=SUMIF(OFFSET(B1,,,ROW()-1,1)," & Chr(34) & "<>#N/A" & Chr(34) & ")"
Edit: Deleted quotes written by mistake
I would like to get the product of values of a range of cells in excel, using VBA.
I got a selection e.g.: (that works)
ActiveSheet.Range(Cells(i, j), Cells(i, z)).Select
I would like to put the product of a range in another cell but this is not working:
'Range("X10").Formula = "=PRODUCT(Range(Cells(i, j),Cells(i, z)))"
Help is really appreciated,
thanks in advance!
Range("X10").Formula = "=PRODUCT(" & Cells(i, j).Address & ":" & Cells(i, z).Address & ")"