Results 1 to 4 of 4

How to combine Private Sub in VBA Code

Threaded View

ShanieReyes How to combine Private Sub in... 01-27-2022, 03:09 PM
WideBoyDixon Re: How to combine Private... 01-27-2022, 05:49 PM
ShanieReyes Re: How to combine Private... 01-27-2022, 05:58 PM
WideBoyDixon Re: How to combine Private... 01-27-2022, 06:40 PM
  1. #1
    Registered User
    Join Date
    01-27-2022
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    3

    How to combine Private Sub in VBA Code

    Hi!

    I need help in how to write or combine my two codes in VBA.

    When I insert both codes, the last one is working.

    Thank youu!

    1st code: Enables me to select multiple dropdown option in some of my columns.
    2nd code: Enables me to autocomplete dropdowns while typing


    Please see below:
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Oldvalue As String
    Dim Newvalue As String
    Application.EnableEvents = True
    On Error GoTo Exitsub
    If Not Intersect(Target, Range("AE:AE,BO:BO,CD:CD,CJ:CJ,CK:CK,CL:CL,CM:CM,CN:CN,CQ:CQ,CX:CX,DF:DF")) Is Nothing Then
      If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
        GoTo Exitsub
      Else: If Target.Value = "" Then GoTo Exitsub Else
        Application.EnableEvents = False
        Newvalue = Target.Value
        Application.Undo
        Oldvalue = Target.Value
          If Oldvalue = "" Then
            Target.Value = Newvalue
          Else
            If InStr(1, Oldvalue, Newvalue) = 0 Then
                Target.Value = Oldvalue & "," & Newvalue
          Else:
            Target.Value = Oldvalue
          End If
        End If
      End If
    End If
    Application.EnableEvents = True
    Exitsub:
    Application.EnableEvents = True
    End Sub
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    'Update by Extendoffice: 2020/01/16
        Dim xCombox As OLEObject
        Dim xStr As String
        Dim xWs As Worksheet
        Dim xArr
         
        Set xWs = Application.ActiveSheet
        On Error Resume Next
        Set xCombox = xWs.OLEObjects("TempCombo")
        With xCombox
            .ListFillRange = ""
            .LinkedCell = ""
            .Visible = False
        End With
        If Target.Validation.Type = 3 Then
            Target.Validation.InCellDropdown = False
            Cancel = True
            xStr = Target.Validation.Formula1
            xStr = Right(xStr, Len(xStr) - 1)
            If xStr = "" Then Exit Sub
            With xCombox
                .Visible = True
                .Left = Target.Left
                .Top = Target.Top
                .Width = Target.Width + 5
                .Height = Target.Height + 5
                .ListFillRange = xStr
                If .ListFillRange = "" Then
                    xArr = Split(xStr, ",")
                    Me.TempCombo.List = xArr
                End If
                .LinkedCell = Target.Address
            End With
            xCombox.Activate
            Me.TempCombo.DropDown
        End If
    End Sub
    Private Sub TempCombo_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
        Select Case KeyCode
            Case 9
                Application.ActiveCell.Offset(0, 1).Activate
            Case 13
                Application.ActiveCell.Offset(1, 0).Activate
        End Select
    End Sub
    
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    'Update by Extendoffice: 2020/01/16
        Dim xCombox As OLEObject
        Dim xStr As String
        Dim xWs As Worksheet
        Dim xArr
         
        Set xWs = Application.ActiveSheet
        On Error Resume Next
        Set xCombox = xWs.OLEObjects("TempCombo")
        With xCombox
            .ListFillRange = ""
            .LinkedCell = ""
            .Visible = False
        End With
        If Target.Validation.Type = 3 Then
            Target.Validation.InCellDropdown = False
            Cancel = True
            xStr = Target.Validation.Formula1
            xStr = Right(xStr, Len(xStr) - 1)
            If xStr = "" Then Exit Sub
            With xCombox
                .Visible = True
                .Left = Target.Left
                .Top = Target.Top
                .Width = Target.Width + 5
                .Height = Target.Height + 5
                .ListFillRange = xStr
                If .ListFillRange = "" Then
                    xArr = Split(xStr, ",")
                    Me.TempCombo.List = xArr
                End If
                .LinkedCell = Target.Address
            End With
            xCombox.Activate
            Me.TempCombo.DropDown
        End If
    End Sub
    Private Sub TempCombo_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
        Select Case KeyCode
            Case 9
                Application.ActiveCell.Offset(0, 1).Activate
            Case 13
                Application.ActiveCell.Offset(1, 0).Activate
        End Select
    End Sub
    Last edited by ShanieReyes; 01-31-2022 at 11:25 AM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Combine Private Sub Worksheet_Change(ByVal Target As Range) VBA Code on same worksheet
    By mark_luke in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-27-2022, 04:48 PM
  2. Combine Private Sub and Private Function VBA code
    By thanhthinh1234 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-25-2018, 06:16 AM
  3. [SOLVED] combine two Private Sub Worksheet_Change
    By mazan2010 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-02-2016, 03:32 AM
  4. [SOLVED] How to Combine two Private Sub Worksheet_Change ?
    By stoicy in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-17-2016, 04:34 PM
  5. [SOLVED] Not sure how to combine two Private Sub Worksheet_Changes
    By sacastiglia in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-23-2014, 11:51 PM
  6. I need to combine 2 private subs
    By superchew in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-17-2014, 10:30 AM
  7. Combine Private Sub Worksheet_Change VBA Code on same worksheet
    By stanlelma in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-06-2013, 12:42 PM

Tags for this Thread

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