PDA

View Full Version : [SOLVED:] Cannot get two subs to run together



MOC
05-25-2021, 09:48 AM
Hello,

I have two subs (actually three subs I guess) that I'm trying to get to work on one sheet. First, is this possible? If it is, how do I do it?
Thanks for any and all help in advance!


Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim strList As String
On Error Resume Next
Application.EnableEvents = False


Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler


If rngDV Is Nothing Then GoTo exitHandler


If Not Intersect(Target, rngDV) Is Nothing Then
If Target.Validation.Type = 3 Then


strList = Target.Validation.Formula1
strList = Right(strList, Len(strList) - 1)
strDVList = strList
frmDVList.Show
End If
End If


exitHandler:
Application.EnableEvents = True


End Sub


Private Sub Worksheet_Change1(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim strSep As String
strSep = ", "
Application.EnableEvents = False
On Error Resume Next
If Target.Count > 1 Then GoTo exitHandler




Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler


If rngDV Is Nothing Then GoTo exitHandler


If Intersect(Target, rngDV) Is Nothing Then
'do nothing
Else


newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
If newVal = "" Then
'do nothing
Else
If oldVal = "" Then
Target.Value = newVal
Else
Target.Value = oldVal & strSep & newVal
End If
End If


End If


exitHandler:
Application.EnableEvents = True
End Sub


Private Sub Worksheet_Change(ByVal Target As Range)Set Target = Range("W4")
If Target.Value = "HIGH" Then
Call PostMitChoice_Initialize
End If
If Target.Value = "CATASTROPHIC" Then
Call PostMitChoice_Initialize
End If
End Sub


Private Sub PostMitChoice_Initialize()
Load PostMitChoice
PostMitChoice.Show
End Sub

SamT
05-25-2021, 01:09 PM
IMO, and I admit, it is a rare opinion, Events subs should only do one thing: That is decide which subsequent sub-procedures to run.

Otherwise:

Sub Worksheet_Change1 is not a valid Event Sub


Worksheet_Change(ByVal Target As Range)
Set Target = Range("W4")
If Range("W4").Value = "HIGH" Then
Would be better as
Worksheet_Change(ByVal Target As Range)
If Range("W4").Value = "HIGH" Then

Paul_Hossler
05-25-2021, 02:30 PM
Really not clear what you want to do, but I don't think that Worksheet_Change1 will ever be triggered

MOC
05-25-2021, 02:45 PM
When I just have the FIRST bit of code (noted above) entered by itself, it runs and works perfectly.
-This FIRST bit of code pops up a Selection Box based on a Data Validation List AND it allows for more than one selection from that list.
-As an example: One list has: Health, Safey, Environment, Public, City, External
-When the Selection Box pops up, the user can select one or more from the list to be entered into the cell.





When I just have the SECOND bit of code (noted above) entered by itself, it runs and works perfectly.
-The SECOND bit of code pops up an alert (userform) based on specific selections from the Data Validation List.
-As an example: One list has: None, Minor, Moderate, High, Catastrophic.
-The userform is called only if the user selects HIGH or CATASTROPHIC and does not get called for any other selections from that list.


When I put both FIRST & SECOND bits of codes together in the same sheet, neither runs or works.

So, I need both bits of code to run together and work together. Is this possible?

MOC
05-25-2021, 03:21 PM
I should also note ... I only need the SECOND bit of code that calls the userform to work in COLUMN "W" ONLY, it does not need to work in any other column.
So, when the user selects from the Popup Selection Box (FIRST bit of code above) in COLUMN W is either HIGH or CATASTROPHIC, then the userform must be called.
The userform is not required anywhere else in the sheet/workbook. The FIRST bit of code above must be available throughout the entire sheet.

MOC
05-25-2021, 04:04 PM
I figured it out!!!

I just started moving part of the SECOND bit of code (RED BOLDED below) into the FIRST bit of code and it eventually worked when I found the right spot to place that little snippet of code!!


Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim strList As String
On Error Resume Next
Application.EnableEvents = False


Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler


If rngDV Is Nothing Then GoTo exitHandler


If Not Intersect(Target, rngDV) Is Nothing Then
If Target.Validation.Type = 3 Then
strList = Target.Validation.Formula1
strList = Right(strList, Len(strList) - 1)
strDVList = strList
frmDVList.Show
If Target.Value = "HIGH" Then
Call PostMitChoice_Initialize
End If
If Target.Value = "CATASTROPHIC" Then
Call PostMitChoice_Initialize
End If
End If
End If
exitHandler:
Application.EnableEvents = True
End Sub


Private Sub Worksheet_Change1(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim strSep As String
strSep = ", "
Application.EnableEvents = False
On Error Resume Next
If Target.Count > 1 Then GoTo exitHandler


Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler


If rngDV Is Nothing Then GoTo exitHandler


If Intersect(Target, rngDV) Is Nothing Then
'do nothing
Else
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
If newVal = "" Then
'do nothing
Else
If oldVal = "" Then
Target.Value = newVal
Else
Target.Value = oldVal & strSep & newVal
End If
End If
End If
exitHandler:
Application.EnableEvents = True
End Sub


Private Sub PostMitChoice_Initialize()
Load PostMitChoice
PostMitChoice.Show
End Sub