Hello,
I’m fairly new to VBA, and this forum has already proved to be a fantastic resource. I have the following four subs, all of which are intended to hide columns B, D, F, and H if all cells but the header are empty. The subs are functional, but I would really like to be able to combine them into one sub. Everything else I’ve written towards this end either hides all or none of these columns and I’m a little stumped. Thank you!
I’m fairly new to VBA, and this forum has already proved to be a fantastic resource. I have the following four subs, all of which are intended to hide columns B, D, F, and H if all cells but the header are empty. The subs are functional, but I would really like to be able to combine them into one sub. Everything else I’ve written towards this end either hides all or none of these columns and I’m a little stumped. Thank you!
Code:
Sub B()
Application.ScreenUpdating = False
For Each Cell In Range("B1:B1000").Offset(1)
If Cell.Value <> "" Then
Exit Sub
End If
Next Cell
For Each Cell In Range("B1:B1000").Offset(1)
If Cell.Value = "" Then
Columns("B").Hidden = True
End If
Next Cell
Application.ScreenUpdating = False
End Sub
Sub D()
Application.ScreenUpdating = False
For Each Cell In Range("D1:D1000").Offset(1)
If Cell.Value <> "" Then
Exit Sub
End If
Next Cell
For Each Cell In Range("D1:D1000").Offset(1)
If Cell.Value = "" Then
Columns("D").Hidden = True
End If
Next Cell
Application.ScreenUpdating = False
End Sub
Sub F()
Application.ScreenUpdating = False
For Each Cell In Range("F1:F1000").Offset(1)
If Cell.Value <> "" Then
Output = MsgBox("Middle Name Found", vbOKOnly, "Sub D")
Exit Sub
End If
Next Cell
For Each Cell In Range("F1:F1000").Offset(1)
If Cell.Value = "" Then
Columns("F").Hidden = True
End If
Next Cell
Application.ScreenUpdating = False
End Sub
Sub H()
Application.ScreenUpdating = False
For Each Cell In Range("H1:H1000").Offset(1)
If Cell.Value <> "" Then
Exit Sub
End If
Next Cell
For Each Cell In Range("H1:H1000").Offset(1)
If Cell.Value = "" Then
Columns("H").Hidden = True
End If
Next Cell
Application.ScreenUpdating = False
End Sub