Putting contents of a cell in a Message Box

Jimmy P

New Member
Joined
Aug 23, 2014
Messages
45
I want to use a Message Box to display the contents of a worksheet cell in between a text string. I keep getting an error "Compile Error" "Syntax Error".

MsgBox "Sample # Sheets("Calc").Range("A2").value "Loaded" & vbNewLine & "Click OK to Load", & vbNewLine, "Next Sample", vbONnly, "Load Complete"

If I remove Sheets("Calc").Range("A2").value it works OK to display the text strings.

HELP!!!!!
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
I want to use a Message Box to display the contents of a worksheet cell in between a text string. I keep getting an error "Compile Error" "Syntax Error".

MsgBox "Sample # Sheets("Calc").Range("A2").value "Loaded" & vbNewLine & "Click OK to Load", & vbNewLine, "Next Sample", vbONnly, "Load Complete"

If I remove Sheets("Calc").Range("A2").value it works OK to display the text strings.
I think this is what you want...

MsgBox "Sample # " & Sheets("Calc").Range("A2").Value & "Loaded" & vbNewLine & "Click OK to Load" & vbNewLine, "Next Sample", vbONnly, "Load Complete"
 
Upvote 0
No joy! Now I get a "Type Mismatch" error. Here the whole Sub. Range "A2" is a number that currently = 1 so the MsgBox should display a "1".

Sub LoadNextSample()
'
Sheets("Samples").Select
'
If Sheets("Calc").range("A2:A2") > 0 Then ' tests value to see if sample count > 0
Sheets("Samples").Select ' selects Sheet Samples
range("B1:B1").Select ' selects Range B1
Application.Goto (ActiveWorkbook.Sheets("Samples").range("B1:B1").Offset(0, Sheets("Calc").range("A2")))
'Offsets from B1 by value in cell A2, which is current sample #

End If
If ActiveCell > Sheets("Calc").Range("TSam") Then ' tests to see if CurrentCell is > Max cells in Row 1:
Sheets("Calc").Select
MsgBox "Sample Run Completed" & vbNewLine & "Click OK to Continue", vbONOnly, "End of Samples" 'If CurrentCell is = TSam, put up MsgBox
End If

Range("B2:B2").Select
Application.Goto (ActiveWorkbook.Sheets("Samples").range("B2:B20").Offset(0, Sheets("Calc").range("A2"))) ' Offset by Value of A2
Selection.Copy
Sheets("Calc").Select
Range("B2:B20").Select
ActiveSheet.Paste ' Pastes calculated results from Calc
Sheets("Samples").Select
Range("B1:B1").Select
Application.Goto (ActiveWorkbook.Sheets("Samples").range("B1:B1").Offset(0, Sheets("Calc").range("A2"))) ' Offsets Value of A2
Selection.Copy
Sheets("Calc").Select
Range("A2").Select
ActiveSheet.Paste

" Crashes here with "Type Mismatch" error
MsgBox "Sample #" & Sheets("Calc").range("A2").Value & "Loaded" & vbNewLine & "Click OK to Load", vbNewLine, "Next Sample", vbONnly, "Load Complete"

End Sub
 
Upvote 0
" Crashes here with "Type Mismatch" error
MsgBox "Sample #" & Sheets("Calc").range("A2").Value & "Loaded" & vbNewLine & "Click OK to Load", vbNewLine, "Next Sample", vbONnly, "Load Complete"
What is the red highlighted text supposed to be? It starts with "vb" so it looks like it is supposed to be a built-in VB constant, but VB does not have one with that name.
 
Upvote 0
Put "OK" in MsgBox. Works great. I'm using Excel V 2007.
On my system, the argument to display the OK button is vbOKOnly (not vbNnly as you spelled it). In any event, this works properly for me...

MsgBox "Sample #" & Sheets("Calc").Range("A2").Value & "Loaded" & vbNewLine & "Click OK to Load" & vbNewLine & "Next Sample", vbOKOnly, "Load Complete"
 
Last edited:
Upvote 0
Works great. Many thanks for your quick and precise solution. Goes in my VB crib notes. I'm learning VBA by trial and error.

Made a typo on vbOKOnly. After mucho hours on this project I'd reached the point where my Brain didn't accurately process what my eyes saw.

Used to live in Sparta (Lake Mohawk), NJ. Loved it except for the winters. Brrrrr!
 
Upvote 0

Forum statistics

Threads
1,223,277
Messages
6,171,156
Members
452,385
Latest member
Dottj

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