Message box [how it works??]

F

furbiuzzu

i've inserted a toggle button in my excel sheet (sheet "ratings").
i want, by clickin' on it, a message box to appear.
the text of this message box is allocated on a cell of a different
sheet (named: labels) of the same excel file.

if i put these lines on togglebutton code:

Private Sub ToggleButton1_Click()
text = Range("labels!A980").Value
MsgBox (text)
End Sub

it doesn't work!

if i write a macro named 'totali'
and associate it to toggle button code it works. why??

here's the effective code that works :

Sub totali()
Dim testo As String
text = Range("labels!A980").Value
MsgBox (text)
End Sub

Private Sub ToggleButton1_Click()
Application.Run "My.T.y.M.xls!totali"
End Sub

thanks !
 
B

Bob Phillips

Because you have used a button from the Forms toolbar. The Click macro
applies to a control toolbox button.
 
F

furbiuzzu

thanks 4 answering.

but i'm a newenewnewbie in programming with VB.
there's not a way to disply the msgbox i want with just one click on a
toggle button ?
 
H

Harald Staff

Rightclick the button, choose "assign macro" and assign the Totali macro to
it.

HTH. Best wishes Harald
 
D

Dave Peterson

I think you used the correct togglebutton for your code (from the Control
toolbox toolbar, right?).

But I think your code is confusing excel.

Try this:

Option Explicit
Private Sub ToggleButton1_Click()
Dim myText As Variant
myText = Worksheets("labels").Range("a980").Value
MsgBox myText
End Sub

(Text is a property that VBA uses. I try to stay away from them--that confuses
me--even if VBA can figure it out.)

But this line:

text = Range("labels!A980").Value

has an unqualifed range object. In a general module, that range syntax would
work ok. But under a worksheet module, excel thinks you mean a range on the
sheet that owns the code (and Labels is a different sheet, right?).

You could have used something like this, too:
myText = Application.Range("labels!A980").Value

But I like this first way lots more:
myText = Worksheets("labels").Range("a980").Value

I find it easier to read later on (when I'm correcting my other mistakes).
 
F

furbiuzzu

really thanks

myText = Worksheets("labels").Range("a9­80").Value

is what i meant to write.

it works, and , as you saud, is very easy to read.

sorry for my newbie questions!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top