[英]Testing multiple columns for zero values at once?
(I am not a programmer and have no idea what I'm doing. This is my first VBA project) (我不是程序员,也不知道我在做什么。这是我的第一个VBA项目)
So, I'm trying to hide columns with a zero value. 因此,我试图隐藏具有零值的列。 So far, thanks to excelribbon.tips.net, I've managed to get this to work with the following (abbreviated) code
到目前为止,多亏了excelribbon.tips.net,我已经设法使它与以下(缩写)代码一起使用
Sub HideColumn1()
If Range(“H5”).Value = 0 Then
Columns(“H”).EntireColumn.Hidden = True
Else
Columns(“H”).EntireColumn.Hidden = False
End If
‘Repeat for everything between H and AG
If Range(“AG5”).Value = 0 Then
Columns(“AG”).EntireColumn.Hidden = True
Else
Columns(“AG”).EntireColumn.Hidden = False
End If
End Sub
There has to be a way to do this that doesn't include 180 lines of repetitive code? 必须有一种不包含180行重复代码的方法来执行此操作? Plus when it runs it takes like ten seconds to bump through all the columns individually, and the charts go through some seizure-inducing flashing, and I'd just really rather it did it all at once.
另外,运行时,大概需要十秒钟才能分别遍历所有列,并且图表会出现诱发癫痫的闪烁,我真的很希望它一次完成所有操作。 I tried changing it to
我尝试将其更改为
If Range(“H5:AG5”).Value = 0 Then
Columns(“H:AG”).EntireColumn.Hidden = True
Else
Columns(“H:AG”).EntireColumn.Hidden = False
End If
but that didn't work. 但这没用。 I also tried changing it to Range(“H5”,.....”AG5”) and Columns(“H5”,....”AG5”) for the sake of being thorough, even though I really didn't expect it to work.
为了更全面,我也尝试将其更改为Range(“ H5”,.....“ AG5”)和Columns(“ H5”,....“ AG5”),尽管我确实没有希望它能工作。
Can anyone help me please? 谁能帮我吗?
Thanks so much everyone!!! 非常感谢大家!!!
Great first attempt Sylphie! 伟大的第一次尝试西尔菲! You can use a loop to go through each column and check row 5 of that column.
您可以使用循环遍历每一列并检查该列的第5行。 Also, to get rid of the flashing you can turn off
ScreenUpdating
, then turn it back on after the code is finished: 另外,要摆脱闪烁,您可以关闭
ScreenUpdating
,然后在代码完成后将其重新打开:
Sub HideColumn1()
Dim i As Long
Application.ScreenUpdating = False
For i = 8 To 33 'H through AG
If Cells(5, i).Value = 0 Then
Columns(i).EntireColumn.Hidden = True
Else
Columns(i).EntireColumn.Hidden = False
End If
Next i
Application.ScreenUpdating = True
End Sub
This unhides all the columns, Loops and hides all the correct columns at once. 这将取消隐藏所有列,并立即循环并隐藏所有正确的列。 Should be quick and clean.
应该快速干净。
Sub HideColumn1()
ActiveSheet.Columns("H:AG").Hidden = False
For j = 8 To 33
If ActiveSheet.Cells(5, j).Value = 0 Then
Dim rng As Range
If rng Is Nothing Then
Set rng = ActiveSheet.Cells(5, j)
Else
Set rng = Union(ActiveSheet.Cells(5, j), rng)
End If
End If
Next j
rng.EntireColumn.Hidden = True
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.