VBA Hide rows based on button selection

anand3dinesh

Board Regular
Joined
Dec 19, 2019
Messages
137
Office Version
  1. 365
Platform
  1. Windows
Hi Please some one help me to find the solution for the below question?

I have 2 sheets called Buttons and Database as shown in the below image.
Upon button selection I would like to show only selected Name/Skills in Database sheet.

eg: If "Dinesh" button selected show only Dinesh rows in Database
If "Excel" Button selected then i would like to show only Excel rows in Database
IF "Dinesh" and "Excel" both button selected then I would like to show only Dinesh and Excel in Database.

I hope my question is clear.
Thanks in advance

VBA Buttons q.JPG
VBA DB q.JPG
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
I am not sure this is what you are after but here is a shot. Name is in column A on sheet Database. Skills are in Column B.

Also, wouldn't this be easier without the buttons or VBA and instead just use a filter?

VBA Code:
Sub Button1_Click()

    ' Dinesh Button
    
    Dim numRows As Long
    
    numRows = Sheets("Database").Range("A:A").End(xlDown).Row           'find the last row

    For i = 2 To numRows                                                'loop all rows
        If Sheets("Database").Cells(i, 1) = "Dinesh" Then               'if this is the person we are looking for
           Sheets("Database").Cells(i, 1).Font.Color = vbBlack          'show their name
           Sheets("Database").Cells(i, 2).Font.Color = vbBlack          'show their skill
        Else                                                            'not our person
            Sheets("Database").Cells(i, 1).Font.Color = vbWhite         'Hide their name
            Sheets("Database").Cells(i, 2).Font.Color = vbWhite         'hide their skill
        End If
    Next
End Sub
Sub Button2_Click()

    ' Harry Button
    
    Dim numRows As Long
    
    numRows = Sheets("Database").Range("A:A").End(xlDown).Row

    For i = 2 To numRows
        If Sheets("Database").Cells(i, 1) = "Harry" Then
           Sheets("Database").Cells(i, 1).Font.Color = vbBlack
           Sheets("Database").Cells(i, 2).Font.Color = vbBlack
        Else
            Sheets("Database").Cells(i, 1).Font.Color = vbWhite
            Sheets("Database").Cells(i, 2).Font.Color = vbWhite
        End If
    Next
End Sub
Sub Button3_Click()
    ' Excel Button
    
    Dim numRows As Long
    
    numRows = Sheets("Database").Range("A:A").End(xlDown).Row

    For i = 2 To numRows

        If Sheets("Database").Cells(i, 2) = "Excel" And Sheets("Database").Cells(i, 1).Font.Color = vbBlack Then
            'if this is the skill and the persons name is visible
           Sheets("Database").Cells(i, 2).Font.Color = vbBlack
        Else
            Sheets("Database").Cells(i, 2).Font.Color = vbWhite
            Sheets("Database").Cells(i, 1).Font.Color = vbWhite
        End If
        
    Next
End Sub
 
Upvote 0
I am not sure this is what you are after but here is a shot. Name is in column A on sheet Database. Skills are in Column B.

Also, wouldn't this be easier without the buttons or VBA and instead just use a filter?

VBA Code:
Sub Button1_Click()

    ' Dinesh Button
   
    Dim numRows As Long
   
    numRows = Sheets("Database").Range("A:A").End(xlDown).Row           'find the last row

    For i = 2 To numRows                                                'loop all rows
        If Sheets("Database").Cells(i, 1) = "Dinesh" Then               'if this is the person we are looking for
           Sheets("Database").Cells(i, 1).Font.Color = vbBlack          'show their name
           Sheets("Database").Cells(i, 2).Font.Color = vbBlack          'show their skill
        Else                                                            'not our person
            Sheets("Database").Cells(i, 1).Font.Color = vbWhite         'Hide their name
            Sheets("Database").Cells(i, 2).Font.Color = vbWhite         'hide their skill
        End If
    Next
End Sub
Sub Button2_Click()

    ' Harry Button
   
    Dim numRows As Long
   
    numRows = Sheets("Database").Range("A:A").End(xlDown).Row

    For i = 2 To numRows
        If Sheets("Database").Cells(i, 1) = "Harry" Then
           Sheets("Database").Cells(i, 1).Font.Color = vbBlack
           Sheets("Database").Cells(i, 2).Font.Color = vbBlack
        Else
            Sheets("Database").Cells(i, 1).Font.Color = vbWhite
            Sheets("Database").Cells(i, 2).Font.Color = vbWhite
        End If
    Next
End Sub
Sub Button3_Click()
    ' Excel Button
   
    Dim numRows As Long
   
    numRows = Sheets("Database").Range("A:A").End(xlDown).Row

    For i = 2 To numRows

        If Sheets("Database").Cells(i, 2) = "Excel" And Sheets("Database").Cells(i, 1).Font.Color = vbBlack Then
            'if this is the skill and the persons name is visible
           Sheets("Database").Cells(i, 2).Font.Color = vbBlack
        Else
            Sheets("Database").Cells(i, 2).Font.Color = vbWhite
            Sheets("Database").Cells(i, 1).Font.Color = vbWhite
        End If
       
    Next
End Sub
Thanks for response but, It works only when one button is clicked. i don't think it works when both Name and Skill selected?
 
Upvote 0
It works for me. It obviously needs more work because there are only 3 buttons implemented.
1671633171535.png

The Dinesh button call Button1_Click(). The Harry button calls Button2_Click() and the Excel button calls Button3_Click()

When I click the Excel button I get
1671633314174.png


When click the Dinesh button
1671633366298.png


When I click the Dinesh and then the Excel I get
1671633411218.png
 
Upvote 0
It works for me. It obviously needs more work because there are only 3 buttons implemented.
View attachment 81327
The Dinesh button call Button1_Click(). The Harry button calls Button2_Click() and the Excel button calls Button3_Click()

When I click the Excel button I get
View attachment 81328

When click the Dinesh button
View attachment 81329

When I click the Dinesh and then the Excel I get
View attachment 81330
I figured it out, it requires only 2 subs to do that one sub for Name col and one to Skills Col.
I want to hide the entire row not just match colour property as background.

i used autofilter method and it worked.
 
Upvote 0
Upvote 0

Forum statistics

Threads
1,214,925
Messages
6,122,301
Members
449,078
Latest member
nonnakkong

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