VBA, If not empty execute for row - excel

In my script I got the following line.
.Range(.Cells(1, "GP"), .Cells(last, "GP")).FormulaR1C1 = "=ISOWEEKNUM(RC" & i & ")"
However, for an empty row this writes "52" (Since that's the week for an empty date).
Is there a way I can write nothing if the line is empty?

Perhaps use this as your formula:
"=IF(RC" & i & "="""","""",ISOWEEKNUM(RC" & i & "))"

I ended up solving it with:
"=IF(RC" & i & "<>"""",ISOWEEKNUM(RC" & i & "),"""")" to be exact worked

Related

Worksheet Function Countifs for resizable range

I have a table in which I count the records through Worksheet.Function.Countif.
It is nice because it counts the rows using .Rows.Count and so I am alwasy ensured if my table changes the size.
It looks like that (subset of the code):
endrow = .Cells(.Rows.Count, 20).End(xlUp).Row
ws1.Cells(6, 34).Formula = "=COUNTIF(" & .Range("U6:U" & endrow).Address & ",U6)"
I wish to write the the worksheet.function formula in the same way as above but for 'Countifs'. In excel, I would type it like that:
=COUNTIFS($U$6:$U$144;U6;$T$6:$T$144$;"<>"&T6)
How to write it in vba, using 'endrow' as in the first demonstarted code, i.e. without '144' as the last row but with '& endrow' ?
I was trying multiple times, but I cannot get it to work :/
I will appreciate any help.
Try this:
ws1.Cells(6, 34).Formula = "=COUNTIFS($U$6:$U$" & endrow & ",U6,$T$6:$T$" & endrow & "," & """" & "<>" & """" & "&T6" & ")"
This formula gets the last row of column A:
=IFERROR(LOOKUP(2,1/(NOT(ISBLANK(A:A))),ROW(A:A)),0)

Expected end of statement VBA error

I hope you're all doing well!
A quick but tricky question (at least for me)
I have a code that interpolates values according to dates. Basically I want to insert the interpolation value to a cell in the main workbook in the sheet "Deals".
BInterpol is a function that works with a program installed in my computer. In the function, I take dates( D4:D18) linked to values (E4:E18), the following argument is the date that is located in another workbook and sheet and then linear is the method of interpolation. When I write the code below, it gives me Expected end of statement error and it highlights Linear. Any idea what needs to be change so it works ? (I want to make so that the formula is written in the cell needed so the interpolation is done there)
wb2.Sheets("Deals ").Range("V" & x).Value = "=BInterpol('INTERP'!D4:D18,'INTERP'!E4:E18," & wb1.Sheets("New").Range( "I" & j) & " , " Linear " )"
Try with .Formula and concatenate the Linear variable to the string with & as mentioned in the comments:
wb2.Sheets("Deals MTL").Range("V" & x).Formula = "=BInterpol('OIS INTERP'!D4:D18,'OIS INTERP'!E4:E18," & wb1.Sheets("NewTrades").Range( "I" & j) & "," & Linear ")"
Depending on what exactly is Range("I" & j) you may be interested in getting its address and not its value:
wb2.Sheets("Deals MTL").Range("V" & x).Formula = "=BInterpol('OIS INTERP'!D4:D18,'OIS INTERP'!E4:E18," & _
wb1.Sheets("NewTrades").Range("I" & j).Address & _
"," & Linear ")"
Nevermind, got it !
wb2.Sheets("Deals").Range("V" & x).Value = "=BInterpol('INTERP'!D4:D18,'INTERP'!E4:E18," & wb1.Sheets("New").Range( "I" & j) & " , "**"** Linear **"**" )"
The Linear needed two pairs of " for some reason ( don't know why, but that solved it.

Incorporate Error-Checking Formula When Copying Excel Vba

I currently have the following code for copying cells:
Set Feeder = Sheets("Projects").Range("B" & Rows.Count).End(xlUp)
With Sheets("Database")
Set Storage = .Range("C" & .Rows.Count).End(xlUp).Offset(-Masterrow + 1)
Storage.Value2 = "=" & "Projects!" & Feeder.Address
End With
Is there a way to incorporate the formula =IFERROR(B2,0) so that my copy location contains =IFERROR(Projects!B2,0) as opposed to =Projects!B2?
I want erroneous cells to return a 0 as opposed to an error code so I can just run my delete rows code easily.
The fix was straightforward after realizing I had already constructed a formula before.
Code from before:
Storage.Value2 = "=" & "Projects!" & Feeder.Address
Code after:
Storage.Value2 = "=" & "IFERROR(" & "Projects!" & Feeder.Address & ",0)"
Sometimes it really is simple!

Quotes in VBA formula

