Combine two Private Sub Worksheet_Change - VBA Macro

uhguy

Board Regular
Joined
Aug 22, 2008
Messages
79
Hi everyone!

Thanks for reading first of all.

I have these two Private Sub Worksheet_Change(ByVal Target As Range) that work fine alone in one sheet, but if I put both together I get errors. I read that they need to be combine into one so there is only one Private Sub Worksheet_Change(ByVal Target As Range) but I have no idea how to even start. Any help is appreciated. Below is both Private subs. Thanks!!


Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Set MyPlageD = Range("D6:D50")
Set MyPlageI = Range("I6:I50")

For Each CellD In MyPlageD
        If CellD.Value = "Other" Then
           
              For Each CellI In MyPlageI
                    If CellI.Value <> "" Then
                    CellI.EntireRow.Interior.ColorIndex = xlNone
                    End If
                                        
                    Next
                    
        
        CellD.EntireRow.Interior.ColorIndex = 3
        End If
        
        If CellD.Value <> "Other" Then
        CellD.EntireRow.Interior.ColorIndex = xlNone
        End If

Next

For Each CellI In MyPlageI
                    If CellI.Value <> "" Then
                    CellI.EntireRow.Interior.ColorIndex = xlNone
                    End If

Next

End Sub



Code:
Private Sub Worksheet_Change(ByVal Target As Range)


    If HasValidation(Range("ValidationRange")) Then
    
      Exit Sub
Else
        Application.Undo
      MsgBox "Your last operation was canceled. " & _
      "It would have deleted data validation rules.", vbCritical
   End If
End Sub

Private Function HasValidation(r) As Boolean

   On Error Resume Next
  x = r.Validation.Type
 If Err.Number = 0 Then HasValidation = True Else HasValidation = False
End Function
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
SOLVED ----->

Probably not the best way but it worked.


Code:
If HasValidation(Range("ValidationRange")) Then
    
                    Set MyPlageD = Range("D6:D50")
                    Set MyPlageI = Range("I6:I50")

                    For Each CellD In MyPlageD
                    If CellD.Value = "Other" Then
                    
                                For Each CellI In MyPlageI
                                If CellI.Value <> "" Then
                                CellI.EntireRow.Interior.ColorIndex = xlNone
                                End If
                                                    
                                Next
                                
                    CellD.EntireRow.Interior.ColorIndex = 3
                    End If
                    
                    If CellD.Value <> "Other" Then
                    CellD.EntireRow.Interior.ColorIndex = xlNone
                    End If

                    Next

                    For Each CellI In MyPlageI
                    If CellI.Value <> "" Then
                    CellI.EntireRow.Interior.ColorIndex = xlNone
                    End If

                    Next
           Else
                Application.Undo
                  MsgBox "Your last operation was canceled. " & _
                  "It would have deleted data validation rules.", vbCritica
End If






'****************
Set MyPlageD = Range("D6:D50")
Set MyPlageI = Range("I6:I50")




For Each CellD In MyPlageD
        If CellD.Value = "Other" Then
        
        
        
                For Each CellI In MyPlageI
                    If CellI.Value <> "" Then
                    CellI.EntireRow.Interior.ColorIndex = xlNone
                    End If
                                        
                    Next
                    
        
        CellD.EntireRow.Interior.ColorIndex = 3
        End If
        
        If CellD.Value <> "Other" Then
        CellD.EntireRow.Interior.ColorIndex = xlNone
        End If

Next

For Each CellI In MyPlageI
                    If CellI.Value <> "" Then
                    CellI.EntireRow.Interior.ColorIndex = xlNone
                    End If

Next


'**************************




'**************************



End Sub



Private Function HasValidation(r) As Boolean


'   Returns True if every cell in Range r uses Data Validation
   On Error Resume Next
  x = r.Validation.Type
 If Err.Number = 0 Then HasValidation = True Else HasValidation = False
End Function
 
Upvote 0

Forum statistics

Threads
1,223,900
Messages
6,175,276
Members
452,629
Latest member
SahilPolekar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top