Quotes for String and Cell Reference - vba

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.

Related

For Loop to create a dynamic outlook email body that will have lines dependent on trades for a specific date

I am generating an email that shows trades for the day in the body of an email. Currently the only way I can do this is with an If then statement for specific numbers of trades. If we traded 10, I need an if then statement with 10 as the variable criteria, but if I only have 9, then I get an error. I want a dynamic method instead. I can do a For Loop that will list all the trades in debug.print, but in the email, it each trade overwrites the prior trade and I show only one line. I also need an intro like "Hi, today today's trades are: " followed by each trade listed below line by line.
I tried this and it works but only if I have the right number of trades and a variable that matches it. For example, if I had 23 trades, I need an If statement with 23 as a variable value in this case m. I used Arrays as VBA does not let me create a list. Unfortunately, I cannot pull the whole array, I need to pull line by line. If I could pull the array, I could just have a simple loop.
If m = 23 Then
.Body = "Hi Chris," & vbLf & vbLf & "The following trade(s) was completed today:" & vbLf & vbLf & ArrayValues(0) & vbLf & ArrayValues(1) & vbLf & ArrayValues(2) & vbLf & ArrayValues(3) & vbLf & ArrayValues(4) & vbLf & ArrayValues(5) & vbLf & ArrayValues(6) & vbLf & ArrayValues(7) & vbLf & ArrayValues(8) & vbLf & ArrayValues(9) & vbLf & ArrayValues(10) & vbLf & ArrayValues(11) & vbLf & ArrayValues(12) & vbLf & ArrayValues(13) & vbLf & ArrayValues(14) & vbLf & ArrayValues(15) & vbLf & ArrayValues(16) & vbLf & ArrayValues(17) & vbLf & ArrayValues(18) & vbLf & ArrayValues(19) & vbLf & ArrayValues(20) & vbLf & ArrayValues(21) & vbLf & ArrayValues(22) & vbLf & vbLf & "Thanks"
End if
I want to use something like:
For b = 1 To LastRow
If Trades.Range("H" & b) = TDate Then
Debug.Print (Range("B" & b) & " " & Range("C" & b) & " " & Range("D" & b) & " " & Range("F" & b) & " " & Range("G" & b))
End If
Next b
This way it does not matter how may trades I have, one formula would over it all. Each Range has a trade characteristic.
If I do a debug.Print in the immediate window I get a list just like I want, but in the email, each line overwrites the prior trade.
I am a rookie at this and appreciate any help. Thanks
Sounds like you need to build a string which can be assigned to the message body. For such tasks you can use String Functions available in VBA where you can prepare the correct string and then assign to the message body.
If m = 23 Then
.Body = "Hi Chris," & vbLf & vbLf & "The following trade(s) was completed today:" & vbLf & vbLf & ArrayValues(0) & vbLf & ArrayValues(1) & vbLf & ArrayValues(2) & vbLf & ArrayValues(3) & vbLf & ArrayValues(4) & vbLf & ArrayValues(5) & vbLf & ArrayValues(6) & vbLf & ArrayValues(7) & vbLf & ArrayValues(8) & vbLf & ArrayValues(9) & vbLf & ArrayValues(10) & vbLf & ArrayValues(11) & vbLf & ArrayValues(12) & vbLf & ArrayValues(13) & vbLf & ArrayValues(14) & vbLf & ArrayValues(15) & vbLf & ArrayValues(16) & vbLf & ArrayValues(17) & vbLf & ArrayValues(18) & vbLf & ArrayValues(19) & vbLf & ArrayValues(20) & vbLf & ArrayValues(21) & vbLf & ArrayValues(22) & vbLf & vbLf & "Thanks"
End if
Your strategical mistake is that you are trying to set up the Body property for a specific case. Instead, consider concatenating the string with the required piece of data (another string) which makes sense for a particular case. And only when you are done adding all the bits to the result string, you can assign it to the message body.
Be aware, the Body property is a plain text string and doesn't deliver any formatting. Instead, I'd suggest using the HTMLBody property instead. The HTMLBody property should be an HTML syntax string. Setting the HTMLBody property will always update the Body property immediately.

ranking data using vba

i come to ask your help i need to rank data using vba i have a block of results in column D and i want to rank them in column E without skiping any value so i tried this vba code but it gives me only zeros in all the column then my computer becom slow untill i close the excel file this is the vba code i am using if anyone can help me :
Sub Mactro5()
LastRow = Range("D" & Cells.Rows.Count).End(xlUp).Row
Range("E2:E" & LastRow).Formula = _
"=IF(D2=" & Chr(34) & Chr(34) & "," & Chr(34) & Chr(34) & ",SUMPRODUCT((D$2:D$" & LastRow & ">D2)/COUNTIF(D$2:D$" & LastRow & ",D$2:D$" & LastRow & "&" & Chr(34) & Chr(34) & "))+1)"
End Sub

sumifs formula in 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) & "))"

Number of query values and destination fields are not the same in vb6 Error

I want to ask something,
I wrote the code like this, but when I execute that, it contain an error like this
"Number of query values and destination fields are not the same"
Before I show my code, I'll show you my structural table :
No : AutoNumber
NoNota : Text
NamaMotor : Text
NamaPeg : Text
Unit : Number
TotalJasa : Number
TotalPart : Number
GrandTotal : Number
Here's my code below :
dung = "INSERT INTO TmpTransaksi VALUES (" & _
"" & TBox(33).Text & "," & _
"'" & TBox(0).Text & "'," & _
"'" & TBox(32).Text & "'," & _
"'" & TBox(30).Text & "'" & _
"" & TBox(25).Text & "," & _
"" & TBox(23).Text & "," & _
"" & TBox(15).Text & "," & _
"" & TBox(16).Text & ")"
MsgBox dung
cn.Execute dung
Can you tell me where's my fault and correct it?
Thanks before..
You missed a comma. Change
"'" & TBox(30).Text & "'" & _
^^^
to
"'" & TBox(30).Text & "'," & _
On a side note: consider to learn and use parameterized queries instead of interpolating query strings.

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.