I'm trying to figure out what i'm doing wrong i'm trying to add quotation marks to "engine oils" below. but it keeps giving me an error.
can anyone see what i'm doing wrong?
Sheets("PDF Copy").Cells(t, 6).Formula = "=IF(Daily!I8" & i & "=""ENGINE OILS"" ,DAILY!I" & i & ",Daily!K" & i
You are missing a closing bracket.
'=IF(Daily!I810="ENGINE OILS",Daily!I10,Daily!K10)
Sheets("PDF Copy").Cells(2, 6).Formula = _
"'=IF(Daily!I8" & i & "=""ENGINE OILS"" ,DAILY!I" & i & ",Daily!K" & i & ")"

Creating a VBA formula using CountIF

I am creating a formula to count based upon 2 conditions. My logic is wrong - again.
When I (manually) enter enter code the summation formula (COUNTIFS) into correct cell itself, it sums correctly:
COUNTIFS(E4:E1362,"Requirement",S4:S1362, "<>4")
When I execute the following code line, I do not get any errors, but instead, all the sums are zero.
Range("G" & Start(groups) - 1).Formula = "=COUNTIFS(E" & Start(groups) & ":E" & Finish(groups) & "," & "Requirement" & ",S" & Start(groups) & ":S" & Finish(groups) & "," & Chr(34) & "<>4" & Chr(34) & ")"
I realized that the COUNTIFS was comparing the value in column S to the string "<>4". And none of the cells contain that string. This is why all my values were zero. I do not want the comparison to be against that string. I want the comparison to be column S value NOT equal to 4.
So, I changed the line to (and variations to get it to work):
Range("G" & Start(groups) - 1).Formula = "=COUNTIFS(E" & Start(groups) & ":E" & Finish(groups) & "," & "Requirement" & ",S" & Start(groups) & ":S" & Finish(groups) & "," & Chr(34) & "<>" & Chr(34) & "4)"
So, I am at a loss. (1) I manually enter the formula and it works. (2) I build it in VBA, and it does not sum correctly. (3) I look up how to do build the formula correctly, and I get errors time and time again. It seems like the only way to get this to work is to keep the quotes within quotes, but I don't want to compare against the string.
Does this make sense? I'm not liking my worksheet anymore. It is no longer any fun at all. :(
Thank you so much.
I think all the Chr(34) are getting in the way of you viewing your formula correctly. try using "" to make the quote for the formula.
.Formula = "=COUNTIFS(E4:E1362,""Requirement"",S4:S1362,""<>4"")"
with your Start and Finish functions, that would change the formula to:
.Formula = "=COUNTIFS(E" & Start(groups) & ":E" & Finish(groups) & _
",""Requirement"",S" & Start(groups) & ":S" & Finish(groups) & ",""<>4"")"
Well, One of the problem I found there was quite simple, instead using "," you must use ";".
It depends on the version you used, some using "," and some others using ";". you can just try it, I hope it solved your problem.
and second one "Requirement" there, I think you should use double quote instead.
and for your case To meet This condition:
COUNTIFS(E4:E1362,"Requirement",S4:S1362, "<>4")
Use This:
"=COUNTIF(E" & Start(groups) & ":" & "E" & Finish(groups) & "," & """Requirement""" & "," & "S" & Start(groups) & ":" & "S" & Finish(groups) & "," & """<>4"")"
I think it Should Work (if I don't miss any Quotes thought).
... Try removing the chr(34) and the extra quotations - It should look exactly like the "Requirement" criteria:
Range("G" & Start(groups) - 1).Formula = "=COUNTIFS(E" & Start(groups) & ":E" & Finish(groups) & ",""Requirement"",S" & Start(groups) & ":S" & Finish(groups) & ",""<>4"")"
After running a quick test with your initial code
Range("G1").Formula = "=COUNTIFS(E1" & ":E20" & "," & "Requirement" & ",S1" & ":S20" & "," & Chr(34) & "<>4" & Chr(34) & ")"
Modified slightly to account for your groups variable cell G1 ends up with the following formula:
=COUNTIFS(E1:E20,requirement,S1:S20,"<>4")
which when tested works absolutely fine for your second if statement S1:S20,"<>4".
I would guess that the problem lies with your first statement. What is in your requirement range and what data are you trying to match with.
I set up the requirement named range to be cell D5, I entered a 5 in there and the formula would increment each time I added an extra 5 to the range E1:E20.
I then started typing 4's into the range S1:s20 and this decreased my count.
edit
The last thing that I can suggest is to add in a helper column in column F that evaluates row to see if it matches the requirement condition, then it would be a simple matter of making your formula:
=COUNTIFS(F1:F20,TRUE,S1:S20,"<>4")

Resources