Hide Rows on cell value.

adamsm

Active Member
Joined
Apr 20, 2010
Messages
444
Hi,

The following code I'm using filters rows on cell value. How could I make the code to hide the rows instead of filtering?

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If UCase(Sheets("Orders").Range("D17").Value) <> "ALL" Then
        Sheets("Orders").Range("C17").AutoFilter Field:=2, Criteria1:=Range("O6").Text
    Else
        If Sheets("Orders").AutoFilterMode Then
            Sheets("Orders").Range("C17").AutoFilter
        End If
    End If
End Sub
Any help on this would be kindly appreciated.

Thanks in advance.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
It is not clears why you wrote the macro as sheet code(event code). It should be in standard module

suppose sample data is like thsi

<table border="0" cellpadding="0" cellspacing="0" width="320"><col style="width: 48pt;" span="5" width="64"> <tbody><tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt; width: 48pt;" height="17" width="64">hdng1</td> <td style="width: 48pt;" width="64">hdng2</td> <td style="width: 48pt;" width="64">hdng3</td> <td style="width: 48pt;" width="64">hdng4</td> <td style="width: 48pt;" width="64">hdng5</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">
</td> <td>
</td> <td>
</td> <td>all</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">
</td> <td>
</td> <td>
</td> <td>x</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">
</td> <td>
</td> <td>
</td> <td>x</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">
</td> <td>
</td> <td>
</td> <td>all</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">
</td> <td>
</td> <td>
</td> <td>all</td> <td>
</td> </tr> <tr style="height: 12.75pt;" height="17"> <td style="height: 12.75pt;" height="17">
</td> <td>
</td> <td>
</td> <td>y</td> <td>
</td> </tr> </tbody></table>
try this macro (in a sandard module)

Code:
Sub test()
Dim j As Integer, k As Integer
Worksheets("orders").Activate
j = Range("D1").End(xlDown).Row
For k = j To 2 Step -1
If Cells(k, "D") <> "all" Then Cells(k, "D").EntireRow.Hidden = True
Next k
End Sub
 
Upvote 0
How could the code be changed so that it hides rows based on textbox1 value?

For example the user writes "all" in textbox1 and clicks the button. All the rows except the rows containing the text "all" gets hidden.

Any help on this would be kindly appreciated.

Thanks in advance.
 
Upvote 0
As I said early keep a copy of the original data in the sheet "orders" in sheet 2 also for undoing the macro


I am not very familiar with text box. However I did some experiments.
It looks there is no text box in forms toolbar and you cannot assign the macro to a text box from drawing toolbar. You have to create a text box from control toolbar. after you create this text box
click the design mode (in the vbeditor toolbar)

if you right click and click properties you see the name of the textbox is "textbox1". i kept the name as it is
then again right click the textbox and click view code
in the window that comes up copy this code

Code:
Private Sub TextBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim j As Integer, k As Integer, x As String
Worksheets("orders").Activate
j = Range("D1").End(xlDown).Row
x = TextBox1.Text
'MsgBox x
For k = j To 2 Step -1
'Cells(k, "D").Select
'MsgBox Cells(k, "D") <> x
If Cells(k, "D") <> x Then Cells(k, "D").EntireRow.Hidden = True
Next k

End Sub
now exit design mode.

now doubleclick the text box and see what happens.

perhaps this can be done even if you create a textbox through userform.
 
Upvote 0
Thanks for the help. But sorry for being late to respond.

Anyways, I do really appreciate your help.
 
Upvote 0

Forum statistics

Threads
1,224,612
Messages
6,179,890
Members
452,948
Latest member
Dupuhini

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