VBA Hide Rows

MelissaM

New Member
Joined
May 6, 2014
Messages
3
Hi

I used the following code to hide rows based on a cell value:

Private Sub Worksheet_Calculate()
Dim MyResult As String

Application.EnableEvents = False

Rows("11:" & Worksheets("Vouchers").UsedRange.Rows.Count).EntireRow.Hidden = False

MyResult = Worksheets("Vouchers").Cells(11, 3).Value

Select Case MyResult

Case "", "None"
Rows("3:37").EntireRow.Hidden = True

Case Else
Rows("3:37").EntireRow.Hidden = False
End Select

MyResult2 = Worksheets("Vouchers").Cells(65, 3).Value

Select Case MyResult2

Case "", "None"
Rows("58:72").EntireRow.Hidden = True

Case Else
Rows("58:72").EntireRow.Hidden = False
End Select

Application.EnableEvents = True
End Sub

It worked fine when I tested it, but once I closed the workbook and reopened it it seemed to stop working. I thought it might have been because I had protected the worksheet so I unprotected it and re-entered the code but it is still not working. The workbook is a macro enabled workbook and all the cell references have been double checked. Any ideas?
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi,
your code works ok but will only trigger each time sheet calculates - if this is what is required, check if calculation been set to manual? If though, you expect it to operate when YOU make a cell change then you need a different event.

you should be able to shorten code a little:

Code:
Private Sub Worksheet_Calculate()
    Application.EnableEvents = False
    With Worksheets("Vouchers")
        .Rows("11:" & .UsedRange.Rows.Count).EntireRow.Hidden = False
        .Rows("3:37").EntireRow.Hidden = .Cells(11, 3).Value = "None"
        .Rows("58:72").EntireRow.Hidden = .Cells(65, 3).Value = "None"
    End With
    Application.EnableEvents = True
End Sub

Dave
 
Upvote 0
I have checked the calculation and it is set to Automatic. I have tried pasting in the code above and that didn't seem to want to work for me either. I repasted the original code and that didn't fix the problem.

What I have done is created some travel vouchers for clients to give to suppliers and, depending on what options are on their trip, I want to hide the rows containing the vouchers that are not required as I save them as a PDF and email them out. The information for the vouchers is coming from their confirmation (in a seperate tab), and that information is coming from the bookings spreadsheet via IF statements and VLOOKUP's. When I change the booking reference on the confirmation the trip information pulls through to the confirmation and filters through to the vouchers. If there is no ferry operator or accommodation provider pulled through (cells 11,3 & 65,3) I want to be able to hide the vouchers relating to those services. It was working perfectly yesterday when I set it up but 10 minutes later, after having closed the spreadsheet and logged off the external server, it didn't want to work again.
 
Upvote 0
Hi,
where have you pasted the code?

The code should be placed in your worksheets code page that performs the calculation - Right Click Tab > View Code


Dave
 
Upvote 0
Then should work

Just a thought, try running ths bit of code & then see if your event works for you.

Code:
Sub reset()
Application.EnableEvents = True
End Sub

if still no joy, are you able to place sample copy of your workbook in a dropbox?

Dave
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,198
Members
449,072
Latest member
DW Draft

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