+ Reply to Thread
Results 1 to 4 of 4

Combine Private Sub Worksheet_Change VBA Code on same worksheet

Hybrid View

  1. #1
    Registered User
    Join Date
    06-20-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    26

    Combine Private Sub Worksheet_Change VBA Code on same worksheet

    Please help me combine the codes below so that they work in the same worksheet.
    The work if only one set is applied but not together.

    Thanks

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim cL
            If Intersect(Target, Range("I:I")) Is Nothing Then Exit Sub
            For Each cL In Target.Cells
                Application.EnableEvents = False
                On Error GoTo enable
                If Len(cL) > 40 Then
                    MsgBox "More than 40 characters input into cell " & Replace(cL.Address, "$", "") & vbCrLf & "Data will be Truncated", vbExclamation + vbOKOnly
                    cL.Value = Strings.Left(cL, 40)
                    Else
                End If
            Next cL
    enable:
        Application.EnableEvents = True
    
    End Sub
    
      
    Private Sub Worksheet_Change(ByVal Target As Range)
       
       Dim FirstRow As Long
       Dim i As Long
       Dim LastRow As Long
       Dim Rng As Range
    
            FirstRow = 2
            LastRow = Range("B:B,A:A").Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False, False, False).Row
            Set Rng = Range(Cells(FirstRow, "B"), Cells(LastRow, "A"))
            
            If Intersect(Target, Rng) Is Nothing Then Exit Sub
            
                For i = FirstRow To LastRow
                    If Cells(i, "B") = "" And Cells(i, "A") <> "" Then
                        MsgBox "Channel ID is Missing" & vbCrLf & "Add Channel ID and Product Code together" & vbCrLf & "Data will be cleared", vbExclamation + vbOKOnly
                        Application.EnableEvents = False
                            Rng.Columns(1).Cells.ClearContents
                            Rng.Columns(2).Cells.ClearContents
                        Application.EnableEvents = True
                        Exit For
                    End If
                Next i
        
    End Sub
    Last edited by stanlelma; 07-16-2013 at 01:46 PM. Reason: Added Code Tags

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Combine Private Sub Worksheet_Change VBA Code on same worksheet

    Hi, stanlelma,

    please add code-tags around the procedures as required per Forum Rule #3.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Registered User
    Join Date
    06-20-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    26

    Re: Combine Private Sub Worksheet_Change VBA Code on same worksheet

    Thanks, the code-tags are added. Any solutions?

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Combine Private Sub Worksheet_Change VBA Code on same worksheet

    Hi, stanlelma,

    maybe like this (untested)
    Private Sub Worksheet_Change(ByVal Target As Range)
      Dim cL As Range
      Dim FirstRow As Long
      Dim i As Long
      Dim LastRow As Long
      Dim Rng As Range
      If Not Intersect(Target, Range("I:I")) Is Nothing Then
        For Each cL In Target.Cells
            Application.EnableEvents = False
            On Error GoTo enable
            If Len(cL) > 40 Then
                MsgBox "More than 40 characters input into cell " & Replace(cL.Address, "$", "") & vbCrLf & "Data will be Truncated", vbExclamation + vbOKOnly
                cL.Value = Strings.Left(cL, 40)
            End If
        Next cL
      Else
        FirstRow = 2
        LastRow = Range("B:B,A:A").Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False, False, False).Row
        Set Rng = Range(Cells(FirstRow, "B"), Cells(LastRow, "A"))
        If Not Intersect(Target, Rng) Is Nothing Then
          For i = FirstRow To LastRow
              If Cells(i, "B") = "" And Cells(i, "A") <> "" Then
                  MsgBox "Channel ID is Missing" & vbCrLf & "Add Channel ID and Product Code together" & vbCrLf & "Data will be cleared", vbExclamation + vbOKOnly
                  Application.EnableEvents = False
                      Rng.Columns(1).Cells.ClearContents
                      Rng.Columns(2).Cells.ClearContents
                  Application.EnableEvents = True
                  Exit For
              End If
          Next i
        End If
      End If
    
    enable:
      Application.EnableEvents = True
    
    End Sub
    although I would recommedn to use SpecialCells(XlCellTypeBlanks) or WorksheetFunction.CountBlank(Rrng) which would only include cells that really are blank (no formulas) instead of looping through all the range.

    Ciao,
    Holger

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Private Sub Worksheet_Change problem
    By Delorean14 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-13-2013, 02:25 AM
  2. [SOLVED] Private sub worksheet_change not working
    By DavidBW in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-21-2013, 06:06 PM
  3. [SOLVED] Merge two Private Sub Worksheet_Change code
    By nickmax1 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-14-2012, 07:46 AM
  4. Private Sub Worksheet_Change:MI Summary.
    By Skybeau in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-01-2010, 02:04 AM
  5. Private Sub Worksheet_Change
    By VicM in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-15-2008, 01:28 AM

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