View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein \(MVP - VB\) Rick Rothstein \(MVP - VB\) is offline
external usenet poster
 
Posts: 2,202
Default Two short questions

You will have trouble getting things to align the way you want in a MsgBox
as the font used by the system is not a fixed-width font; hence, spaces have
a different width than non-spaces do. Another possibility that you can
consider is using a UserForm and constructing a fake message box in which
the text is displayed using a fixed-width font (Courier New for this
example). Here is a fully hard-coded sample for you to how to implement the
concept (you can use it as a guide in making a more flexible one). In the
VBA editor, insert a Module into your project add put this code in its code
window...

'****** Start Module Code ******
Public dVar1 As Double
Public dVar2 As Double
'****** End User Form Code ******

Next, insert a UserForm into your project and put a Label control and a
CommandButton on it. Then copy/paste this code into its code window...

'****** Start User Form Code ******
Private Sub CommandButton1_Click()
Unload Me
End Sub

Private Sub UserForm_Initialize()
Dim sFmt As String
Dim fmtDVar As String
Dim fmtDVar1 As String
Dim fmtDVar2 As String
Dim MaxLength As Long
sFmt = "#,##0.00"
fmtDVar1 = Format$(dVar1, sFmt)
fmtDVar2 = Format$(dVar2, sFmt)
fmtDVar = Format$(dVar1 + dVar2, sFmt)
sFmt = String(Len(fmtDVar), "@")
With CommandButton1
.Caption = "OK"
.Move 59.2, 99, 51, 24
End With
With Me
.Caption = "Totals"
.Height = 152.25
.Width = 176.25
End With
With Label1
.Move 9.75, 11.25, 150, 75.75
.Font.Name = "Courier New"
.Font.Bold = True
.Font.Size = 10
.Caption = "EMS Job 1: $" & Format(fmtDVar1, sFmt) & vbLf & _
"EMS Job 2: $" & Format(fmtDVar2, sFmt) & vbLf & _
" " & String(Len(fmtDVar) + 1, "=") & vbLf & _
"TOTAL: $" & Format(fmtDVar, sFmt)
End With
End Sub
'****** End User Form Code ******

Finally, double-click the worksheet name for the worksheet you want to get
your values from (assumed to be A1 and B1 for this example) in the Project
Explorer window (Ctrl+R if it is not showing) and copy/paste this code into
the code window that appeared when you double clicked it...

'****** Start Worksheet Code ******
Sub ShowPrices()
dVar1 = CDbl(ActiveSheet.Range("A1").Value)
dVar2 = CDbl(ActiveSheet.Range("B1").Value)
UserForm1.Show
End Sub
'****** End Worksheet Code ******

Now, go to this worksheet and type a value into both A1 and B1. Then, press
Alt+F8, select the ShowPrices macro from the list and Run it. Try other
values in A1 and B1 and run the macro to see how the display adapts to your
entered numbers. This macro can eventually be assigned to a some action
initiated from the worksheet (a button, a right-click event on the
worksheet, or some other method).

Rick


"WLMPilot" wrote in message
...
Thanks. Worked great. However I have two more questions in order to
tweak
the display.

1) the format I used ("$##0.00) causes the numbers to not line up. If
there is no money made on a job, then I get $0.00, when I need $ 0.00.
How
can I get the spaces in there? I tried "$000.00" also.

2). I want to underline between 2nd line and Total. How do I get the
underline in there?

Thanks again!!

Les

"Ron Rosenfeld" wrote:

On Sun, 27 Jan 2008 10:43:01 -0800, WLMPilot
wrote:

1) How is a variable DIM if it might hold decimal places?


DIM dNum as Double



2) Ref Msgbox, how do I show multiple lines within Msgbox?


Separate the lines with vbLf

I have two
part-time jobs and I want to display the estimated pay for each for
current
payperiod and then a total. I can get it on one line, but I would like
to
show each job on separate line and lined up so I can display total
amount too.

EXAMPLE: EMS Job1: $100.00
EMS Job2: $235.00
TOTAL: $335.00


For example:

================================
Option Explicit
Sub foo()
Const dVar1 As Double = 100
Const dVar2 As Double = 235
Const sFmt As String = "$#,##0.00"

MsgBox ("EMS Job 1: " & Format(dVar1, sFmt) & _
vbLf & "EMS Job 2: " & Format(dVar1, sFmt) & _
vbLf & "TOTAL: " & Format(dVar1 + dVar2, sFmt))

End Sub
====================================

--ron