Excel VBA: Stop userform from reopening - excel

I have two userforms in a worksheet that open when yes or no are respectively selected from a dropdown list in cell G5.
Everytime a user continues to enter data somwhere else in the worksheet (in cells other than G5), the userform reopens/reappears though.
Is there a way to ensure the userform is only opened when the value in G5 changes?
(Application.EnableEvents = True needs to be on as there are more userforms further down the sheet.)
Thanks in advance for any help!
Here's my code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = True
Set Target = Range("G5")
Application.EnableEvents = False
If Target = "No" Then
Form1.Show
ElseIf Target = "Yes" Then
From2.Show
End If
Application.EnableEvents = True
End Sub

Something like
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Target.Address = "$G$5" Then
If Target = "No" Then
Form1.Show
ElseIf Target = "Yes" Then
From2.Show
End If
End If
Application.EnableEvents = True
End Sub

Related

VBA Multiple Target.Address (OR) for the Same Action

In a worksheet I have two drop-down lists (cells C7 and C68) which each have a dependent drop-down in the cell below. I have a code (below) which will clear the cell of the dependent drop-down if I change the selection in the above list (so that the lists do not mis-match), however I can only get this to work for the one drop-down in the sheet. How can I amend this to that it works if I alter either of the cells with the "Parent" list?
.
Existing code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$7" Then
If Target.Validation.Type = 3 Then
Application.EnableEvents = True
Target.Offset(1, 0).Value = ""
End If
End If
exitHandler:
Application.EnableEvents = True
Exit Sub
I suggest using only 1 dropdown list (which user actually selects) and then 2nd "linked cell" is using vlookup from some kind of data transformation list.
All fixed - for anyone who also has this problem, the correct code was:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$7" Or Target.Address = "$C$68" Then
If Target.Validation.Type = 3 Then
Application.EnableEvents = False
Target.Offset(1, 0).Value = ""
End If
End If
exitHandler:
Application.EnableEvents = True
Exit Sub
End Sub

Copy paste values triggered by worksheet change event is not working

I am using worksheet change event to trigger copy paste values. Worksheet change code is in the sheet2
Sub worksheet_change(ByVal Target As Range)
Application.EnableEvents = True
Set Target = Range("AB2")
If Target.Value = "OK" Then
Call myTR1
End If
Please note AB2 cell takes it's value from another sheet
Copy paste code is in a Module
Sub myTR1()
Sheets("BA1").Range("AR6:AS8").Value = Sheets("BA1").Range("AL17:AM19").Value
End Sub
When target range changes to "OK", my copy paste macro is not triggering. What am I doing wrong?
Using your eaxct code worked, although you didnt have end sub in your example?
EDIT:
Bear in mind the 'OK' is case sensitive so it will have to be in uppercase to fire, if you want it to fire either on lower or upper you can use the second code.
Sub worksheet_change(ByVal Target As Range)
Application.EnableEvents = True
Set Target = Range("AB2")
If Target.Value = "OK" Then
Call myTR1
End If
End Sub
Sub worksheet_change(ByVal Target As Range)
Application.EnableEvents = True
Set Target = Range("AB2")
If Target.Value = "OK" Or Target.Value = "ok" Then
Call myTR1
End If
End Sub

MS Excel Objects Lock Row based on Value working with Other Object

I have programming, with major help from others, that works great for two separate workbooks. The problem is that I can't seem to have them both work within the same workbook and I don't know why.
We have a large contact database (4800 rows) workbook with a lot of sorting macros. I use the following to allow the user to double-click a cell to select or de-select a contact. This is under the Excel Objects, Sheet1 (MASTER):
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Limit Target count to 1
If Target.Count > 1 Then Exit Sub
'Isolate Target to a specific range
If Intersect(Target, Range("SelectionMaster")) Is Nothing Then Exit Sub
'set Target font tp "marlett"
Target.Font.Name = "marlett"
'Check value of target
If Target.Value < "a" Then
Target.Value = "a" 'Sets target Value = "a"
Cancel = True
Exit Sub
End If
If Target.Value = "a" Then
Target.Value = "r"
Cancel = True
Exit Sub
End If
If Target.Value = "r" Then
Target.ClearContents 'Sets Target Value = ""
Cancel = True
Exit Sub
End If
End Sub
In another workbook, I tested the following for locking a particular row if an 'X' is placed in Column AZ (end of contact data). This is under the Excel Objects, Sheet1 (Sheet1):
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim LR As Long
If Not Target.Column = 52 Then Exit Sub
If Target.Count > 1 Then Exit Sub
If UCase(Target.Value) = "X" Then
ActiveSheet.Unprotect
Target.EntireRow.Locked = True
ActiveSheet.Protect
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Locked Then
If MsgBox("Row is locked. Do you want to change the value ?", vbYesNo) = vbYes Then
ActiveSheet.Unprotect
Target.EntireRow.Locked = False
Cells(ActiveCell.Row, 52).Value = ""
ActiveSheet.Protect
End If
End If
End Sub
In the Database file, I have certain rows that identify the contact's category that I would like to have locked. All other cells can be changed. If I copy over the 2nd code to the database file, it doesn't work correctly. Instead it locks all of the cells and prompts the message box regardless of an 'X' in Column AZ. Can these two not co-exist or what is this newb doing wrong?

How do I fix non target cells from activating the sub Worksheet_Change(ByVal Target As Range)

My spread sheet has target cells and cells that I want to be able to enter manual data without the Worksheet Change to activate or run. How do I allow these open user cells to be populated without the private sub running??
More information sample code. There are many code sections like this in the same format.
Private sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = True
If Intersect(Target, Range("$B$8:$k$9")) Is Nothing Then
GoTo NEXT10
'Application.EnableEvents = False
End If
Application.EnableEvents = True
MsgBox Target.Address
'-------------------------------------------------------------------
'Application.EnableEvents = True
Application.EnableEvents = False
If Target.Address = "$B$8" Then
If Target.Value >= 0 Then
Range("B9") = Range("B8").Value * 42
End If
ElseIf Target.Address = "$B$9" Then
Range("B8") = Range("B9").Value / 42
End If
Application.EnableEvents = True
End Sub
The problem is if I enter, delete value or text in any empty cells on the worksheet, it activates the Worksheet_Change (ByVal Target ....).I use the If Intersect(Target,Range($B8$:$k$8) Is Nothing to bypass the target if no change. This format is used for other target ranges of important. But I do not understand why when any cell is changed the program runs?? Can this be avoid so various manual entries can be performed? For example text notes, labels, etc.
The Next10 is the start of another section of code but with diferent target address. If the target addess indicated is not intersected, it goes the check the next intersection.

How to Unhide a number Cells in one sheet based on a number in another sheet?

Hi there i want to hide cells in Sheet 2 based on a number entered in a cell in Sheet 1
What I have tried:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
'This is were im stuck?
If Target.Address = "Sheet1!$D$387" Then
Rows("28:46").Hidden = True
Rows("28:" & 28 + Val(Target.Value)).Hidden = False
Application.ScreenUpdating = True
End If
End Sub
What is the macros to access the value of D387 in Sheet 1?
Here is my own solution with help from Larry(first comment on the question)
This code will go into Sheet1's Module (as suggested by Larry):
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$D$387" Then
Sheet2.Rows("27:46").Hidden = True
Sheet2.Rows("27:" & 27 + Val(Target.Value)).Hidden = False
Application.ScreenUpdating = True
End If
End Sub
I had originally placed it into Sheet2's Module

Resources