Userform title based on a cell value

MrOzman

New Member
Joined
May 16, 2007
Messages
49
I don't understand why this isn't working.

Sub showsheet()
UserForm1.Caption = Range("Adm!b2").Value
UserForm1.Show
UserForm1.TextBox1.SetFocus
End Sub

I need the titlebar of the userform to show whatever is in Adm!b2. If it's important this value will always be text.

Thanks in advance.
 

Some videos you may like

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,067
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
That should work as long as the code is in a normal module.
 

MrOzman

New Member
Joined
May 16, 2007
Messages
49
Thanks for that.

I had it in the code for sheet 1.

Is there a short explanation that a simpleton would understand for why having it in a module makes a difference?
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,067
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Yep - when you use:
Code:
Range("Adm!b2").Value
in the worksheet code module for Sheet1, what it translates to is:
Code:
Sheet1.Range("Adm!b2").Value
which doesn't make sense since the range is not on Sheet1. In a normal module, it translates to:
Code:
Application.Range("Adm!b2").Value
which is OK. I would use:
Code:
Worksheets("Adm").Range("b2").Value
or even:
Code:
ThisWorkbook.Worksheets("Adm").Range("b2").Value
which will work pretty much anywhere.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,521
Messages
5,596,651
Members
414,083
Latest member
Mrsash

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
Top