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

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

我想借助单元格值实现两个不同的目标。它适用于列,但不适用于行。

当我在消息框中单击“是”时,此代码应取消隐藏行。

Private Sub Worksheet_Change(ByVal Target As Range)
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
excel vba
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

您可以使用代码压头来获得更清晰的代码格式 例如(https://www.automateexcel.com/vba-code-indenter/#)

© www.soinside.com 2019 - 2024. All rights reserved.