UserForm dependent upon cell content. Now opens every time any cell is selected the userform opens again.

AwesomeSteph

Board Regular
Joined
Aug 18, 2017
Messages
80
I have it so that the userform only shows when "XYZ" is selected from a drop down in B2.
But now even after it is satisfied if I select any other drop down in any other cell of this worksheet the userform opens again. I only want it to open if "XYZ" is selected from B4.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
If [B2].Text = "XYZ" Then
UserForm.Show
With UserForm
  .StartUpPosition = 1
  .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
  .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
  .Show
End With
End If
Application.ScreenUpdating = True
End Sub

Any help is greatly appreciated!
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
[COLOR=#0000ff]If Target.Address <> "B2" Then Exit Sub[/COLOR]
If [B2].Text = "XYZ" Then
UserForm.Show
With UserForm
  .StartUpPosition = 1
  .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
  .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
  .Show
End With
End If
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
[COLOR=#0000ff]If Target.Address <> "B2" Then Exit Sub[/COLOR]
If [B2].Text = "XYZ" Then
UserForm.Show
With UserForm
  .StartUpPosition = 1
  .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
  .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
  .Show
End With
End If
Application.ScreenUpdating = True
End Sub
This stopped the userform from opening at all.
 
Upvote 0
Is B2 a data validation dropdown?
 
Upvote 0
Sorry, it should be
Code:
If Target.Address[COLOR=#ff0000](0,0)[/COLOR] <> "B2" Then Exit Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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