Consulting

Results 1 to 6 of 6

Thread: Cannot get two subs to run together

  1. #1
    VBAX Regular
    Joined
    May 2021
    Posts
    7
    Location

    Question Cannot get two subs to run together

    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
    Last edited by MOC; 05-25-2021 at 11:31 AM.

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Really not clear what you want to do, but I don't think that Worksheet_Change1 will ever be triggered
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    VBAX Regular
    Joined
    May 2021
    Posts
    7
    Location
    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?

  5. #5
    VBAX Regular
    Joined
    May 2021
    Posts
    7
    Location
    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.

  6. #6
    VBAX Regular
    Joined
    May 2021
    Posts
    7
    Location

    Lightbulb

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •