如何使循环在VBA中工作?

时间:2018-08-02 03:53:15

标签: excel vba excel-vba

我已经开始学习VBA中的循环,由于某种原因,我无法使此代码正常工作。

Sub test()

Dim Sheet1 As Worksheet
Dim fr As Long
Dim c As Long

Set Sheet1 = Worksheets("Sheet1")

fr = Cells(Rows.Count, "A").End(xlUp).Row

    With Sheet1

        For c = fr To step - 1
            If .Cells(c, "A").Value = "" Then
                .Cells(c, "A").EntireRow.Clear
            End If
            Next c
    End With

End Sub

我想要做的就是在A列中,如果没有值可清除整个行。但是,此代码对我不起作用,我也不知道为什么。

任何帮助都会得到极大的帮助。

谢谢

G

2 个答案:

答案 0 :(得分:3)

首先将http://www.myapp.com/index.php/Login放在模块顶部。这将迫使您声明所有变量,并将导致指出您的问题的编译错误。在这一行...

Option Explicit

...您错过了循环的终点。你可能想要在那里...

For c = fr To step - 1

请注意,关键字For c = fr To 1 Step - 1 在代码中小写。这是因为VBA解析器正在寻找表达式Step,其中For X = Y To Z [Step #]是可选的。由于[Step #]缺失并且是必需的,因此它将Z解析为表达式,从 未声明的变量 step - 1中减去1。由于数字变量被初始化为step,因此您将超出工作表的开头。

答案 1 :(得分:2)

您的For ...不完整。Next并且单元格引用不匹配。 Sheet1很可能是工作表的代号,不应重复使用。

mycat.log
2018-08-02 09:59:41.205  INFO [$_NIOConnector] (io.mycat.backend.datasource.PhysicalDatasource.getConnection(PhysicalDatasource.java:529)) - no ilde connection in pool,create new connection for hostS1 of schema mysql
2018-08-02 09:59:53.939  INFO [Timer0] (io.mycat.backend.datasource.PhysicalDatasource.getConnection(PhysicalDatasource.java:529)) - no ilde connection in pool,create new connection for hostS1 of schema mysql
2018-08-02 10:00:01.173 ERROR [$_NIOConnector] (io.mycat.net.NIOConnector.finishConnect(NIOConnector.java:155)) - error:
java.net.ConnectException: Connection timed out
    at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) ~[?:1.7.0_111]
    at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:744) ~[?:1.7.0_111]
    at io.mycat.net.NIOConnector.finishConnect(NIOConnector.java:165) ~[Mycat-server-1.6.5-release.jar:?]
    at io.mycat.net.NIOConnector.finishConnect(NIOConnector.java:143) ~[Mycat-server-1.6.5-release.jar:?]
    at io.mycat.net.NIOConnector.run(NIOConnector.java:98) ~[Mycat-server-1.6.5-release.jar:?]
2018-08-02 10:00:01.173  INFO [$_NIOConnector] (io.mycat.net.AbstractConnection.close(AbstractConnection.java:514)) - close connection,reason:java.net.ConnectException: Connection timed out ,MySQLConnection [id=0, lastTime=1533175073937, user=root, schema=mysql, old shema=mysql, borrowed=false, fromSlaveDB=true, threadId=0, charset=utf8, txIsolation=3, autocommit=true, attachment=null, respHandler=null, host=parse1, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=false]
2018-08-02 10:00:01.173  INFO [$_NIOConnector] (io.mycat.sqlengine.SQLJob.connectionError(SQLJob.java:117)) - can't get connection for sql :select user()