sumifs formula in vba - vba

I am trying to populate a column with a SUMIFS formula if the criteria is matched.
cell.Offset(0, 2).Value = "=SUMIFS(PickData!E:E,PickData!A:A, _
" & cell.Address(Rowabsolute:=False, Column:=False) & ", PickData!C:C, _
"Retail",PickData!C:C, PickData!L:L, "Report1.TextBox1.Value")"
I can't see where i'm going wrong with it looking up the specific work Retail in PickData|C:C & the value from TextBox1 (this is date)
Any help would be greatly appreciated.
Thanks
Al

If you want a formula in the cell(s) then try this.
cell.Offset(0, 2).Formula = "=SUMIFS(PickData!E:E, PickData!A:A, " _
& cell.Address(0, 0) & ", PickData!C:C, " & Chr(34) & "Retail" & Chr(34) _
& ", PickData!L:L, DATEVALUE(" & Report1.TextBox1.Value & "))"
That should give you a valid SUMIFS(...) formula.
Addendum: looking at that a second time, the form's textbox value might need to be in quotes.
cell.Offset(0, 2).Formula = "=SUMIFS(PickData!E:E, PickData!A:A, " _
& cell.Address(0, 0) & ", PickData!C:C, " & Chr(34) & "Retail" & Chr(34) _
& ", PickData!L:L, DATEVALUE(" & Chr(34) & Report1.TextBox1.Value & Chr(34) & "))"

Related

Quotes for String and Cell Reference

I need to convert an excel formula into VBA codes, however I tried many times but no luck.
Below is the formula which I need to convert.
=IF(G7959<0, "Delivered to end customer " & E7959, "To be delivered to end customer " & E7959)
Below is what I have tried but did not success,the parameter 'IC_inventory_new_row' represent a variable row index. Could anyone help to take a look and advise?
IC_inventory_WS.Cells(IC_inventory_new_row, 15).Formula = "=IF(G" & IC_inventory_new_row + 1 & "<0, " & "Delivered to end customer" & "E" & IC_inventory_new_row & "," & "To be delivered to end customer " & "E" & IC_inventory_new_row & ")"
Try, please:
IC_inventory_WS.Cells(IC_inventory_new_row, 15).formula = "=IF(G" & IC_inventory_new_row + 1 & _
"<0, ""Delivered to end customer ""&E" & IC_inventory_new_row & _
", ""To be delivered to end customer ""&E" & IC_inventory_new_row & ")"
But it can be written in a way to not needing iteration. I mean, to drop it in all the range at once. Try this way which should do what you need, if i correctly understood what is needed:
IC_inventory_WS.Range("O2:O" & lastRow).formula = "=IF(" & Range("G2").address(0, 0) & _
"<0, ""Delivered to end customer ""&" & Range("E2").address(0, 0) & _
", ""To be delivered to end customer ""&" & Range("E2").address(0, 0) & ")"
If not, please specify what does it against what you really need.

VBA adding multiple criterias for SumIF

