Show userform when cell is selected

buzz71023

Active Member
Joined
May 29, 2011
Messages
295
Office Version
  1. 2016
Platform
  1. Windows
I need to be able to make a macro run to show a userform when a cell is selected. Hopefully this will fix the solution to my problem. I dont want an user to be able to enter data into this cell unless its through the useform. The cells defined name is "LotNumber".
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Try this: right click the sheet tab, select View Code and paste in

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Name.Name = "LotNumber" Then UserForm1.Show
End Sub
 
Upvote 0
as this sheet is copied and renamed will the code stay with the copied sheet as well? because a have a macro to copy this sheet and i need it to stay with the new copied and renamed sheet.
 
Upvote 0
the code is not doing anything... I tried saving and going back in but it still didnt do anything and now i am getting a run-time error 1004, Application-defined or object-defined error everytime i select a cell that does not have a defined name. i've double checked my name and it is LotNumber.
 
Upvote 0
if i wanted to add more defined names/cells to this sheet, would i just keep using the same code over and over with the appropriate names and userforms?
 
Upvote 0
To avoid the error

Rich (BB code):
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next
If Target.Name.Name = "LotNumber" Then UserForm1.Show
On Error GoTo 0
End Sub

To work with more named ranges replicate the line in red.
 
Upvote 0
ok the error is gone but the userform is still not showing when that cell is being selected. all i did was paste your code and claify that the names were correct (Userform and Cell defined name are both correct).
 
Upvote 0
It works for me. The code has to go in the sheet's code module and macros must be enabled. This is a better version

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error GoTo ErrHandl
If Target.Name.Name = "LotNumber" Then UserForm1.Show
Exit Sub
ErrHandl:
Err.Clear
End Sub
 
Upvote 0
First, Thanks for your help and your patience

Yeah, I dont know why it is not working for me. Macros are enabled (I have a about a 100 or so one this one sheet that have been working for 6 months or so. I even tried another named cell and userform and still didnt work for me. do i need to hit the Run button in VBE and select a macro or anything like that because when i hit the Run button in VBE all the Macros pop up.

My exact steps:
In VBE I right clicked on "For Copy" (which is the name of my sheet) and then posted the code you gave me and changed the name of the Userform "LotNumber" (which is also the name of my userform. Close out VBE, Goto my spread sheet and try selecting the cell with the name LotNumber. Sound correct?

Does the version of excel matter?
 
Upvote 0

Forum statistics

Threads
1,215,455
Messages
6,124,938
Members
449,197
Latest member
k_bs

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