根据单元格值和MsgBox问题隐藏/取消隐藏空白行

问题描述 投票:0回答:1

我想借助单元格值实现两个不同的目标,它在列上运行良好,但在行上则不然。当我在消息框中单击“是”时,此代码不会取消隐藏行。

    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. 
excel vba worksheet-function
1个回答
0
投票

这部分代码不在 For 循环和 If 循环之外。

请。试试这个

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
© www.soinside.com 2019 - 2024. All rights reserved.