一次测试多个列的零值?

时间:2018-04-23 16:37:15

标签: vba excel-vba excel

(我不是程序员,也不知道我在做什么。这是我的第一个VBA项目)

所以,我试图隐藏零值的列。到目前为止,感谢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 

必须有一种方法可以做到这一点,不包括180行重复代码?另外,当它运行时,它需要花费十秒钟来单独撞击所有列,并且图表会经历一些引发癫痫发作的闪烁,而我真的宁愿它立刻完成所有这些操作。我尝试将其更改为

If Range(“H5:AG5”).Value = 0 Then 

  Columns(“H:AG”).EntireColumn.Hidden = True 

Else 

  Columns(“H:AG”).EntireColumn.Hidden = False 

End If 

但这不起作用。为了彻底,我也尝试将它改为Range(“H5”,.....“AG5”)和列(“H5”,......“AG5”),即使我真的没有期待它的运作。

有人能帮帮我吗?

非常感谢大家!!!

2 个答案:

答案 0 :(得分:2)

伟大的第一次尝试Sylphie!您可以使用循环遍历每列并检查该列的第5行。此外,要摆脱闪烁,您可以关闭<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.earnix.eo</groupId> <artifactId>EOParent</artifactId> <version>8.8.4.0-SNAPSHOT</version> <relativePath>../EOParent/pom.xml</relativePath> </parent> <artifactId>PricingBatch</artifactId> <name>PricingBatch</name> <packaging>jar</packaging> <dependencies> <dependency> <groupId>com.earnix.eo</groupId> <artifactId>Tools</artifactId> <version>${project.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.earnix.eo</groupId> <artifactId>EarnixShared</artifactId> <version>${project.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.earnix.eo</groupId> <artifactId>EOGUI</artifactId> <version>${project.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.earnix.eo</groupId> <artifactId>EOSessions</artifactId> <version>${project.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.earnix.eo</groupId> <artifactId>EOUtils</artifactId> <version>${project.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.sample</groupId> <artifactId>sample1</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>C:\ibm\WebSphere\AppServer\lib\bootstrap.jar</systemPath> </dependency> <dependency> <groupId>com.sample</groupId> <artifactId>sample2</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>C:\ibm\WebSphere\AppServer\runtimes\com.ibm.ws.webservices.thinclient_8.5.0.jar</systemPath> </dependency> <dependency> <groupId>com.sample</groupId> <artifactId>sample3</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>C:\ibm\WebSphere\AppServer\java\jre\lib\xml.jar</systemPath> </dependency> <dependency> <groupId>com.sample</groupId> <artifactId>sample4</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>C:\ibm\WebSphere\AppServer\java\jre\lib\ibmorb.jar</systemPath> </dependency> </dependencies> <build> <finalName>PricingBatch</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.1.0</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>com.earnix.tools.batchpricing.Main</mainClass> </manifest> <manifestEntries> <Class-Path>EOSessions.jar EOUtils.jar EOSessions.jar EOGUI.jar EarnixShared.jar bootstrap.jar ibmorb.jar xml.jar com.ibm.ws.webservices.thinclient_8.5.0.jar</Class-Path> </manifestEntries> </archive> </configuration> </plugin> </plugins> </build> ,然后在代码完成后重新打开它:

ScreenUpdating

答案 1 :(得分:1)

这会取消隐藏所有列,循环并立即隐藏所有正确的列。应该快速而干净。

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