EndRowH = ActiveSheet.Range("H65536").End(xlUp).Row
ActiveSheet.Range("H1").Formula = "=SUMIFS(H3:H" & EndRowH & ", C3:C" & EndRowH & ", "">=" & lngStart & """)"
I Have a criteria on the C column where it has to be greater than the lngStart Value.
The code is working till here.. But I have to add an extra criteria where the sumif needs to be done only if there is blank value in the column I
Dim Blank As Long
Blank = ""
ActiveSheet.Range("H1").Formula = "=SUMIFS(H3:H" & EndRowH & ", C3:C" & EndRowH & ", "">=" & lngStart & "",I3:I" & EndRowH & ", ""!=" & Blank & "" ")"
When working with complex strings you want to put as a Formula, you might want to use an "helper" string, in the code below I use FormulaString, so when you run the Debug.Print line, you can see if the formula string is valid.
Also, and it's only my preference, to get the " inside, I prefer to use Chr(34).
Code
Dim FormulaString As String
FormulaString = "=SUMIFS(H3:H" & EndRowH & ", C3:C" & EndRowH & "," & Chr(34) & ">=" & lngStart & Chr(34) & ",I3:I" & EndRowH & "," & Chr(34) & "<>" & Chr(34) & ")"
Debug.Print FormulaString
ActiveSheet.Range("H1").Formula = FormulaString
Running this code, you will get in the immediate window the following formula:
=SUMIFS(H3:H10, C3:C10,">=5",I3:I10,"<>")
I'd go this way
ActiveSheet.Range("H1").Formula = "=SUMIFS(H3:H" & EndRowH & ", C3:C" & EndRowH & ", concatenate("">=""," & lngStart & "),I3:I" & EndRowH & ", ""="")"

How to extract specific multiple text in SQL?

I am trying to extract specific text from 1 column which seems to have concatenated several data points. Here is an example of part of the output that appears in 1 row:
[{"q":{"as":[{"id":"1","tags":[{"tagid":"62","tagstr":"Example1"},{"tagid":"3","tagstr":"Example1"},{"tagid":"65","tagstr":"Example1"},{"tagid":"71","tagstr":"Example1"}],"text":"Example1"}],"hidden":"false","id":"1","questionalias":"1","text":"Example1","ttl":"Example1"}},
The text in bold is what I am trying to extract. In practice each 'Example1' is selected from an option of words. Therefore I know exactly what text I am looking for. What I am struggling with is creating a way for the output to strip out the unwanted text and return the key words (around 8)
Alternatively, if someone has done something similar in VBA, that could also be an option.
Has anyone faced this before?
You can parse you data with Regex! It's marvelous!
There're plenty (Left, Mid, Right, Instr) functions to parse your data, right?
Some people, when confronted with a problem, think
“I know, I'll use regular expressions.”
Now they have two problems.
I think, that you trying to bound to some keywords(tagstr, text and ttl), so take a look at this.
Feel free to modify this expression, take a look on this and that!
In VBA there's no regex from scratch , so add VBA reference to "Microsoft VBScript Regular Expressions 5.5"
This is my exapmle with your data:
Sub test()
Dim Data As String
Dim Re As RegExp
Dim ReMatch As MatchCollection
Dim CurrentMatch As Match
Data = "[{" & Chr(34) & "q" & Chr(34) & ":{" & Chr(34) & "as" & Chr(34) & ":[{" & Chr(34) & "id" & Chr(34) & ":" & Chr(34) & "1" & Chr(34) & "," & Chr(34) & "tags" & Chr(34) & _
":[{" & Chr(34) & "tagid" & Chr(34) & ":" & Chr(34) & "62" & Chr(34) & "," & Chr(34) & "tagstr" & Chr(34) & ":" & Chr(34) & "Example1" & Chr(34) & "},{" & Chr(34) & "tagid" & Chr(34) & ":" & Chr(34) & "3" & Chr(34) & _
"," & Chr(34) & "tagstr" & Chr(34) & ":" & Chr(34) & "Example1" & Chr(34) & "},{" & Chr(34) & "tagid" & Chr(34) & ":" & Chr(34) & "65" & Chr(34) & "," & Chr(34) & "tagstr" & Chr(34) & ":" & Chr(34) & "Example1" & Chr(34) & _
"},{" & Chr(34) & "tagid" & Chr(34) & ":" & Chr(34) & "71" & Chr(34) & "," & Chr(34) & "tagstr" & Chr(34) & ":" & Chr(34) & "Example1" & Chr(34) & "}]," & Chr(34) & "text" & Chr(34) & ":" & Chr(34) & "Example1" & Chr(34) & _
"}]," & Chr(34) & "hidden" & Chr(34) & ":" & Chr(34) & "false" & Chr(34) & "," & Chr(34) & "id" & Chr(34) & ":" & Chr(34) & "1" & Chr(34) & "," & Chr(34) & "questionalias" & Chr(34) & ":" & Chr(34) & "1" & Chr(34) & _
"," & Chr(34) & "text" & Chr(34) & ":" & Chr(34) & "Example1" & Chr(34) & "," & Chr(34) & "ttl" & Chr(34) & ":" & Chr(34) & "Example1" & Chr(34) & "}},"
Debug.Print "My data is:" & vbNewLine & Data
Set Re = New RegExp
Re.IgnoreCase = True
Re.Global = True
Re.MultiLine = True
Re.Pattern = "(?=" & Chr(34) & "tagstr" & Chr(34) & "|" & Chr(34) & _
"text" & Chr(34) & "|" & Chr(34) & "ttl" & Chr(34) & ")(?:" & Chr(34) & _
"\w*" & Chr(34) & ":" & Chr(34) & "(.*?)" & Chr(34) & ")"
Debug.Print "My pattern is:" & vbNewLine & Re.Pattern
Set ReMatch = Re.Execute(Data)
Debug.Print "Matched " & ReMatch.Count & " times!"
For Each CurrentMatch In ReMatch
Debug.Print "Capture " & CurrentMatch.SubMatches(0) & " in " & CurrentMatch.Value
Next
End Sub
Output:
Not so complicated, right?
You can do this with standart string functions after all..

Put formula with if statements in the entire columns

I have recorded a macro with the following formula, but it gives me an error in the second line.
Expected end of statement.
I guess the issue is that its way to long. Please suggest how to make this work?
Sub Macro1()
Range("CG2").Select
ActiveCell.FormulaR1C1 = _
"=IF(RC69=""High"",(IF(AND(RC4<>"""",RC5<>"""",RC6<>"""",RC7<>"""",RC8<>"""",RC10<>"""",RC11<>"""",RC12<>"""",RC13<>"""",RC14<>"""",RC16<>"""",RC17<>"""",RC18<>"""",RC19<>"""",RC20<>"""",RC21<>"""",RC22<>"""",RC23<>"""",RC24<>"""",RC25<>"""",RC26<>"""",RC27<>"""",RC28<>"""",RC29<>"""",RC30<>"""",RC31<>"""",RC32<>"""",RC33<>"""",RC34<>"""",RC35<>"""",RC36<>"""",RC37<>"""",RC38<>"""",RC39<>"""",RC40<>"""",RC41<>"""",RC42<>"""",RC43<>"""",RC44<>"""",RC45<>"""",RC46<>""""),""No"",""Yes""))," & Chr(10) & "(IF(RC69=""Medium"",(IF(AND(RC4<>"""",RC5<>"""",RC6<>"""",RC7<>"""",RC8<>"""",RC10<>"""",RC11<>"""",RC12<>"""",RC13<>"""",RC14<>"""",RC16<>"""",RC17<>"""",RC18<>"""",RC19<>"""",RC20<>"""",RC21<>"""",RC22<>"""",RC23<>"""",RC31<>"""",RC32<>"""",RC33<>"""",RC34<>"""",RC35<>"""",RC36<>"""",RC37<>"""",RC38<>"""",RC39<>"""",RC40<>"""",RC41<>"""",RC42<>"""",RC44<>"""",RC45<>"""",RC46<>""""),""No"",""Yes""))," & Chr(10) & "(IF(AND(RC4<>"""",RC5<>"""",RC6<>"""",RC7<>"""",RC8<>"""",RC10<>"""",RC11<>"""",RC12<>""""
""",RC14<>"""",RC16<>"""",RC17<>"""",RC18<>"""",RC19<>"""",RC20<>"""",RC31<>"""",RC32<>"""",RC33<>"""",RC34<>"""",RC35<>"""",RC36<>"""",RC37<>"""",RC38<>"""",RC39<>"""",RC41<>"""",RC42<>"""",RC45<>"""",RC46<>""""),""No"",""Yes"")))))"
End Sub
The issue here is the multiples ", and you also have a different kind of these : “ which do not work!
So replace the " by Chr(34) like this :
Sub formula()
Range("CO2:CO").formula = "=IF(OR(LEN($BN2)=0;$BN2=" & Chr(34) & "Not Performed" & Chr(34) & _
";LEN($BK2)=0;$BK2=" & Chr(34) & "Not Performed" & Chr(34) & _
";LEN($BL2)=0;$Q2=" & Chr(34) & "Not Performed" & Chr(34) & _
";LEN($BM2)=0);" & Chr(34) & "Yes" & Chr(34) & ";" & Chr(34) & _
"No" & Chr(34) & ")"
End Sub

VBA escaping characters in formula

I have a formula which hardcoded should look like this:
=SUMMEWENNS(Rawdata!K2:K3446;Rawdata!I2:I3446;"bezahlt";Rawdata!A2:A3446;">="&DATWERT("18.03.2013 00:00");Rawdata!A2:A3446;"<="&DATWERT("24.03.2013 23:59"))
I want to add the formula via VBA into different cells and have come up with this string, but there is a syntax problem and I cannot find the error. It most likely has to do with the escaping of the characters espacially with the "DATWERT".
qq = Chr(34)
Cells(5, fieldextsales).FormulaLocal = "=SUMMEWENNS(RawData!K2:K" & _
maxnumrows & ";Rawdata!I2:I" & maxnumrows & ";" & qq & _
"bezahlt" & qq & ";Rawdata!A2:A" & maxnumrows & ";" & _
qq & ">= " & DATWERT(weekstart & " 00:00") * 1 & qq & _
";RawData!A2:A" & maxnumrows & ";" & qq & "<= " & _
DATWERT(weekend & " 23:59") * 1 & qq & ")"
Could anybody help me out? Hope I get the hang of it then.
Thx
Here we go:
"=SUMMEWENNS(RawData!K2:K" & _
maxnumrows & ";Rawdata!I2:I" & maxnumrows & ";" & _
"""bezahlt""" & ";Rawdata!A2:A" & maxnumrows & ";" & _
""">=""&DATWERT(""" & weekstart & " 00:00"")" & _
";RawData!A2:A" & maxnumrows & ";" & _
"""<=""&DATWERT(""" & weekend & " 23:59""))"
From the VBA side you use plain English function names, not local names => DATWERT shoud be DateValue, unless you want to embed it into your formula.