Sheet names with spaces in VBA

hatstand

Well-known Member
Joined
Mar 17, 2005
Messages
778
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have been using the code below for a form. But now I need to change the sheet name to have spaces. But when I do. I comes up with the Run Time 1004 error on the “Ctrl.Text = Range(Ctrl.Tag).Text '*** “ line.

Why is it that this line is not allowing me to have spaces?

Private Sub Load_Controls()
Dim Ctrl As Control
For Each Ctrl In UserForm1.Controls
If Ctrl.Tag <> "" Then
Ctrl.Text = Range(Ctrl.Tag).Text '***
End If
Next Ctrl
End Sub
 
Hi,

I really appreciate the help form everyone.

Unfortunately I’m totally lost. I’m no programmer and am really trying to learn, but still can’t get it to work.
 
Upvote 0

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Here is all of the code for the form:

As you may have guessed, I didn't write these! But I'm happy to try and understand what they do, for the future.

Private Sub UserForm_Activate()
Call Load_Controls
End Sub
'--------------------------------
Private Sub Btn_OK_Click()
Dim Ctrl As Control
For Each Ctrl In KeyPoints.Controls
If Ctrl.Tag <> "" Then
Range(Ctrl.Tag).Value = Ctrl.Text
End If
Next Ctrl
Btn_Cancel_Click
End Sub
'-----------------------------------
Private Sub Load_Controls()
Dim Ctrl As Control
For Each Ctrl In KeyPoints.Controls
If Ctrl.Tag <> "" Then
Ctrl.Text = Range(Ctrl.Tag).Text '***
End If
Next Ctrl
End Sub
'---------------------------------------
Private Sub Btn_Cancel_Click()
Unload Me
End Sub
 
Upvote 0
Assuming that the code is from the Keypoints userform....
When that form is loaded, it displays some list boxes.
For those listboxes that have a value in their .Tag field
that value identifies an address which contains the data that is displayed
in the listbox.

When the OK button is pressed, the values that are now in the listboxes
are copied back to the addresses specified in the .Tag values

If the address is invalid, the 1004 error is raised.

Valid addresses for the .Tag would inlcude those like:
A1
Sheet1!A1
'Sheet Name With Spaces'!A1

Note the single quote around the worksheet name (Sheet Name With Spaces) in the last example.

See what the .Tag values are for all of the controls and make sure they are valid addresses.
 
Upvote 0
i guess the tag property has been set at designtime
put the line
msgbox ctrl.tag
into the code to display the content of the tag property
Rich (BB code):
If Ctrl.Tag <> "" Then
msgbox ctrl.tag
             Ctrl.Text = Range(Ctrl.Tag).Text '***
        End If
    Next Ctrl
 
Upvote 0
I still can't get it to work, so I guess I'll start fromscratch and try another way of doing the form.

Thanks for the help I really appreciate it.
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,625
Members
449,093
Latest member
catterz66

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