Greetings all.
I've been to frustration land and back with this issue and I just can't figure it out. It took me a while to get where the VBA code is now but now, I'm totally stuck.
The code is a Worksheet_Change event. Cell D3 (Target) controls visibility of rows 4 through 12 (inclusive). There is a MsgBox that supposed to display if D3>10 but it doesn't if D3 is 11 but will display if D3>=12. Add to that, if D3=1 or IsEmpty, Row 3 is the only Row supposed to be visible but Row 4 is still visible.
This is driving me nuts and it's something so simple I'm overlooking it.
This image shows how I set up a test worksheet:
Thanks for your generous attention, I really appreciate it.
Derick
Here's the event code.
I've been to frustration land and back with this issue and I just can't figure it out. It took me a while to get where the VBA code is now but now, I'm totally stuck.
The code is a Worksheet_Change event. Cell D3 (Target) controls visibility of rows 4 through 12 (inclusive). There is a MsgBox that supposed to display if D3>10 but it doesn't if D3 is 11 but will display if D3>=12. Add to that, if D3=1 or IsEmpty, Row 3 is the only Row supposed to be visible but Row 4 is still visible.
This is driving me nuts and it's something so simple I'm overlooking it.
This image shows how I set up a test worksheet:
Thanks for your generous attention, I really appreciate it.
Derick
Here's the event code.
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
'Hide/Show rows 4 through 12
If Target.Address = "$D$3" Then
Application.EnableEvents = False
Dim visibleRowCount As Long
visibleRowCount = Range("D3").Value - 1 'If not -1, it will show an additional row ??????? I don't know why. I just tried -1 and it worked.
' Hide rows 4 to 12 to start.
Rows("4:12").EntireRow.Hidden = True
If visibleRowCount > 10 Then
MsgBox "No more than 10 rows will be shown. Please enter a value from 1 to 10.", vbExclamation, "Incorrect Value"
Range("D3").Value = 10
Rows("4:12").EntireRow.Hidden = False
ElseIf IsEmpty(visibleRowCount) Then
Range("D3").Value = 1
Rows("4:12").EntireRow.Hidden = True
Else
Rows("4:" & visibleRowCount + 3).EntireRow.Hidden = False '+3 because working with rows 4:12
End If
Application.EnableEvents = True
End If
End Sub
Last edited: