ActiveCell changes contents of TextBox

N

nickm687

I have on a worksheet a textbox, and i want its contents to change
depending on the highlighted cell.

For example:

when A1 is selected the textbox contents are "msg1"

when A6 is selected the textbox reads "msg2"

Thanks in advance.
Nick
 
A

Alan

This should do what you want but the sub will run EVERY TIME you select
a new cell.

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal
Target As Range)

dim mymsg as string

if ActiveCell.Address="A1" then
mymsg="String 1"
elseif activecell.address="A6" then
mymsg="String 2"
else
exit sub
end if

msgbox mymsg

End Sub
 
N

nickm687

Thank you very much. Works a treat!

This should do what you want but the sub will run EVERY TIME you select
a new cell.

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal
Target As Range)

dim mymsg as string

if ActiveCell.Address="A1" then
mymsg="String 1"
elseif activecell.address="A6" then
mymsg="String 2"
else
exit sub
end if

msgbox mymsg

End Sub
 
A

Alan

Sorry, read the exam question and check your answer!!

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal
Target As Range)

dim mymsg as string

if ActiveCell.Address="$A$1" then
mymsg="String 1"
elseif activecell.address="$A$6" then
mymsg="String 2"
else
exit sub
end if

ActiveSheet.Shapes("Text Box").Select
Selection.Characters.Text = mymsg

End Sub
 
G

Guest

This would be a good place to use Select Case:

Select Case ActiveCell.Address
Case "$A$1"
mymsg="String 1"
Case "$A$6"
mymsg="String 2"
case else
exit sub
end select

Besides saving time, not having to write the same thing (activecell.address)
to be tested multiple times, it runs faster. The Case tests can include
combinations of multiple values, ranges of values, and inequalities (eg.,
Case 3, 6, 10 to 20 ; Case Is > 8)
 

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

Similar Threads

ActiveCell and TextBox 3
ActiveCell and TextBox 1
Range through Inputbox 3
TextBox change issue 1
textbox & tab 2
Word Wrap In UserForm TextBox 4
Textbox data to cell 5
Userform Textbox to Worksheet Textbox 1

Top