+ Reply to Thread
Results 1 to 14 of 14

Combine Two Similar Private Subs

Hybrid View

swordswinger710 Combine Two Similar Private... 02-27-2012, 09:43 AM
OnErrorGoto0 Re: Combine Two Similar... 02-27-2012, 09:45 AM
swordswinger710 Re: Combine Two Similar... 02-27-2012, 09:51 AM
swordswinger710 Re: Combine Two Similar... 02-27-2012, 10:17 AM
OnErrorGoto0 Re: Combine Two Similar... 02-27-2012, 10:19 AM
swordswinger710 Re: Combine Two Similar... 02-27-2012, 11:25 AM
OnErrorGoto0 Re: Combine Two Similar... 02-27-2012, 11:58 AM
swordswinger710 Re: Combine Two Similar... 02-27-2012, 12:08 PM
swordswinger710 Re: Combine Two Similar... 02-27-2012, 12:13 PM
OnErrorGoto0 Re: Combine Two Similar... 02-27-2012, 12:18 PM
swordswinger710 Re: Combine Two Similar... 02-27-2012, 12:30 PM
OnErrorGoto0 Re: Combine Two Similar... 02-27-2012, 12:40 PM
swordswinger710 Re: Combine Two Similar... 02-27-2012, 01:01 PM
swordswinger710 Re: Combine Two Similar... 02-27-2012, 02:15 PM
  1. #1
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Question Combine Two Similar Private Subs

    Hello,

    I need to add this code from Sheet 2 of my workbook:

    Option Explicit
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
        If Not Intersect(Target, Range("A14,G14,L14,Q14,U14,A16,A18")) Is Nothing Then
            If Target.Value = "P" Then
                 Target.Value = ""
            Else
                 Target.Value = "P"
            End If
        Range("G12").Select  ' or whatever cell you want selected after a click.
        End If
    
    End Sub
    ..to this code on Sheet 1 of the same workbook:

    Option Explicit
    Const tCell As String = "X1" ' Set this to the individual sheet cell
    Private Sub Worksheet_Activate()
        nPages = setPages(Me, tCell)
    End Sub
    Private Sub Worksheet_Calculate()
        nPages = setPages(Me, tCell)
    End Sub
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        With Range("X1")
            If .Value = Empty Then
                Application.EnableEvents = False
                .Select
                MsgBox "Please enter the number of pages required."
                Application.EnableEvents = True
            End If
        End With
    End Sub
    What is the correct way of doing this so that both SelectionChange events will work? I'm trying to learn how.
    Last edited by swordswinger710; 02-27-2012 at 05:30 PM.

  2. #2
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Combine Two Similar Private Subs

    Are you sure you want SelectionChange and not Change for the first code?
    Good luck.

  3. #3
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Combine Two Similar Private Subs

    Absolutely. See here on where I got it from and it's purpose.

  4. #4
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Combine Two Similar Private Subs

    Okay sweet, thank you - I'm still trying to see how you combined the two, apparently it's not just an easy matter of copying and pasting in the right location.

    And yes, I find it irritating as well! I still can't believe the fact that there's no way I'm not able to click once on a cell to check it, then click the same cell again to un-check it without having some other cell getting in the way, but oh well.

  5. #5
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Combine Two Similar Private Subs

    That would be a double-click, no?

  6. #6
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Combine Two Similar Private Subs

    Technically, I guess so!

    So if I decided to go with this less annoying code instead:

    Option Explicit
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    
        Select Case Target.Address
            Case "$A$14", "$G$14", "$L$14"
                If Target = "P" Then Target = "" Else Target = "P"
        End Select
    
    End Sub
    ..how would that combine? I see you're adding tCell and rCell? Is there a standard way of combining formulas like these? I may have to do this to several more sheets in my workbook.

  7. #7
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Combine Two Similar Private Subs

    I only used rCell because my version loops through the selected cells, in case you selected more than one of your target cells. If you won't be doing that, then your code is fine without the loop.

  8. #8
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Combine Two Similar Private Subs

    So if this..

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        With Range("X1")
            If .Value = Empty Then
                Application.EnableEvents = False
                .Select
                MsgBox "Please enter the number of pages required."
                Application.EnableEvents = True
            End If
        End With
    End Sub
    ..plus this..

    Option Explicit
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
        If Not Intersect(Target, Range("A14,G14,L14,Q14,U14,A16,A18")) Is Nothing Then
            If Target.Value = "P" Then
                 Target.Value = ""
            Else
                 Target.Value = "P"
            End If
        Range("G12").Select  ' or whatever cell you want selected after a click.
        End If
    
    End Sub
    ..equals this:

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Dim rCell As Range
        With Range(tCell)
            If .Value = Empty Then
                Application.EnableEvents = False
                .Select
                MsgBox "Please enter the number of pages required."
                Application.EnableEvents = True
            End If
        End With
        If Not Intersect(Target, Range("A14,G14,L14,Q14,U14,A16,A18")) Is Nothing Then
            For Each rCell In Intersect(Target, Range("A14,G14,L14,Q14,U14,A16,A18")).Cells
            If rCell.Value = "P" Then
                 rCell.Value = ""
            Else
                 rCell.Value = "P"
            End If
        Next rCell
        Range("G12").Select  ' or whatever cell you want selected after a click.
        End If
    End Sub
    ..then would this..

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        With Range("X1")
            If .Value = Empty Then
                Application.EnableEvents = False
                .Select
                MsgBox "Please enter the number of pages required."
                Application.EnableEvents = True
            End If
        End With
    End Sub
    ..plus this..

    Option Explicit
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    
        Select Case Target.Address
            Case "$A$14", "$G$14", "$L$14"
                If Target = "P" Then Target = "" Else Target = "P"
        End Select
    
    End Sub
    ..equal.. this?

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Dim rCell As Range
        With Range(tCell)
            If .Value = Empty Then
                Application.EnableEvents = False
                .Select
                MsgBox "Please enter the number of pages required."
                Application.EnableEvents = True
            End If
        End With
        Select Case Target.Address
            Case "$A$14", "$G$14", "$L$14"
                If Target = "P" Then Target = "" Else Target = "P"
        End Select
        End If
    End Sub

  9. #9
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Combine Two Similar Private Subs

    Hmpf. Apparently not as I'm getting errors with it.

  10. #10
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Combine Two Similar Private Subs

    You have a superfluous End If at the end.

  11. #11
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Combine Two Similar Private Subs

    When I take that away, I get more errors.

  12. #12
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Combine Two Similar Private Subs

    What errors?

  13. #13
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Combine Two Similar Private Subs

    If I do this:

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Dim rCell As Range
        With Range(tCell)
            If .Value = Empty Then
                Application.EnableEvents = False
                .Select
                MsgBox "Please enter the number of pages required."
                Application.EnableEvents = True
            End If
        End With
        Select Case Target.Address
            Case "A$14","$G$14", "$L$14"
                If Target = "P" Then Target = "" Else Target = "P"
        End Select
    End Sub
    I get this error- Haha, and now it works, of course. Wow. There is still one issue, however. I don't know how difficult the fix would be, but by clicking any of the cells listed in the second part of the code (A14, G14, or L14) the first part of the code (which requires a page number to be entered before any other cell gets data entered) gets bypassed and the checkbox cell gets changed. The Message Box does appear, but after clicking OK, the checkbox cell that was clicked changes.

    Is there something we can change to solve this?

  14. #14
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Combine Two Similar Private Subs

    Oh wow, that's fantastic! Thank you so much for all your help. Much appreciated!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1