The last line is causing some trouble.
MsgBox ("Book Code = " & ISBN & vbNewLine & _
"Book Title= " & title & vbNewLine & _
"Unit Price= " & title & vbNewLine & _
"Quanity = " & UnitCost & vbNewLine & _
"Quanity = " & CustomerInput & vbNewLine & _
"Final Price = " & (Format(CustomerInput * UnitCost), Currency))
Move the close-parenthesis after UnitCost in the Format statement to the end, and Currency in the format statement needs to be a string (i.e: in quotes):
MsgBox ("Book Code = " & ISBN & vbNewLine & _
"Book Title= " & Title & vbNewLine & _
"Unit Price= " & Title & vbNewLine & _
"Quanity = " & UnitCost & vbNewLine & _
"Quanity = " & CustomerInput & vbNewLine & _
"Final Price = " & (Format(CustomerInput * UnitCost, "Currency")))
Although the above works, you can omit some of your parentheses, and possibly make the code a bit easier to understand:
MsgBox "Book Code = " & ISBN & vbNewLine & _
"Book Title= " & Title & vbNewLine & _
"Unit Price= " & Title & vbNewLine & _
"Quanity = " & UnitCost & vbNewLine & _
"Quanity = " & CustomerInput & vbNewLine & _
"Final Price = " & Format(CustomerInput * UnitCost, "Currency")
Related
I am trying to make a macro that loops through a table on any sheet across a document and generated emails with info from that table to the individuals on each line. I think I got most of it to work but I can't figure out why .Body is no longer sending any information to outlook.
When I had shorter test messages it worked but now it isn't sending any body text to Outlook. My .To line is working fine as is my subject.
With OutMail
.To = Range("D" & i).Value
.CC = ""
.BCC = ""
.Subject = ActiveSheet.Name & " Service Insert"
'.HTMLBody = "Hi " & Range(C, i).Value & "<br><br>" & "Please see your Service Insert Below." & "<br>" & "<br>" _
& Range(S, 1).Value & "<br>" & "Services Played: " & Range(S, i).Value & "<br>" & "Doubling Services: " & Range("T" & i).Value & "<br>" & "Move Up Services: " & Range("W" & i).Value & " services from " & Range(Y, i) & "<br>" & "Solo Services: " & Range(Z, i).Value & "<br><br>" _
& Range(AA, 1).Value & "<br>" & "Services Played: " & Range(AA, i).Value & "<br>" & "Doubling Services: " & Range(AB, i).Value & "<br>" & "Move Up Services: " & Range(AE, i).Value & " services from " & Range(AG, i) & "<br>" & "Solo Services: " & Range(AH, i).Value & "<br><br>" _
& "Pay Period Totals" & "<br>" & "Total Leave Used: " & Range(F, i).Value & "<br>" & "Sick Leave Used: " & Range(i, i).Value & "<br>" & "Total Doubling Pay: " & Range(K, i).Value & "<br>" & "Total Move Up Pay: " & Range(L, i).Value & "<br>" & "Total Solo Pay: " & Range(M, i).Value & "<br>" & "Total Pay Correction: " & Range(N, i).Value & "<br>" & "Parking Reimbursement: " & Range(O, i).Value & "<br>" & "Mileage Reimbursement: " & Range(P, i).Value & "<br>" & "Travel Reimbursement: " & Range(Q, i).Value & "<br>" & "Total Additional Pay: " & Range(R, i).Value & "<br><br>" _
& "Season Totals" & "<br><br>" & "Total Season Services Used: " & Range(AZ, i).Value & "<br>" & "Sick Leave Remaining: " & Range(AY, i).Value & "<br><br>" & "Please let me know if you have any questions or concerns." & "<br><br>" & "Best, "
.Body = "Hi " & Range("C" & i).Value & vbNewLine & vbNewLine & "Please see your Service Insert Below." & vbNewLine & vbNewLine _
& Range("S1").Value & vbNewLine & "Services Played: " & Range("S" & i).Value & vbNewLine & "Doubling Services: " & Range("T" & i).Value & vbNewLine & "Move Up Services: " & Range("W" & i).Value & " services from " & Range("Y" & i) & vbNewLine & "Solo Services: " & Range("Z" & i).Value & vbNewLine & vbNewLine _
& Range("AA1").Value & vbNewLine & "Services Played: " & Range("AA" & i).Value & vbNewLine & "Doubling Services: " & Range("AB" & i).Value & vbNewLine & "Move Up Services: " & Range("AE" & i).Value & " services from " & Range("AG" & i) & vbNewLine & "Solo Services: " & Range("AH" & i).Value & vbNewLine & vbNewLine _
& "Pay Period Totals" & vbNewLine & "Total Leave Used: " & Range("F" & i).Value & vbNewLine & "Sick Leave Used: " & Range("I" & i).Value & vbNewLine & "Total Doubling Pay: " & Range("K" And i).Value & vbNewLine & "Total Move Up Pay: " & Range("L" And i).Value & vbNewLine & "Total Solo Pay: " & Range("M" And i).Value & vbNewLine & "Total Pay Correction: " & Range("N" And i).Value & vbNewLine & "Parking Reimbursement: " & Range("O" And i).Value & vbNewLine & "Mileage Reimbursement: " & Range("P" And i).Value & vbNewLine & "Travel Reimbursement: " & Range("Q" And i).Value & vbNewLine & "Total Additional Pay: " & Range("R" And i).Value & vbNewLine & vbNewLine _
& "Season Totals" & vbNewLine & vbNewLine & "Total Season Services Used: " & Range("AZ" & i).Value & vbNewLine & "Sick Leave Remaining: " & Range("AY" & i).Value & vbNewLine & vbNewLine & "Please let me know if you have any questions or concerns." & vbNewLine & vbNewLine & "Best, "
'.Attachments.Add ActiveWorkbook.FullName
.Display 'or use .Send
End With
On Error GoTo 0
This is the Body and HTMLBody I tried to make work, neither is sending any information, even failed cell searches to outlook.
I can show more of my the code if needed, for readability I hope this is enough.
Create a variable to create your body string by making some concatenations then assign the string to .Body. This way you can easily check your string and find an error by running step by step.
Dim emailBody as string: emailBody = ""
emailBody = emailBody & "Hi " & Range("C" & i).Value & vbNewLine & vbNewLine
emailBody = emailBody & "Please see your Service Insert Below." & vbNewLine & vbNewLine
emailBody = emailBody & Range("S1").Value & vbNewLine
...
With OutMail
...
.Body = emailBody
...
End With
Original Query that works
CSQL2 = CSql1 & vbNewLine & _
"SELECT CASE WHEN TRIM ( y.fbn ) LIKE '%ABC%' THEN ( bd.build_id ) ELSE TRIM ( y.fbn ) END AS fbn, y.number, y.title, y.owner, y.type, y.open_closed," & vbNewLine & _
"y.date_approved, y.date_submitted_for_approval, y.expires_after, y.status, y.reg," & vbNewLine & _
"y.site, y.lines, y.cap, y.committed, y.invoiced, CASE" & vbNewLine & _
"WHEN y.Lines = 'Electrical' THEN 'Electrical_Equipment'" & vbNewLine & _
"WHEN y.Lines = 'Mechanical' THEN 'Mechanical_Equipment'" & vbNewLine & _
"WHEN y.Lines = 'Construction' THEN 'Base'" & vbNewLine & _
"ELSE y.Lines END category, " & vbNewLine & _
"CASE WHEN y.Lines = 'Electrical' THEN 'Other' " & vbNewLine & _
"WHEN y.Lines = 'Mechanical' THEN 'Other' " & vbNewLine & _
"WHEN y.Lines = 'Construction' THEN 'Base' ELSE y.Lines " & vbNewLine & _
"END subcategory " & vbNewLine & _
"FROM s1.line_new y LEFT JOIN (SELECT build_id, fbn FROM bd) bd ON bd.fbn = y.fbn " & vbNewLine & _
"WHERE Lines <> 'NW' AND ( y.Cap <> 0 OR y.Committed <> 0 OR y.Invoiced <> 0 )" & vbNewLine & _
"AND y.fbn IN ( SELECT DISTINCT fbn FROM bd )" & vbNewLine & _
"AND y.number NOT IN (SELECT num FROM s1.att) AND" & vbNewLine & CARFilters
New query that doesn't work to refresh the list object:
CSql2 = CSql1 & vbNewLine & _
"SELECT CASE WHEN TRIM ( bd.fbn ) LIKE '%ABC%' THEN ( bd.build_id ) ELSE TRIM ( bd.fbn ) END AS fbn, y.number, y.title, y.owner, y.type, y.open_closed," & vbNewLine & _
"y.date_approved, y.date_submitted_for_approval, y.expires_after, y.status, y.reg," & vbNewLine & _
"y.site, y.lines, ROUND(SUM(y.capex) OVER ( PARTITION BY y.fbn, lines ),2) AS cap, y.committed, y.invoiced, CASE" & vbNewLine & _
"WHEN y.Lines = 'Electrical' THEN 'Electrical_Equipment'" & vbNewLine & _
"WHEN y.Lines = 'Mechanical' THEN 'Mechanical_Equipment'" & vbNewLine & _
"WHEN y.Lines = 'Construction' THEN 'Base'" & vbNewLine & _
"ELSE y.Lines END category, " & vbNewLine & _
"CASE WHEN y.Lines = 'Electrical' THEN 'Other' " & vbNewLine & _
"WHEN y.Lines = 'Mechanical' THEN 'Other' " & vbNewLine & _
"WHEN y.Lines = 'Construction' THEN 'Base' ELSE y.Car_Lines " & vbNewLine & _
"END subcategory ,mcl.currency_id mcl_currency ,fx_2.local_currency ,fx_2.conversion_rate 2_fx_rate " & vbNewLine & _
",mcl.exchange_rate_effective_date,ROUND(fx.rate,4) conversion_rate " & vbNewLine & _
",ROUND(SUM(y.cap*fx.rate) OVER ( PARTITION BY y.fbn, lines ),2) AS cap_local " & vbNewLine & _
"FROM s1.mcl_carline_new y LEFT JOIN (SELECT build_id, fbn FROM bd) bd ON bd.fbn = y.fbn " & vbNewLine & _
"LEFT JOIN (SELECT number, exchange_rate_effective_date, currency_id FROM s1.mcl_new) mcl ON y.number = mcl.number " & vbNewLine & _
"LEFT JOIN s1.fx_rate_2 fx_2 ON fx_2.reg = y.reg " & vbNewLine & _
"LEFT JOIN s2.prcmt_exchange_rate fx ON fx_2.local_currency = fx.to_currency_id " & vbNewLine & _
"AND mcl.currency_id = fx.from_currency_id AND mcl.exchange_rate_effective_date = fx.effective_date " & vbNewLine & _
"WHERE Lines <> 'NW' AND ( y.Cap <> 0 OR y.Committed <> 0 OR y.Invoiced <> 0 ) " & vbNewLine & _
"AND y.fbn IN ( SELECT DISTINCT fbn FROM bd ) " & vbNewLine & _
"AND y.number NOT IN (SELECT num FROM s1.att) AND " & vbNewLine & CARFilters
Listobject Refresh section
With CData.ListObjects.Add(SourceType:=0, Source:=CS, Destination:=CData.Range("$A$1")).QueryTable
.CommandText = CSql2
.RefreshStyle = xlInsertDeleteCells
.AdjustColumnWidth = True
.ListObject.DisplayName = "CData"
.Refresh BackgroundQuery:=False
End With
During this LO existed and I had to modify the query, which added some columns; this refresh worked find until the columns got added. Now I get a 1004 General OBDC, but when I Debug.Print the query and run it AS IS in my SQL client it works without issue.
How can I tell what is causing the 1004? Note - CSql1 is unchanged.
12 hours of working on this and less than an hour after the bounty; I find the issue. Someone had removed permissions for the user running the query on the table required to retrieve the results.
I am sending mail linked to Excel data. i need to bold and red only few words and i am trying and i am not able to do it. Hope someone from here can help me out. Cells(i,13) has to get bold and Red when i am sending.
toList = Cells(i, 4) 'gets the recipient from col D
eSubject = "Request for KBR transport to " & Cells(i, 2) & " on " & Format(Cells(i, 3), "dd-mmm-yy")
eBody = "Dear " & Cells(i, 1) & vbCrLf & vbCrLf & "Can you please arrange KBR Transport for below PASSENGERS." & vbNewLine & vbNewLine & _
" " & Cells(i, 13) & vbNewLine & vbNewLine & _
" " & vbNewLine & vbNewLine & _
"Full Name : " & Cells(i, 6) & vbNewLine & vbNewLine & _
"Nationality : " & Cells(i, 7) & vbNewLine & vbNewLine & _
"Departure/Arrival Date : " & "" & Format(Cells(i, 8), "dd-mmm-yy") & vbNewLine & vbNewLine & _
"Airline : " & Cells(i, 9) & vbNewLine & vbNewLine & _
"Flight Number : " & Cells(i, 10) & vbNewLine & vbNewLine & _
"Departure/Arrival Time : " & Cells(i, 11) & vbNewLine & vbNewLine & _
"Escort required: " & Cells(i, 14) & vbNewLine & vbNewLine & _
" " & vbNewLine & vbNewLine & _
" Contact Number: " & Cells(i, 12) & vbNewLine & vbNewLine & _
" " & vbNewLine & vbNewLine & _
"Please confirm pick up time and availability." & vbNewLine & vbNewLine ##
Use HTML body instead, then you can use the HTML <b> and <font> tags. It may suit better as you can use a HTML <table> for the data displayed also.
Can someone help me out here with formatting date when I am sending email? In Cell 8 I have date but when I'm sending email date will show as 10/03/2016 but I wanted to show date as 03-Oct-2016. I tried putting code as & Format(Date, "dd/mmm/yy") but when I run the program it will show today's date next to 10/03/2016.
Please see below my coding:
## "Full Name : " & Cells(i, 6) & vbNewLine & vbNewLine & _
"Nationality : " & Cells(i, 7) & vbNewLine & vbNewLine & _
"Departure/Arrival Date : " & Cells(i, 8) & vbNewLine & vbNewLine & _
"Airline : " & Cells(i, 9) & vbNewLine & vbNewLine & _
"Flight Number : " & Cells(i, 10) & vbNewLine & vbNewLine & _
"Departure/Arrival Time : " & Cells(i, 11) & vbNewLine & vbNewLine & _
"Escort required: " & Cells(i, 14) & vbNewLine & vbNewLine & _
" " & vbNewLine & vbNewLine & _
" Contact Number: " & Cells(i, 12) & vbNewLine & vbNewLine & _ ##
Change
"Departure/Arrival Date : " & Cells(i, 8) & vbNewLine & vbNewLine & _
to
"Departure/Arrival Date : " & Format(Cells(i, 8), "dd-mmm-yyyy") & vbNewLine & vbNewLine & _
The cells which the code is pointing (b2, b3 ..) are already showing up 2 decimal houses, but the msg box shows many . How can I fix it? I had an attempt with & _
Format(dTotalArea, "0.00"), but didnt work.
Many thanks in advance.
MsgBox "First Response Time (hours)= " & _
Worksheets("Results").Range("B2").Value & _
vbCrLf & "Investigation Time (hours)= " & _
Worksheets("Results").Range("B3").Value & _
vbCrLf & "Nr of Incidents Resolved= " & _
Worksheets("Results").Range("B4").Value & _
vbCrLf & "Resolved Same Day (%)= " & _
Worksheets("Results").Range("B5").Value & _
Format(dTotalArea, "0.00"), , "title_of_msgbox"
Try this
MsgBox "First Response Time (hours)= " & _
Worksheets("Sheet1").Range("B2").Value & _
vbCrLf & "Investigation Time (hours)= " & _
Format(Worksheets("Sheet1").Range("B3").Value, "0.00") & _
vbCrLf & "Nr of Incidents Resolved= " & _
Format(Worksheets("Sheet1").Range("B4").Value, "0.00") & _
vbCrLf & "Resolved Same Day (%)= " & _
Format(Worksheets("Sheet1").Range("B5").Value, "0.00") _
, , "title_of_msgbox"