vba - 如何解决VBA中的 'Next without For'错误

标签 vba excel

我收到一个 next without for 错误:

Sub CTLines()
Dim iVal As Integer
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim rng As Range
    
Set ws1 = Worksheets("INCIDENTS")
Set ws2 = Worksheets("INCDB")
    
iVal = Application.WorksheetFunction.CountIf(Range("AO5:AO999"), "Yes")
    
Dim i
    
For i = 1 To iVal
    With Sheets("INCDB")
        .Range("5:5").Insert Shift:=x1Down
Next i
End Sub

我尝试过更改变量、缩进等很多东西,但没有成功。

我想做的就是计算 AO 列中包含 Yes 的行数,并在 INCDB 电子表格中添加尽可能多的行。

最佳答案

将代码更改为底部附近的代码:

For i = 1 To iVal
    With Sheets("INCDB")
        .Range("5:5").Insert Shift:=xlDown
    End With
Next i

当 VBA 编译器遇到包含一行或多行缺少匹配终止行的代码时,它不擅长报告错误。

就您而言,您从未终止过 With 语句。

关于vba - 如何解决VBA中的 'Next without For'错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33286860/

相关文章:

vba - 如何获取 IPM.DistList 中的收件人地址?

excel - 日期智能列可在 PowerPivot 中保持过去 3 个月的运行总计

excel - 为什么 Excel VBA 会覆盖这些对象值

excel - VBA获取组合框到 'suggest'的一个选项

json - 使用 iTunes 搜索 API 的 Excel VBA 宏 - 查询和解析 JSON 结果的最快方法

Excel VBA 导出到 Excel - 删除连接

vba - 无法使用excel vba在foreach循环中获取单元格值

c++ - 在 Excel XLOPER 中通过 char* 返回日语字符

vba - 如何在 Excel 中将 DocumentProperty 添加到 CustomDocumentProperties?

vba - 为什么 Debug.print 区分 bool 值和整数?