What is wrong with this vba code

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,352
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Sub Workbook_Open()
'Application.WindowState = xlMaximized
txtName.Value = "Type sheet name here."
End Sub

I am just trying to make a default value appear for a text box upon open of worksheet and it won't work and I don't know why.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Is this what you mean ???
Code:
Sub Workbook_Open()
'Application.WindowState = xlMaximized
MsgBox Activesheet.name
End Sub
 
Upvote 0
I want the text to be visible in the text box when the form is opened but it can be typed over.
 
Upvote 0
Where is this textbox, in a userform?
If so, you could set the form's default value to "Type sheet name here." in the property window at design time.
Or you could use syntax like

Code:
UserForm1.txtName.Text =  "Type sheet name here."

If it is an activeX textbox, you could also set the Value property of the text box to "Type sheet name here."
 
Upvote 0
So, did my code do as required ?
If not, you need to be more specific.

You could also use....

Code:
Sub Workbook_Open()
'Application.WindowState = xlMaximized
Sheets(1).Shapes("TextBox 1").TextFrame.Characters.Text = "Type Sheet Name Here"
End Sub
 
Upvote 0
This code gives me an error.


Sheets("home").Shapes("txtName").TextFrame.Characters.Text = "Type sheet name here."
 
Upvote 0
You didn't answer any of Mikes questions in post #4....They are relevent to getting the correct result ??

My answer won't work if it is NOT a worksheet TextBox as opposed to a VBA userform TxtBox !!!
 
Upvote 0
Where is this textbox, in a userform?
If so, you could set the form's default value to "Type sheet name here." in the property window at design time.
Or you could use syntax like

Code:
UserForm1.txtName.Text =  "Type sheet name here."

It is an active x text box but this won't work Mike as I change the value within the value property then I add something into the text box. I have to save it when I close it then when i reopen it, the new contents are displayed.
 
Upvote 0
Where is this textbox, in a userform?
If so, you could set the form's default value to "Type sheet name here." in the property window at design time.
Or you could use syntax like

Code:
UserForm1.txtName.Text =  "Type sheet name here."

If it is an activeX textbox, you could also set the Value property of the text box to "Type sheet name here."



It is an active x text box but this won't work Mike as I change the value within the value property then I add something into the text box. I have to save it when I close it then when i reopen it, the new contents are displayed.
 
Upvote 0
Then then code in the Open event should be

Code:
Sheets("someSheet").txtName.Text =  "Type sheet name here."
 
Upvote 0

Forum statistics

Threads
1,215,558
Messages
6,125,511
Members
449,236
Latest member
Afua

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