Private Sub asking me to name it before I run

cmaize

New Member
Joined
Aug 11, 2011
Messages
3
I had found some code about hiding and unhiding cells based on specific entry in another cell which I have changed to hopefully fit my needs.

My problem is that when I try to run it from the VBA screen it asks me to name it in the Macros Name screen. If I type in the name and click create, it takes back to VBA with just the shell code. If I paste the code into that shell and try to run it, I'm back to the Macro naming screen.

I'm sure I'm doing something stupid, but I've searched on everything I think is logical. Any help would be very appreciated it. Thank you for your help and for all the other help you have unknowingly been providing.

Here is the code I'm using:


Private Sub Worksheet_Change(ByVal Target As Range)
'Hide cells based on entry in Column E, column E is chosen from a drop down list

If WorksheetFunction.CountIf(Columns(5), 1) = 1 Then
Columns("H:O").Hidden = True
If WorksheetFunction.CountIf(Columns(5), 1) = 2 Then
Columns("J:O").Hidden = True
If WorksheetFunction.CountIf(Columns(5), 1) = 3 Then
Columns("L:O").Hidden = True
If WorksheetFunction.CountIf(Columns(5), 1) = 4 Then
Columns("N:O").Hidden = True
If WorksheetFunction.CountIf(Columns(5), 1) = 5 Then
Columns("F:O").Hiddent = False

Else
Columns("H:O").Hidden = True

End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Post deleted by Sykes.
 
Last edited:
Upvote 0
Try posting your code.


I see that you have now.

Where did you put the code? you need to right click the sheet tab, select View Code then paste in the code. It will then run automatically when you make changes to the sheet.
 
Upvote 0
Try replacing that with

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Hide cells based on entry in Column E, column E is chosen from a drop down list

If WorksheetFunction.CountIf(Columns(5), 1) = 1 Then
    Columns("H:O").Hidden = True
ElseIf WorksheetFunction.CountIf(Columns(5), 1) = 2 Then
    Columns("J:O").Hidden = True
ElseIf WorksheetFunction.CountIf(Columns(5), 1) = 3 Then
    Columns("L:O").Hidden = True
ElseIf WorksheetFunction.CountIf(Columns(5), 1) = 4 Then
    Columns("N:O").Hidden = True
ElseIf WorksheetFunction.CountIf(Columns(5), 1) = 5 Then
    Columns("F:O").Hiddent = False
Else
    Columns("H:O").Hidden = True
End If
End Sub
 
Upvote 0
Hi, and Welcome to Mr Excel (even if you've been using the site for some time!!)

The worksheet_change event will behave like this if you try and run it from the VBA browser.

As Peter said - you need to actually make a change in the sheet in order for the code to fire...........then you'll realise that the code is erroring due to "Block if without end if"

Peter's changed code will do the trick.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,726
Members
452,939
Latest member
WCrawford

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