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.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
38,958
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
38,958
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.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,167,956
Messages
5,856,504
Members
431,818
Latest member
AKEMAROD

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