Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 我能';我不知道它现在什么时候退出for循环,我得到运行时错误28_Vba_Runtime - Fatal编程技术网

Vba 我能';我不知道它现在什么时候退出for循环,我得到运行时错误28

Vba 我能';我不知道它现在什么时候退出for循环,我得到运行时错误28,vba,runtime,Vba,Runtime,我有下面的函数来查找电子表格中包含数据的最后一列,我得到一个运行时错误28。我相信它没有正确退出for循环。我错过了什么愚蠢的事吗?我制作了多个简单的函数,就像这样,没有任何问题 Function max_column() Dim i As Integer Dim max_col As Integer For i = 1 To 200 If Worksheets("Sheet1").Cells(2, i) = "" Then max_col = i: Exit For Next i max_

我有下面的函数来查找电子表格中包含数据的最后一列,我得到一个运行时错误28。我相信它没有正确退出for循环。我错过了什么愚蠢的事吗?我制作了多个简单的函数,就像这样,没有任何问题

Function max_column()

Dim i As Integer
Dim max_col As Integer

For i = 1 To 200
If Worksheets("Sheet1").Cells(2, i) = "" Then max_col = i: Exit For
Next i
max_column() = max_col
Exit Function
End Function

我会稍微修改你的
If
语句

Function max_column()

Dim i As Integer
Dim max_col As Integer

For i = 1 To 200
    If Worksheets("Sheet1").Cells(2, i) = "" Then 
    max_col = i
    Exit For
    Else
    End If
Next i

max_column() = max_col

End Function

希望这有帮助

我会稍微修改你的
If
语句

Function max_column()

Dim i As Integer
Dim max_col As Integer

For i = 1 To 200
    If Worksheets("Sheet1").Cells(2, i) = "" Then 
    max_col = i
    Exit For
    Else
    End If
Next i

max_column() = max_col

End Function

希望这有帮助

不需要循环,只需使用END()

这将返回第2行中的第一个空白单元格:

Function max_column() as Long
    If Worksheets("Sheet1").Cells(2, 1) = "" Then
        max_column = 1
    Else
        max_column = Worksheets("Sheet1").Cells(2, 1).End(xlToRight).Column + 1
    End If
End Function
如果您想要的是行中最后一个使用的单元格右边的列,请使用:

Function max_column() as Long
    max_column = Worksheets("Sheet1").Cells(2, Worksheets("Sheet1").Columns.Count).End(xlToLeft).Column + 1
End Function

不需要循环,只需使用END()

这将返回第2行中的第一个空白单元格:

Function max_column() as Long
    If Worksheets("Sheet1").Cells(2, 1) = "" Then
        max_column = 1
    Else
        max_column = Worksheets("Sheet1").Cells(2, 1).End(xlToRight).Column + 1
    End If
End Function
如果您想要的是行中最后一个使用的单元格右边的列,请使用:

Function max_column() as Long
    max_column = Worksheets("Sheet1").Cells(2, Worksheets("Sheet1").Columns.Count).End(xlToLeft).Column + 1
End Function

这里发生了一些事情。首先,应该避免使用
整数
,而使用
长数
。为
整数
分配一个大于32767的值。如果您尝试给它一个32768的值,您将得到一个运行时
溢出
错误(错误号8)

修复第一个位将如下所示:

Function max_column()

    Dim i As Long
    Dim max_col As Long

    For i = 1 To 200
    If Worksheets("Sheet1").Cells(2, i) = "" Then max_col = i: Exit For
    Next i
    max_column() = max_col
    Exit Function
End Function
当然,这并不能解决问题,它只是消除了一个容易导致问题的常见错误。这里有两件事情可能更险恶。首先,您使用的是不合格的
工作表
引用,这意味着您依赖的是
活动工作簿
,无论这是否是预期目标

第二个问题是
字符。这表示换行,但实际上不是换行!多方便啊……除了你的逻辑没有解决问题

For i = 1 To 200
    If Worksheets("Sheet1").Cells(2, i) = "" Then max_col = i: Exit For
Next i
事实上:

For i = 1 To 200
    If Worksheets("Sheet1").Cells(2, i) = "" Then 
        max_col = i
    End If

    Exit For
Next i
由于第二行中的第一个单元格为空,或者循环退出,因此该循环只会返回1或0

最后,函数return调用再次被调用,这就是造成堆栈溢出错误的原因(因为它一直在调用、调用、调用……)

将其修正为
max\u column
,它实际上应该是
getthefirst columnsethetivesheethasnallstringvalueinthesecondrow
(请注意,实际函数与简单的
max\u column
不同)

通过这些更改,您的代码将变为:

Function max_column()
    Dim i As Long
    Dim max_col As Long

    For i = 1 To 200
        If Worksheets("Sheet1").Cells(2, i) = "" Then 
            max_col = i
            Exit For
        End If
    Next i

    max_column = max_col

    Exit Function
End Function
并进行最终调整以避免其他错误:

Public Function GetMaxColumn() as Long
    Dim i As Long
    For i = 1 To 200
        If ActiveWorkbook.Worksheets("Sheet1").Cells(2, i) = vbNullString Then 
            GetMaxColumn = i
            Exit Function
        End If
    Next i
End Function

瞧!功能完善。

这里发生了一些事情。首先,应该避免使用
整数
,而使用
长数
。为
整数
分配一个大于32767的值。如果您尝试给它一个32768的值,您将得到一个运行时
溢出
错误(错误号8)

修复第一个位将如下所示:

Function max_column()

    Dim i As Long
    Dim max_col As Long

    For i = 1 To 200
    If Worksheets("Sheet1").Cells(2, i) = "" Then max_col = i: Exit For
    Next i
    max_column() = max_col
    Exit Function
End Function
当然,这并不能解决问题,它只是消除了一个容易导致问题的常见错误。这里有两件事情可能更险恶。首先,您使用的是不合格的
工作表
引用,这意味着您依赖的是
活动工作簿
,无论这是否是预期目标

第二个问题是
字符。这表示换行,但实际上不是换行!多方便啊……除了你的逻辑没有解决问题

For i = 1 To 200
    If Worksheets("Sheet1").Cells(2, i) = "" Then max_col = i: Exit For
Next i
事实上:

For i = 1 To 200
    If Worksheets("Sheet1").Cells(2, i) = "" Then 
        max_col = i
    End If

    Exit For
Next i
由于第二行中的第一个单元格为空,或者循环退出,因此该循环只会返回1或0

最后,函数return调用再次被调用,这就是造成堆栈溢出错误的原因(因为它一直在调用、调用、调用……)

将其修正为
max\u column
,它实际上应该是
getthefirst columnsethetivesheethasnallstringvalueinthesecondrow
(请注意,实际函数与简单的
max\u column
不同)

通过这些更改,您的代码将变为:

Function max_column()
    Dim i As Long
    Dim max_col As Long

    For i = 1 To 200
        If Worksheets("Sheet1").Cells(2, i) = "" Then 
            max_col = i
            Exit For
        End If
    Next i

    max_column = max_col

    Exit Function
End Function
并进行最终调整以避免其他错误:

Public Function GetMaxColumn() as Long
    Dim i As Long
    For i = 1 To 200
        If ActiveWorkbook.Worksheets("Sheet1").Cells(2, i) = vbNullString Then 
            GetMaxColumn = i
            Exit Function
        End If
    Next i
End Function

瞧!一个功能完善的函数。

如果
单元格(2,1)
的值是
vbNullString
则End命令的工作方式与OP的函数不同。@BrandonBarney true,已修复。请参见编辑。@BrandonBarney,仍然不需要循环。同意,不需要循环,但可以使用类似
ActiveWorkbook.Worksheets(“Sheet1”).Cells(2,ActiveWorkbook.Worksheets(“Sheet1”).UsedRange.Column+1.End(xlToLeft)。Column+1
的方法来简化。跳过丑陋的if块。不过,我还是建议您稍微解释一下这种方法。我想OP根本不知道这是怎么回事。而且,平心而论,如果单元格之间有空格,这种方法在功能上并不等同于OP。@BrandonBarney这不是我读OP想要什么的方式。他们正在寻找第一个空白。这可能与行中最后使用的单元格重合,也可能不重合,或者我会使用:
Worksheets(“Sheet1”).Cells(2,Worksheets(“Sheet1”).Columns.Count).End(xlToLeft).Column+1
如果
Cells(2,1)
的值为
vbNullString
则End命令的工作方式与OP的函数不同。@BrandonBarney true,固定的。请参见编辑。@BrandonBarney,仍然不需要循环。同意,不需要循环,但可以使用类似
ActiveWorkbook.Worksheets(“Sheet1”).Cells(2,ActiveWorkbook.Worksheets(“Sheet1”).UsedRange.Column+1.End(xlToLeft)。Column+1
的方法来简化。跳过丑陋的if块。不过,我还是建议您稍微解释一下这种方法。我想OP根本不知道这是怎么回事。而且,平心而论,如果单元格之间有空格,这种方法在功能上并不等同于OP。@BrandonBarney这不是我读OP想要什么的方式。他们正在寻找第一个空白。这可能与最后使用的单元格一致,也可能不一致