msgbox with cell reference

L

Legacy 287389

Guest
Hi good people!,

I found tons of examples on the net but not one where the cell value to be displayed in the msgbox, is the is in the beginning of the code. I have this:
Code:
Sheets("Scheduler").Activate
MsgBox (Range("AB2").Value & " Records Found And Printed")

The message box does not show the value in cell AB2. it only returns
Records Found And Printed
.

thanx for the help!
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Try....but make sure there is actually something in "AB2"

Code:
MsgBox (Sheets("Scheduler").Range("AB2").Value & " Records Found And Printed")
 
Upvote 0
Try:

Rich (BB code):
Sub example()
  MsgBox ThisWorkbook.Worksheets("Scheduler").Range("AB2").Value & " some text"
End Sub

Hope that helps,

Mark
 
Upvote 0
Hi juriemagic,

I failed to mention that I would suggest against including the parentheses unless you using the return value of the MsgBox, like:

Rich (BB code):
Option Explicit
  
Sub example()
Dim RetVal As VbMsgBoxResult
  
  RetVal = MsgBox(ThisWorkbook.Worksheets("Scheduler").Range("AB2").Value & " some text", vbYesNo Or vbQuestion, "Do we want to?")
  
  If RetVal = vbYes Then
    MsgBox "Do something here..."
  End If
  
  'Or... just use the return of the MsgBox directly, like
  
  If MsgBox(ThisWorkbook.Worksheets("Scheduler").Range("AB2").Value & " some text", vbYesNo Or vbQuestion, "Do we want to?") = vbYes Then
    MsgBox "Do something here..."
  End If
  
End Sub


For an explanation as to the whys, check out: Quick VBA Tip: Parentheses | Daily Dose of Excel

In particular, reading Rick Rothstein's comments at about post #6.

Hope that helps,

Mark
 
Upvote 0
GTO,

thanx for the extra info, the message box return info is purely for confirming how many sheets had been printed. it's not really used for anything else...thanx a lot..
 
Upvote 0

Forum statistics

Threads
1,215,062
Messages
6,122,925
Members
449,094
Latest member
teemeren

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top