help with code to automatically hid rows if a cell is empty

bleeet

Board Regular
Joined
May 11, 2009
Messages
208
Office Version
  1. 2013
Platform
  1. Windows
hello

i am not good with VBA but i want to make a code in one of of the pages in my excel file so that if nothing appears in a cell in the range of C4 to C23 then the row is automatically hidden and and if something appears the row unhides

dunno if this matters but those cells will show info that come from info on another sheet.

i saw this code on another thread here but i don't know how to adjust this to fit my setting


Private Sub Worksheet_Calculate()
Dim i As Long
For i = 9 To 53
Rows(i).Hidden = Range("A" & i).Value = ""
Next i
End Sub

if anyone knwo how to do this or advice on a better idea please help

i hope you guys understand what i am saying
 
Yes, you put the code in the right place.

If the sheet is filtered you will have drop down arrows in row 1.

The code will fail if there are error values like #N/A. Are there errors?
hrmm

i have if statement codes in these cells which are connected to another sheet so if there is no info put on another sheet the rows are hidden

i don't see any #n/a right now

i guess it's not filtered then because i don't see a drop down arrow on row 1
 
Upvote 0

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
This is a more robust version that will handle error values.

Code:
Private Sub Worksheet_Calculate()
Dim i As Long
For i = 4 To 23
    If Not IsError(Range("C" & i)) Then
        Rows(i).Hidden = Range("C" & i).Value = ""
    End If
Next i
End Sub
Try creating a new workbook, add the code, in C1 enter

=A1+B1

and see if it works. If it does then there is some peculiarity with the sheet you were testing it on.
 
Upvote 0
This is a more robust version that will handle error values.

Code:
Private Sub Worksheet_Calculate()
Dim i As Long
For i = 4 To 23
    If Not IsError(Range("C" & i)) Then
        Rows(i).Hidden = Range("C" & i).Value = ""
    End If
Next i
End Sub
Try creating a new workbook, add the code, in C1 enter

=A1+B1

and see if it works. If it does then there is some peculiarity with the sheet you were testing it on.

yea it worked when i created a new workbook

I have if statements in on C collum that stay something if something is inputed on another page in the workbook i don't know if that is the problem

if there is a way i could send it to you so you could see you would probably have a better understanding
 
Upvote 0
Try this

Code:
Private Sub Worksheet_Calculate()
Dim i As Long
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
For i = 4 To 23
    If Not IsError(Range("C" & i)) Then
        Rows(i).Hidden = Range("C" & i).Value = ""
    End If
Next i
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
End Sub
 
Upvote 0
works!! so far so good

thank you so much for your help

i don't knwo what you did but it seems to work now

thanks
 
Upvote 0
sorry another question

how do i protect this sheet and still allow the rows to move and a person to be able to highlight and copy cells?

and sometimes it works then it doesn't after i try to protect it then close the work (not saving) then reopen
 
Last edited:
Upvote 0
Maybe

Code:
Private Sub Worksheet_Calculate()
Dim i As Long
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Me.Unprotect password:="password"
For i = 4 To 23
    If Not IsError(Range("C" & i)) Then
        Rows(i).Hidden = Range("C" & i).Value = ""
    End If
Next i
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Me.Protect password:="password"
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,740
Members
452,940
Latest member
Lawrenceiow

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