Hi Christy,
I'm glad to see that first part worked out. Now it is time to replace the MsgBox "Hello Christy" with your solution code.
First lets make a new workbook. In "Sheet1" create a table with some data starting at cell A6:
table1_C.PNG
Now let's create two buttons (like you did in the previous post). Once they are created, put them at the top and edit their properties in the Visual Basic Editor (to get to the editor press alt+f11). Don't see the properties for the buttons? Go to "View" and then "Properties Window" to open the properties window.
You can locate the names Command Button1 and Command Button2 in the properties window under (Name)
For the button "Command Button1" we will set the following properties:
-Caption: Hide SCL Rows
-Height: 30
-Width: 180
For the button "Command Button2" we will the set the following properties:
-Caption: Unhide All Rows
-Height: 30
-Width: 180
Align the buttons near the top. Your worksheet should now look similar to this:
buttons1_C.PNG
Now it is time to add some code to your buttons.
Let's add code to the Hide SCL Rows button (Command Button1). Go into Excel and double click on the button (make sure you are in Design Mode).
Now you will enter code in it to look like this:
Private Sub CommandButton1_Click()
With Worksheets("Sheet1")
Dim intLastRow As Integer
Dim rngColumnA As Range
Dim cellColumnA As Range
intLastRow = .Cells(1048576, 1).End(xlUp).Row
Set rngColumnA = .Range(.Cells(7, 1), .Cells(intLastRow, 1))
For Each cellColumnA In rngColumnA
If cellColumnA.Value = "SCL" Then
cellColumnA.EntireRow.Hidden = True
End If
Next cellColumnA
End With
End Sub
There is a lot going on here. I'll break down each step:
Code:
With Worksheets("Sheet1")
...
End With
"With Worksheets..." code allows you to trap whatever code you are writing within the worksheet specified. In this case we are working with Sheet1. Now anytime we refer to cells or ranges the application will understand we are referring to only those found within Sheet1.
Dim intLastRow As Integer
Dim rngColumnA As Range
Dim cellColumnA As Range
Dim is short for dimension. Dimensioning a variable or I believe some call it "instantiating" simply allows you to establish what type of variable it is that you are creating.
An integer is a whole number and a range is a collection of cells (i.e., Cells A1 through A10).
intLastRow = .Cells(1048576, 1).End(xlUp).Row
Here we are setting a value to the variable we just declared. We are saying that this number intLastRow is equal to the last cell in column A.
.Cells(1048576, 1).End(xlUp).Row is the code that we use to locate the last row in column A. What it does is it goes to cell row 1048576 (the very last row in Excel) column 1 (first column) and then goes up until it gets to a value. Once it gets to a value it then captures the row that it is on. That is a lot to take in, I know. But it does allow you to get the last row and this is especially useful as the amount of your data changes.
Set rngColumnA = .Range(.Cells(7, 1), .Cells(intLastRow, 1))
Ranges must be "Set" using the "Set" keyword. Here we are setting our variable rngColumnA equal to the range of data we want to look through later. Notice that is uses a range that starts with row 7 column 1 and goes through our last row (we just set this in the last step) and column 1. Now that we know what range to look through we can begin a loop to check for a specific value.
For Each cellColumnA In rngColumnA
If cellColumnA.Value = "SCL" Then
cellColumnA.EntireRow.Hidden = True
End If
Next cellColumnA
This is a For Each loop. It goes through each value in the range we created and asks a question, is the value of the cell I'm in equal to "SCL"? if it is then it will set the entire row for that cell as hidden. This is where your solution rests.
Let's now add some code to unhide all these rows in the second button. This is easy since we don't need to test any condition to know if we should hide the row or not. We will simply unhide all rows within the worksheet Sheet1.
Here is the code to do this:
Private Sub CommandButton2_Click()
Worksheets("Sheet1").Rows.Hidden = False
End Sub
And that's it! If you have successfully entered the code you should be able to hide and unhide the rows at the click of the button. Using the code as a template you can also create more buttons for more rows to hide based off of potentially different criteria.
I know this was a lot to take in. Feel free to ask me any questions and I'll do my best to get back to you.
Bookmarks