Format vba msgbox to show cells with two decimal houses - excel

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"

Related

1004 ODBC General Error when columns are added to a query

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.

Humble business student - msgbox failing - SOS

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")

Excel VBA - String padding not working

I have a VBA function that returns a lot of information about the computer it's running on. I'd like to format the output neatly, with the titles left-justified and the results all tabbed on a neat line to the right.
Here's what I thought would work, and this is the solution on many sites I've consulted:
AllInfo = _
"Date/Time Opened:" & Space(33 - Len("Date/Time Opened:")) & Now() & vbCrLf & _
"Filename:" & Space(33 - Len("Filename:")) & Application.ActiveWorkbook.Name & vbCrLf & _
"Name in Cell B2:" & Space(33 - Len("Name in Cell B2:")) & Cells(2, 2).Value & vbCrLf & _
But the output, in both a message box and the body of an email, is jagged. The titles are all lined up on the left, of course, but the results aren't lined up nicely.
Can anyone see what I'm doing wrong?
This works:
Sub djfsdf()
allinfo = _
"Date/Time Opened:" & Space(33 - Len("Date/Time Opened:")) & Now() & vbCrLf & _
"Filename:" & Space(33 - Len("Filename:")) & Application.ActiveWorkbook.Name & vbCrLf & _
"Name in Cell B2:" & Space(33 - Len("Name in Cell B2:")) & Cells(2, 2).Value
Cells(3, 3) = allinfo
End Sub
with the proper font:
You are not including the string vars in the calculation for the number of spaces to add.
AllInfo = _
"Date/Time Opened:" & Space(33 - Len("Date/Time Opened:" & Now())) & Now() & vbLf & _
"Filename:" & Space(33 - Len("Filename:" & Application.ActiveWorkbook.Name)) & Application.ActiveWorkbook.Name & vbLf & _
"Name in Cell B2:" & Space(33 - Len("Name in Cell B2:" & Cells(2, 2).Value)) & Cells(2, 2).Value & vbLf & _
(etc)
I've also change your vbCRLF to just vbLF; that may or may not be correct.

DSAL query for Unread mail in the filter for Outlook using Inbox.Items.Restrict

I am writing a code for automatically downloading an attachment from unread mail containing several keywords in the subject for example "training" but when I tried using SQL query for unread mail, it's giving me an error.
Filter = "#SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & _
Chr(34) & " Like '%Training%' AND " & _
Chr(34) & "urn:schemas:httpmail:hasattachment" & _
Chr(34) & "=1"
Set Items = Inbox.Items.Restrict(Filter) 'No error while running this code
Filter = "#SQL=" & Chr(34) & "& Chr(34) & "urn:schemas:httpmail:subject" & _
Chr(34) & " Like '%Training%' AND" & _
Chr(34) & "urn:schemas:httpmail:hasattachment" & _
Chr(34) & "= 1" & Chr(34) & "AND" & _
Chr(34) & "urn:schemas:httpmail:read" & _
Chr(34) & "= 0"
Set Items = Inbox.Items.Restrict(Filter)
' Now here it is giving me runtime error '-2147352567(800200009)'
All the help will be appreciated. Thanks in Advance
You almost got it,
The Error is coming from here Chr(34) & "= 1" & Chr(34) & "AND" & _
It Should be Chr(34) & "=1 AND " & _
Example
Filter = "#SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & _
Chr(34) & " Like '%Training%' AND " & _
Chr(34) & "urn:schemas:httpmail:hasattachment" & _
Chr(34) & "=1 AND " & _
Chr(34) & "urn:schemas:httpmail:read" & _
Chr(34) & "=0"
Filtering Items Using a String Comparison that DASL filters support includes equivalence, prefix, phrase, and substring matching. Note that when you filter on the Subject property, prefixes such as "RE: " and "FW: " are ignored.

Can I format Strings in VB6 with rich text commands?

I need to add a string to an email in richtext format. I cannot format the text out of a textbox by selecting it as I usually do. Can I just modify the string? And how could I do that?
Currently the text was formatted in HTML but that isn't an option anymore. Looks something like this
string = vbCrLf & vbCrLf & "<B><FONT face=Arial color=#365f91 size=2>" & _
strName & "</FONT>" & _
"<FONT face=Arial size=2><BR>" & _
"<I>" & strPosition & "</I>" & _
"</FONT></B><BR/><B><FONT face=Arial size=2>" & _
strAddress1 & "</FONT></B><BR/><FONT face=Arial size=1>" & _
strAddress2 & "<BR>" & strCity & ", " & _
strProvince & ", " & strPostalCode & _
"<BR>" & strCountry & "<BR>Office: " & strPhone & _
"<BR>" & strEmail & "<BR>www.website.com<BR>" & _
" " & _
"<I><FONT face=Arial size=2>" & strImageCaption & "</FONT>" & _
"<BR/><BR/><BR/></I><FONT face=Arial size=1>" & strDisclaimer & _
"</FONT></P>"
I can't find any resources online about formatting rich text that isn't currently in a rich text box so I think maybe it isn't possible.

Resources