【发布时间】:2017-09-06 09:22:25
【问题描述】:
我有一个 Excel 工作表,其中的值是 A、B、C、D 和 F 列中的数字(这些列有多行)。我想要的是当点击一个按钮时,它的行为零值的所有列都将被隐藏。
所以如果有人对此有解决方案,请帮助我!非常感谢
【问题讨论】:
-
您是否已经尝试过任何代码?如果是这样,您可以将其发布在您的问题上。如果列有 0 或所有列都是 0,也是这样吗?
我有一个 Excel 工作表,其中的值是 A、B、C、D 和 F 列中的数字(这些列有多行)。我想要的是当点击一个按钮时,它的行为零值的所有列都将被隐藏。
所以如果有人对此有解决方案,请帮助我!非常感谢
【问题讨论】:
Option Explicit
Sub HideZeros()
HideCells (1)
End Sub
Sub HideCells(Col_Num As Integer)
Dim Row_num As Integer
For Row_num = 1 To 10
If Sheet1.Cells(Row_num, Col_Num).Value = 0 Then Sheet1.Cells(Row_num, Col_Num).Delete Shift:=xlUp
Next
End Sub
以上内容为您指明正确的方向,可以根据需要进行修改。它将删除工作表 1 列 A 的前 10 行中值为 0 的所有单元格,并将其余单元格向上移动。 你不能隐藏一个单元格,你只能隐藏一行/列。所以删除是你唯一的选择……希望对你有帮助
【讨论】:
我创建了两个解决方案,具体取决于您想要做什么。在代码中,我已经解释了每个步骤在做什么,其余的代码只是针对不同的列重复。 如果该列中的行等于零,则第一个隐藏该列:
Sub HideRowsWhereAnyRowIsZero()
Dim i As Long 'Declarations
For i = 1 To Rows.Count 'Find the last row and loop through until reached
If Cells(i, 1).Value = "0" Then 'if the cell equals 0 then move to next step if not move to next cell
Cells(i, 1).EntireColumn.Hidden = True 'hide column
If Cells(i, 1).EntireColumn.Hidden = True Then Exit For 'if column has been hidden then break the loop
End If 'if cell doesnt equal 0 move on
Next i 'creates loop to go thorugh each row in column
For i = 1 To Rows.Count
If Cells(i, 2).Value = "0" Then
Cells(i, 2).EntireColumn.Hidden = True
If Cells(i, 2).EntireColumn.Hidden = True Then Exit For
End If
Next i
For i = 1 To Rows.Count
If Cells(i, 3).Value = "0" Then
Cells(i, 3).EntireColumn.Hidden = True
If Cells(i, 3).EntireColumn.Hidden = True Then Exit For
End If
Next i
For i = 1 To Rows.Count
If Cells(i, 4).Value = "0" Then
Cells(i, 4).EntireColumn.Hidden = True
If Cells(i, 4).EntireColumn.Hidden = True Then Exit For
End If
Next i
For i = 1 To Rows.Count
If Cells(i, 6).Value = "0" Then
Cells(i, 6).EntireColumn.Hidden = True
If Cells(i, 6).EntireColumn.Hidden = True Then Exit For
End If
Next i
End Sub
下一段代码仅在该列中所有行都为 0 的地方隐藏
Sub HideColumnsWhenAllRowsAreZero()
Dim Lastrow As Long
Lastrow = Range("A" & Rows.Count).End(xlUp).row 'finds the last row for column A
If WorksheetFunction.Sum(Range("A1:A" & Lastrow)) = 0 Then 'sum the values of the column if it equals 0 then move on to hide column
Range("A1:A" & Lastrow).EntireColumn.Hidden = True 'Hides column
End If 'Ends statement for column
Lastrow = Range("B" & Rows.Count).End(xlUp).row
If WorksheetFunction.Sum(Range("B1:B" & Lastrow)) = 0 Then
Range("B1:B" & Lastrow).EntireColumn.Hidden = True
End If
Lastrow = Range("C" & Rows.Count).End(xlUp).row
If WorksheetFunction.Sum(Range("C1:C" & Lastrow)) = 0 Then
Range("C1:C" & Lastrow).EntireColumn.Hidden = True
End If
Lastrow = Range("D" & Rows.Count).End(xlUp).row
If WorksheetFunction.Sum(Range("D1:D" & Lastrow)) = 0 Then
Range("D1:D" & Lastrow).EntireColumn.Hidden = True
End If
Lastrow = Range("F" & Rows.Count).End(xlUp).row
If WorksheetFunction.Sum(Range("F1:F" & Lastrow)) = 0 Then
Range("F1:F" & Lastrow).EntireColumn.Hidden = True
End If
End Sub
我建议单步执行代码(使用 F8)并尝试了解实际发生的情况。
【讨论】: