Error Code 400 in VBA help

andrewb90

Well-known Member
Joined
Dec 16, 2009
Messages
1,077
I am hoping this is pretty minor, but I have a macro that I am trying to run, and when I do, it seems to interfere with my selection change code on my sheet.

They both work independently, but when they are on the same sheet I have issues. Can anybody help with this?

Here is one of my macros that I run:

Code:
Sub filter()
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Dim Cell As Range
    With ActiveSheet
        Unprotect "majinbuu"
        For Each Cell In .Range("E4:EE4").Cells
            If .Range("A4").Value = "" Then
                With Cell
                    .EntireColumn.Hidden = .Value <> .Parent.Range("A3").Value
                End With
            Else
                With Cell
                    .EntireColumn.Hidden = Not ((.Value >= .Parent.Range("A3").Value) And (.Value <= .Parent.Range("A4").Value))
                End With
            End If
        Next Cell
        Protect "majinbuu"
    End With
    Application.ScreenUpdating = True
    Application.EnableEvents = True
End Sub

Here is my selection change:
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    'Line Highlighting Tool
    Range("B6:EE16").Interior.Color = xlNone 'range that get colored
    Set S = Application.Intersect(Range(Target.Address), Range("B6:B16")) 'range to click on
    If Not S Is Nothing Then
        Range("B" & Target.Row & ":EE" & Target.Row).Interior.ColorIndex = 22  'total column range
    End If

    Range("B18:EE28").Interior.Color = xlNone 'range that get colored
    Set S = Application.Intersect(Range(Target.Address), Range("B18:B28")) 'range to click on
    If Not S Is Nothing Then
        Range("B" & Target.Row & ":EE" & Target.Row).Interior.ColorIndex = 22  'total column range
    End If
    
    Range("B30:EE46").Interior.Color = xlNone 'range that get colored
    Set S = Application.Intersect(Range(Target.Address), Range("B30:B46")) 'range to click on
    If Not S Is Nothing Then
        Range("B" & Target.Row & ":EE" & Target.Row).Interior.ColorIndex = 22  'total column range
    End If

    Range("B48:EE57").Interior.Color = xlNone 'range that get colored
    Set S = Application.Intersect(Range(Target.Address), Range("B48:B57")) 'range to click on
    If Not S Is Nothing Then
        Range("B" & Target.Row & ":EE" & Target.Row).Interior.ColorIndex = 22  'total column range
    End If
    
    Range("B59:EE89").Interior.Color = xlNone 'range that get colored
    Set S = Application.Intersect(Range(Target.Address), Range("B59:B89")) 'range to click on
    If Not S Is Nothing Then
        Range("B" & Target.Row & ":EE" & Target.Row).Interior.ColorIndex = 22  'total column range
    End If

    Range("B91:EE100").Interior.Color = xlNone 'range that get colored
    Set S = Application.Intersect(Range(Target.Address), Range("B91:B100")) 'range to click on
    If Not S Is Nothing Then
        Range("B" & Target.Row & ":EE" & Target.Row).Interior.ColorIndex = 22  'total column range
    End If
    
    Range("B102:EE110").Interior.Color = xlNone 'range that get colored
    Set S = Application.Intersect(Range(Target.Address), Range("B102:B110")) 'range to click on
    If Not S Is Nothing Then
        Range("B" & Target.Row & ":EE" & Target.Row).Interior.ColorIndex = 22  'total column range
    End If
    
    Range("B112:EE136").Interior.Color = xlNone 'range that get colored
    Set S = Application.Intersect(Range(Target.Address), Range("B112:B136")) 'range to click on
    If Not S Is Nothing Then
        Range("B" & Target.Row & ":EE" & Target.Row).Interior.ColorIndex = 22  'total column range
    End If
    
    Range("B138:EE145").Interior.Color = xlNone 'range that get colored
    Set S = Application.Intersect(Range(Target.Address), Range("B138:B145")) 'range to click on
    If Not S Is Nothing Then
        Range("B" & Target.Row & ":EE" & Target.Row).Interior.ColorIndex = 22  'total column range
    End If
    
    Range("B147:EE156").Interior.Color = xlNone 'range that get colored
    Set S = Application.Intersect(Range(Target.Address), Range("B147:B156")) 'range to click on
    If Not S Is Nothing Then
        Range("B" & Target.Row & ":EE" & Target.Row).Interior.ColorIndex = 22  'total column range
    End If
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
You need a period in front of .Unprotect and .Protect. That's one issue, but I don't know if it's the issue.

Code:
Sub filter()
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Dim Cell As Range
    With ActiveSheet
        [COLOR="Red"].[/COLOR]Unprotect "majinbuu"
        For Each Cell In .Range("E4:EE4").Cells
            If .Range("A4").Value = "" Then
                With Cell
                    .EntireColumn.Hidden = .Value <> .Parent.Range("A3").Value
                End With
            Else
                With Cell
                    .EntireColumn.Hidden = Not ((.Value >= .Parent.Range("A3").Value) And (.Value <= .Parent.Range("A4").Value))
                End With
            End If
        Next Cell
        [COLOR="Red"].[/COLOR]Protect "majinbuu"
    End With
    Application.ScreenUpdating = True
    Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,270
Messages
6,171,102
Members
452,379
Latest member
IainTru

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