Hide row if cell is blank

DPARDY

New Member
Joined
Dec 3, 2009
Messages
41
Office Version
  1. 365
Platform
  1. Windows
Merry Christmas. I have a report that I have to do every day for 50+ employees, the problem I am tiring to solve is some times it puts a blank row (row 13) between the data.

This is what I have, but is not working. Just need it to find Employee Total: in column F and move up 1 row and to the right 1 column and if blank, hide that row. Thanks in advance for your time.

Sub HideRowsBasedOnCondition()
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long

' Set the worksheet variable to the desired sheet
Set ws = ThisWorkbook.Sheets("Sheet1")

' Find the last row with data in column F
lastRow = ws.Cells(ws.Rows.Count, "F").End(xlUp).Row

' Loop through each row in column F
For i = 1 To lastRow
' Check if the cell in column F contains "Employee Total:"
If ws.Cells(i, "F").Value = "Employee Total:" Then
' Move up one cell and check if the cell to the right is blank
If ws.Cells(i - 1, "G").Value = "" Then
' If blank, hide the entire row
ws.Rows(i).EntireRow.Hidden = True
End If
End If
Next i
End Sub



1703435546777.png
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
First of all I would fix this line:
VBA Code:
from:
ws.Rows(i).EntireRow.Hidden = True
to:
ws.Rows(i - 1).EntireRow.Hidden = True '<- changed
then, if that isn't enough, there may be extra spaces in the cells that contain "Employee Total:" and in column G so, respectively, use instead:
Code:
this:
If Trim(ws.Cells(i, "F").Value) = "Employee Total:" Then '<- changed
and this:
If Trim(ws.Cells(i - 1, "G").Value) = "" Then '<- changed
 
Upvote 0
Solution
First of all I would fix this line:
VBA Code:
from:
ws.Rows(i).EntireRow.Hidden = True
to:
ws.Rows(i - 1).EntireRow.Hidden = True '<- changed
then, if that isn't enough, there may be extra spaces in the cells that contain "Employee Total:" and in column G so, respectively, use instead:
Code:
this:
If Trim(ws.Cells(i, "F").Value) = "Employee Total:" Then '<- changed
and this:
If Trim(ws.Cells(i - 1, "G").Value) = "" Then '<- changed
Extra spaces, Thank you.
 
Upvote 0
Thanks for the positive feedback(y), glad having been of some help.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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