Hide/Unhide Blank Row Based on Cell Value and MsgBox Question

36 views Asked by At

I want to achieve two different goals with the help of a cell value, it is working well on columns but not on rows. When i click Yes on message box so this code not unhide rows.

    Dim HowMany As Long
    Dim rng As Range
    Dim i As Integer
    Dim message As String
    Dim Ans As VbMsgBoxResult
    Ans = MsgBox("New Joining", vbQuestion + vbYesNo + vbDefaultButton2, "ATTENDANCE SHEET")
             
If Target.CountLarge > 1 Then Exit Sub   
If Intersect(Target, Range("B5:B6")) Is Nothing Then Exit Sub  
   Range("D11:AH11").EntireColumn.Hidden = False   ' initially display 31 columns
   HowMany = CInt(Range("B9").Text)    ' days in this particular month
If HowMany = 31 Then Exit Sub

Set rng = Cells(11, HowMany + 3).Offset(, 1).Resize(, 31 - HowMany)     'end day will be in column HowMany plus 3
rng.EntireColumn.Hidden = True
Range("D13:AH32").ClearContents
                
If Target.Address = Range("B6").Address Then
For i = 13 To 32
        If Cells(i, 1).Value = "" Then
           Cells(i, 1).EntireRow.Hidden = True
        Else
           Cells(i, 1).EntireRow.Hidden = False
        End If
        Next i

End If
        If Ans = vbYes Then
        Cells(i, 1).EntireRow.Hidden = False
        Else
        If Ans = vbNo Then
        Exit Sub
        End If
End If ```

Something's wrong with me here, but I don't know. 
1

There are 1 answers

3
Black cat On BEST ANSWER

That part of the code is out of the For loop and the If loop too.

Pls. try this

If Target.Address = Range("B6").Address Then
    For i = 13 To 32
        If Cells(i, 1).Value = "" Then
            Cells(i, 1).EntireRow.Hidden = TRUE
        Else
            Cells(i, 1).EntireRow.Hidden = FALSE
        End If
        
        If Ans = vbYes Then
            Cells(i, 1).EntireRow.Hidden = FALSE
        Else
            If Ans = vbNo Then
                Exit Sub
            End If
        End If
    Next i
End If

You can use a Code Indenter to get a more clear format of the code e.g. (https://www.automateexcel.com/vba-code-indenter/#)