PC Review


Reply
Thread Tools Rate Thread

Concatenation in MsgBox

 
 
katjambor
Guest
Posts: n/a
 
      15th Aug 2009
Excel 2007 - this works just as I expect it to, but I would like to do a
little more:

Sub HisSales()
Dim intSales As Integer
intSales = Range("O3")
Range("O3").Select
If ActiveCell.Value = 0 Then
If MsgBox("He has no sales this period.", vbOKOnly, "No sales!") =
vbOKOnly Then
End If
ElseIf MsgBox("He has " & intSales, vbOKOnly, "His sales!") = vbOK
Then
End If
End Sub

Cell O3 contains a CountIf formula which tells how many sales he has made
this period, and since he currently has 9, running the code displays a
message box that says, "He has 9". I would like it to say, "He has 9 sales
this period." but can't figure out how to put more text after intSales.
 
Reply With Quote
 
 
 
 
Chip Pearson
Guest
Posts: n/a
 
      15th Aug 2009
A few things come to mind. First, declare intSales as Long rather than
Integer, just in case the value is greater the 32K. Next, you don't
seem to be doing anything with the result of the MsgBox (the value of
the button clicked), so you can streamline that. Finally, there is no
need to select O3 and use ActiveCell. Try...


Sub HisSales()
Dim intSales As Long
intSales = Range("O3").Value
If intSales = 0 Then
MsgBox "No Sales", vbOKOnly
Else
MsgBox "Has Sales of: " & _
Format(intSales, "#,##0"), vbOKOnly, "Has Sales"
End If
End Sub



Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)



On Sat, 15 Aug 2009 13:12:01 -0700, katjambor
<(E-Mail Removed)> wrote:

>Excel 2007 - this works just as I expect it to, but I would like to do a
>little more:
>
>Sub HisSales()
>Dim intSales As Integer
> intSales = Range("O3")
> Range("O3").Select
> If ActiveCell.Value = 0 Then
> If MsgBox("He has no sales this period.", vbOKOnly, "No sales!") =
>vbOKOnly Then
> End If
> ElseIf MsgBox("He has " & intSales, vbOKOnly, "His sales!") = vbOK
>Then
> End If
> End Sub
>
>Cell O3 contains a CountIf formula which tells how many sales he has made
>this period, and since he currently has 9, running the code displays a
>message box that says, "He has 9". I would like it to say, "He has 9 sales
>this period." but can't figure out how to put more text after intSales.

 
Reply With Quote
 
Matthew Herbert
Guest
Posts: n/a
 
      15th Aug 2009
On Aug 15, 2:12*pm, katjambor <katjam...@discussions.microsoft.com>
wrote:
> Excel 2007 - this works just as I expect it to, but I would like to do a
> little more:
>
> Sub HisSales()
> Dim intSales As Integer
> * * intSales = Range("O3")
> * * Range("O3").Select
> * * If ActiveCell.Value = 0 Then
> * * * * If MsgBox("He has no sales this period.", vbOKOnly, "No sales!") =
> vbOKOnly Then
> * * * * End If
> * * * * ElseIf MsgBox("He has " & intSales, vbOKOnly, "His sales!") = vbOK
> Then
> * * * * End If
> * * End Sub
>
> Cell O3 contains a CountIf formula which tells how many sales he has made
> this period, and since he currently has 9, running the code displays a
> message box that says, "He has 9". *I would like it to say, "He has 9 sales
> this period." but can't figure out how to put more text after intSales.


katjambor,

You concatenated the first part, so you can use the same method, i.e.
"He has " & intSales & " this period.". Also, I'm not sure why you
have the MsgBox embedded in the If Then statement, especially since
nothing is done regardless of the outcome. You can change this to be
something like the following:

If ActiveCell.Value = 0 Then
MsgBox "He has no sales this period.", vbOKOnly, "No sales!"
Else
MsgBox "He has " & intSales & " this period.", vbOKOnly, "His
sales!"
End If

Best,

Matthew Herbert
 
Reply With Quote
 
katjambor
Guest
Posts: n/a
 
      16th Aug 2009
Thanks, Matthew - I kept trying to do just that but must have had a comma out
of place or such because I kept getting errors! I've just been looking at
this project to long I guess.

There are things happening after this code, BTW, but these lines were giving
me the issue. I'm self-teaching here & stumbling a bit!

"Matthew Herbert" wrote:

> On Aug 15, 2:12 pm, katjambor <katjam...@discussions.microsoft.com>
> wrote:
> > Excel 2007 - this works just as I expect it to, but I would like to do a
> > little more:
> >
> > Sub HisSales()
> > Dim intSales As Integer
> > intSales = Range("O3")
> > Range("O3").Select
> > If ActiveCell.Value = 0 Then
> > If MsgBox("He has no sales this period.", vbOKOnly, "No sales!") =
> > vbOKOnly Then
> > End If
> > ElseIf MsgBox("He has " & intSales, vbOKOnly, "His sales!") = vbOK
> > Then
> > End If
> > End Sub
> >
> > Cell O3 contains a CountIf formula which tells how many sales he has made
> > this period, and since he currently has 9, running the code displays a
> > message box that says, "He has 9". I would like it to say, "He has 9 sales
> > this period." but can't figure out how to put more text after intSales.

>
> katjambor,
>
> You concatenated the first part, so you can use the same method, i.e.
> "He has " & intSales & " this period.". Also, I'm not sure why you
> have the MsgBox embedded in the If Then statement, especially since
> nothing is done regardless of the outcome. You can change this to be
> something like the following:
>
> If ActiveCell.Value = 0 Then
> MsgBox "He has no sales this period.", vbOKOnly, "No sales!"
> Else
> MsgBox "He has " & intSales & " this period.", vbOKOnly, "His
> sales!"
> End If
>
> Best,
>
> Matthew Herbert
>

 
Reply With Quote
 
katjambor
Guest
Posts: n/a
 
      16th Aug 2009
Thanks, Chip - I'm sure there are lots of things I SHOULD be doing to make my
code slicker. This is really my first foray into Excel programming, I guess
Access has spoiled me a little!

BTW, the boss recently sent us to an Excel Techniques class and you were
mentioned more than once in glowing terms!

"Chip Pearson" wrote:

> A few things come to mind. First, declare intSales as Long rather than
> Integer, just in case the value is greater the 32K. Next, you don't
> seem to be doing anything with the result of the MsgBox (the value of
> the button clicked), so you can streamline that. Finally, there is no
> need to select O3 and use ActiveCell. Try...
>
>
> Sub HisSales()
> Dim intSales As Long
> intSales = Range("O3").Value
> If intSales = 0 Then
> MsgBox "No Sales", vbOKOnly
> Else
> MsgBox "Has Sales of: " & _
> Format(intSales, "#,##0"), vbOKOnly, "Has Sales"
> End If
> End Sub
>
>
>
> Cordially,
> Chip Pearson
> Microsoft Most Valuable Professional
> Excel Product Group, 1998 - 2009
> Pearson Software Consulting, LLC
> www.cpearson.com
> (email on web site)
>
>
>
> On Sat, 15 Aug 2009 13:12:01 -0700, katjambor
> <(E-Mail Removed)> wrote:
>
> >Excel 2007 - this works just as I expect it to, but I would like to do a
> >little more:
> >
> >Sub HisSales()
> >Dim intSales As Integer
> > intSales = Range("O3")
> > Range("O3").Select
> > If ActiveCell.Value = 0 Then
> > If MsgBox("He has no sales this period.", vbOKOnly, "No sales!") =
> >vbOKOnly Then
> > End If
> > ElseIf MsgBox("He has " & intSales, vbOKOnly, "His sales!") = vbOK
> >Then
> > End If
> > End Sub
> >
> >Cell O3 contains a CountIf formula which tells how many sales he has made
> >this period, and since he currently has 9, running the code displays a
> >message box that says, "He has 9". I would like it to say, "He has 9 sales
> >this period." but can't figure out how to put more text after intSales.

>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MsgBox - Display variable in MsgBox Shiller Microsoft Access VBA Modules 2 29th Sep 2008 10:14 PM
Substituting my own MsgBox for the standard delete confirm MsgBox John S. Ford, MD Microsoft Access Getting Started 8 20th Aug 2004 12:05 AM
Substituting my own MsgBox for the standard delete confirm MsgBox John S. Ford, MD Microsoft Access Forms 8 20th Aug 2004 12:05 AM
Substituting my own MsgBox for the standard delete confirm MsgBox John S. Ford, MD Microsoft Access 8 20th Aug 2004 12:05 AM
Substituting my own MsgBox for the standard delete confirm MsgBox John S. Ford, MD Microsoft Access Form Coding 8 20th Aug 2004 12:05 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:22 AM.