Error run-time 13 with VBA - Excel

FlavioMorrone

New Member
Joined
Mar 5, 2018
Messages
2
Hi everybody,
I am trying to run the following coding in the macro:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 4 And Target.Row = 2 And Target.Value = "5" Then
        Application.Columns("L:P").Select
        Application.Selection.EntireColumn.Hidden = True
    Else
        Application.Columns("L:P").Select
        Application.Selection.EntireColumn.Hidden = False
    End If
End Sub

My goal is to hide columns from L to P based on the selection on the dropdown list in cell D2. However, by running the code, I bump into a series of problems:

  • A window pops out saying "Error 13 run-time" everytime I insert or delete any row
  • Many times, while working on the Excel sheet, I automatically happen to select the columns L:P even against my will, and this turns out to be quite annoying.

Would like to ask for help to solve these two issues, thank you so much in advance!
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi. See if this works:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim rng As Range

Set rng = Intersect(Target, Cells(2, 4))
If Not rng Is Nothing Then
   Columns("L:P").Hidden = (rng.Value = 5)
End If

End Sub
 
Upvote 0
Its going to produce a true or a false based on the value of the cell. This is more of a toggle in my mind:

Code:
Columns("L:P").Hidden = Not(Columns("L:P").Hidden)
 
Upvote 0
It is in my mind too what I am asking is if range.value does not = 5 do the columns re-appear
 
Upvote 0
Oh sorry yes they do because the test in the brackets is then false.
 
Upvote 0

Forum statistics

Threads
1,215,140
Messages
6,123,267
Members
449,093
Latest member
Vincent Khandagale

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