Code for command button to unfill cell by cell

nayrbnalhguoc

New Member
Joined
Feb 3, 2023
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
I have a data set with columns and headers as normal. I want to reveal the data cell by cell using a command button so every time I click the button it reveals the data in each cell e.g b2, b3, b4 ,b5 etc and then moves on to C2, C3 C4 and so on. Does anyone have code to do that so it tabs automatically the whole way down the table without having to click the cells individually? Not sure if the below pic is a VBA interface or excel sheet using command button. Looking for something along these lines to reveal scores at a sports event.
Screenshot_20230203-182807_YouTube.jpg
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
The following code (linked to a command button) finds the last used row in column A and uses that as the last row to scan in each column. It finds the last used column in row 1, too. It then loops through each row of each column (starting in column 2, row 2) and checks if the cell fill (interior) color is black (1). If so, it sets it to no fill (0) and then exits the macro.
VBA Code:
Private Sub CommandButton1_Click()
Dim i As Long, j As Long, lastrow As Long, lastcol As Long, arr() As Variant
lastrow = Range("A" & Rows.Count).End(xlUp).Row
lastcol = Range("XFD1").End(xlToLeft).Column

For i = 2 To lastcol
    For j = 2 To lastrow
        If Cells(j, i).Interior.ColorIndex = 1 Then
            Cells(j, i).Interior.ColorIndex = 0
            Exit Sub
        End If
    Next j
Next i
End Sub
 
Upvote 0
The following code (linked to a command button) finds the last used row in column A and uses that as the last row to scan in each column. It finds the last used column in row 1, too. It then loops through each row of each column (starting in column 2, row 2) and checks if the cell fill (interior) color is black (1). If so, it sets it to no fill (0) and then exits the macro.
VBA Code:
Private Sub CommandButton1_Click()
Dim i As Long, j As Long, lastrow As Long, lastcol As Long, arr() As Variant
lastrow = Range("A" & Rows.Count).End(xlUp).Row
lastcol = Range("XFD1").End(xlToLeft).Column

For i = 2 To lastcol
    For j = 2 To lastrow
        If Cells(j, i).Interior.ColorIndex = 1 Then
            Cells(j, i).Interior.ColorIndex = 0
            Exit Sub
        End If
    Next j
Next
[/QUOTE]

The following code (linked to a command button) finds the last used row in column A and uses that as the last row to scan in each column. It finds the last used column in row 1, too. It then loops through each row of each column (starting in column 2, row 2) and checks if the cell fill (interior) color is black (1). If so, it sets it to no fill (0) and then exits the macro.
VBA Code:
Private Sub CommandButton1_Click()
Dim i As Long, j As Long, lastrow As Long, lastcol As Long, arr() As Variant
lastrow = Range("A" & Rows.Count).End(xlUp).Row
lastcol = Range("XFD1").End(xlToLeft).Column

For i = 2 To lastcol
    For j = 2 To lastrow
        If Cells(j, i).Interior.ColorIndex = 1 Then
            Cells(j, i).Interior.ColorIndex = 0
            Exit Sub
        End If
    Next j
Next i
End Sub

Thanks so much for this @Z51
This starts at row 2, col B and works down B3, B4 etc as required.

Is it possible to reveal columns A & B row 2 (2 Cells simultaneously) to start with all the way down and then move to col C row 2 (1 cell)? How do I amend the above code to do that.
 
Upvote 0
Added a check to see if the current column number (i) is 2 (column B), and if so, change the background color of the adjacent cell in column A as well.
VBA Code:
Private Sub CommandButton1_Click()
Dim i As Long, j As Long, lastrow As Long, lastcol As Long, arr() As Variant
lastrow = Range("A" & Rows.Count).End(xlUp).Row
lastcol = Range("XFD1").End(xlToLeft).Column

For i = 2 To lastcol
    For j = 2 To lastrow
        If Cells(j, i).Interior.ColorIndex = 1 Then
            If i = 2 Then Cells(j, i - 1).Interior.ColorIndex = 0
            Cells(j, i).Interior.ColorIndex = 0
            Exit Sub
        End If
    Next j
Next i
End Sub
 
Upvote 0
Thank you so much - that is exactly what I am looking for for this table!!
Could I be cheeky then to ask one more question:
I am also looking to develop another worksheet with a similar table all in black but i want to reveal each row in full (by unfilling the black colour). Is it possible to send on code to allow the button to unfill each row of black in full starting at row 2?
 
Upvote 0
VBA Code:
Private Sub CommandButton1_Click()
Dim i As Long, lastcol As Long
lastcol = Range("XFD1").End(xlToLeft).Column

For i = 2 To Range("A" & Rows.Count).End(xlUp).Row
    If Cells(i, 1).Interior.ColorIndex = 1 Then
        Range(Cells(i, 1), Cells(i, lastcol)).Interior.ColorIndex = 0
        Exit Sub
    End If
Next i
End Sub
 
Upvote 0
VBA Code:
Private Sub CommandButton1_Click()
Dim i As Long, lastcol As Long
lastcol = Range("XFD1").End(xlToLeft).Column

For i = 2 To Range("A" & Rows.Count).End(xlUp).Row
    If Cells(i, 1).Interior.ColorIndex = 1 Then
        Range(Cells(i, 1), Cells(i, lastcol)).Interior.ColorIndex = 0
        Exit Sub
    End If
Next i
End Sub
Thank you for all the help. Much appreciated
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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