Need VBA Help on Unhiding Row Based on Value + Rows Below

gwomfb

New Member
Joined
Jan 23, 2023
Messages
4
Office Version
  1. 2013
Platform
  1. Windows
I tried putting together a code for a checkbox in Sheet "Budget" which will unhide 4 rows on Sheet "Vendor Cost". When the checkbox is ticked, it should unhide the row on Sheet "Vendor Cost" which contains value "1000L" on column A and 3 more rows underneath it (no value on column A). I can't specify row numbers because there is an option in the sheet to add more rows so column A entries may change row address.

Here's what I have so far but not working.

Private Sub CheckBox1_Click() Dim c As Range

Application.ScreenUpdating = False
With ThisWorkbook.Worksheets("Budget")
For Each c In ThisWorkbook.Worksheets("Vendor Cost").Range("A4:A1000" & ThisWorkbook.Worksheets("Vendor Cost").Cells(.Rows.Count, "A").End(xlUp).Row).Cells
If IsError(c) Then c.Value2 = "1000L" 'Hides errors as well
c.Resize(4).EntireRow.Hidden = (c.Value = "1000L" And ThisWorkbook.Worksheets("Vendor Cost").CheckBox1.Value) = False
Next
End With
Application.ScreenUpdating = True

End Sub

Can anyone take a look and teach me where I'm wrong? Please and thank you.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi,

according to description
for a checkbox in Sheet "Budget"

while in code
Rich (BB code):
ThisWorkbook.Worksheets("Vendor Cost").CheckBox1


Maybe try

VBA Code:
Private Sub CheckBox1_Click()
Dim c As Range

Application.ScreenUpdating = False
With ThisWorkbook.Worksheets("Vendor Cost")
  For Each c In .Range("A4", .Cells(.Rows.Count, "A").End(xlUp))
   'If you find a matching value, indicate success by setting bln to true and exit the loop;
   'otherwise, continue searching until you reach the end of the workbook.
    If IsError(c) Then c.Value2 = "1000L" 'Hides errors as well
    If c.Value = "1000L" Then c.Resize(4).EntireRow.Hidden = ThisWorkbook.Worksheets("Budget").CheckBox1.Value
  Next c
End With
Application.ScreenUpdating = True
End Sub

Ciao,
Holger
 
Upvote 0
Hi,

according to description


while in code
Rich (BB code):
ThisWorkbook.Worksheets("Vendor Cost").CheckBox1


Maybe try

VBA Code:
Private Sub CheckBox1_Click()
Dim c As Range

Application.ScreenUpdating = False
With ThisWorkbook.Worksheets("Vendor Cost")
  For Each c In .Range("A4", .Cells(.Rows.Count, "A").End(xlUp))
   'If you find a matching value, indicate success by setting bln to true and exit the loop;
   'otherwise, continue searching until you reach the end of the workbook.
    If IsError(c) Then c.Value2 = "1000L" 'Hides errors as well
    If c.Value = "1000L" Then c.Resize(4).EntireRow.Hidden = ThisWorkbook.Worksheets("Budget").CheckBox1.Value
  Next c
End With
Application.ScreenUpdating = True
End Sub

Ciao,
Holger

Hi, thanks for the help. I tried fresh with the code you edited but it's not doing anything unfortunately. By default, rows A4+ on Vendor Cost are hidden. I want the checkbox which is in Budget to unhide. I tried using it with the rows unhidden and ran the code and it hid them so I think it does the opposite?
 
Upvote 0
Hi,

according to description


while in code
Rich (BB code):
ThisWorkbook.Worksheets("Vendor Cost").CheckBox1


Maybe try

VBA Code:
Private Sub CheckBox1_Click()
Dim c As Range

Application.ScreenUpdating = False
With ThisWorkbook.Worksheets("Vendor Cost")
  For Each c In .Range("A4", .Cells(.Rows.Count, "A").End(xlUp))
   'If you find a matching value, indicate success by setting bln to true and exit the loop;
   'otherwise, continue searching until you reach the end of the workbook.
    If IsError(c) Then c.Value2 = "1000L" 'Hides errors as well
    If c.Value = "1000L" Then c.Resize(4).EntireRow.Hidden = ThisWorkbook.Worksheets("Budget").CheckBox1.Value
  Next c
End With
Application.ScreenUpdating = True
End Sub

Ciao,
Holger
Ah I tweaked it and finally got it working. Thanks @HaHoBe .

Working code:

Private Sub CheckBox1_Click()
Dim c As Range

Application.ScreenUpdating = False
With ThisWorkbook.Worksheets("Vendor Cost")
For Each c In .Range("A4:A500", .Cells(.Rows.Count, "A").End(xlUp))
'If you find a matching value, indicate success by setting bln to true and exit the loop;
'otherwise, continue searching until you reach the end of the workbook.
If IsError(c) Then c.Value2 = "1000L" 'Hides errors as well
If c.Value = "1000L" Then c.Resize(4).EntireRow.Hidden = ThisWorkbook.Worksheets("Budget").CheckBox1.Value = False
Next c
End With
Application.ScreenUpdating = True
End Sub

Let me know if it makes sense why it works or if you see any trouble I may encounter in the future.
 
Upvote 0
Hi gwomfb,

please make good use of code-tags when posting your procedures here.

VBA Code:
    If c.Value = "1000L" Then c.Resize(4).EntireRow.Hidden = Not ThisWorkbook.Worksheets("Budget").CheckBox1.Value

does the same.

Holger
 
Upvote 0
Hi gwomfb,

please make good use of code-tags when posting your procedures here.

VBA Code:
    If c.Value = "1000L" Then c.Resize(4).EntireRow.Hidden = Not ThisWorkbook.Worksheets("Budget").CheckBox1.Value

does the same.

Holger
Ah I'll try that.

Yeah I tried editing but couldn't afterwards :/

Thanks again!
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,691
Members
448,978
Latest member
rrauni

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