VBA Conditional Row Delete Question

strathybananas

New Member
Joined
Mar 15, 2011
Messages
9
Hey Guys,

I am currently creating a programme which uses a drop down box, and I want to get rid of different options depending on the box selected.

However, I am having trouble getting rid of the rows which contain 'HIDE', which I have labelled as needing to be hidden (row height = 0).

I have been using a borrowed VBA code which gets rid of the 'HIDE' rows, but won't return them for the relevent boxes! (if that makes sense?!)

The code is:


Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
' Dim R As Range
' Set R = Application.Intersect(Target, Range("A1:E50"))
' If R Is Nothing Then Exit Sub
With ActiveSheet
For Each cell In Range("A1:E50")
If cell.Value = "HIDE" Then
cell.EntireRow.Hidden = True
End If
Next
End With
Application.ScreenUpdating = True
End Sub


Essentially, what I'm asking is how can I alter this code to return my 'HIDE' boxes when they no longer =HIDE?

Thanks,

Johnnie
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try

Code:
For Each cell In Range("A1:E50")
    cell.EntireRow.Hidden = cell.Value = "HIDE"
Next
 
Upvote 0
Try this

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
Application.ScreenUpdating = False
For Each cell In Range("A1:E50")
    cell.EntireRow.Hidden = cell.Value = "HIDE"
Next cell
Application.ScreenUpdating = True
End Sub
 
Upvote 0
VoG,

That hides the first 'HIDE' cell I have, but the rest of them are still there? Do I need to loop the code to allow for excel to hide all of the 'HIDE' cells between A:1 and E:50?
 
Upvote 0
Try this

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long
Application.ScreenUpdating = False
For i = 1 To 50
    Rows(i).Hidden = WorksheetFunction.CountIf(Range("A" & i & ":E" & i), "HIDE") > 0
Next i
Application.ScreenUpdating = True
End Sub
 
Upvote 0
VoG,

Thanks so much for the help!

I had one final question, which was if I was to change it from hiding rows to hiding other sheets depending on the values in the cells, would this be possible?

Say I had a value on Sheet1 which, when at HIDE, would hide Sheet 3 and 4.

What would I input for that?

Thanks,

Johnnie
 
Upvote 0
Not terribly clear. Maybe

Code:
If Sheets("Sheet1").Range("A1").Value = "Hide" Then
    Sheets("Sheet3").Visible = xlSheetHidden
    Sheets("Sheet4").Visible = xlSheetHidden
End If
 
Upvote 0
Ok so the value C2 of sheet, Origin, is HIDE.

I want to hide sheet, Area, as well as sheet, Ocean.

Is that hopefully a bit clearer?

Thanks!

Johnnie
 
Upvote 0
Try

Code:
If Sheets("Origin").Range("C2").Value = "HIDE" Then
    Sheets("Area").Visible = xlSheetHidden
    Sheets("Ocean").Visible = xlSheetHidden
End If
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

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