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
Excel 有编译错误的代码:没有do的循环。但是代码中有什么错误吗?_Excel_Vba_Loops_Compiler Errors - Fatal编程技术网

Excel 有编译错误的代码:没有do的循环。但是代码中有什么错误吗?

Excel 有编译错误的代码:没有do的循环。但是代码中有什么错误吗?,excel,vba,loops,compiler-errors,Excel,Vba,Loops,Compiler Errors,我一直收到一个编译错误:Loop without Do,即使我在代码中编写了Do。我错过了什么 Sub SearchForString() Dim StartCell As Integer Dim EndCell As Integer StartCell = 2 EndCell = 545 Do While StartCell < EndCell Sheets("tian_corp_donations_Aug2019_how").Ac

我一直收到一个编译错误:
Loop without Do
,即使我在代码中编写了
Do
。我错过了什么

Sub SearchForString()

    Dim StartCell As Integer
    Dim EndCell As Integer

    StartCell = 2
    EndCell = 545

    Do While StartCell < EndCell

    Sheets("tian_corp_donations_Aug2019_how").Activate
    Sheets("tian_corp_donations_Aug2019_how").Select

    With ThisWorkbook.Worksheets("tian_corp_donations_Aug2019_how")

        'If value in column E = "Company_Name", copy entire row to Sheet2
        If Range("B:StartCell").Value = "George Weston Limited" Then
        'Select row in Sheet1 to copy
            Rows(StartCell).Select
            Selection.Copy
        'Paste row into Sheet2 in next row
            Sheets("Sheet2").Select
            Rows(StartCell).Select
            ActiveSheet.Paste
        'Move counter to next row
        'LCopyToRow = LCopyToRow + 1
            Exit Do                
        End If

        'Go back to Sheet1 to continue searching
        Sheets("tian_corp_donations_Aug2019_how").Select

        StartCell = StartCell + 1       

    Loop                

End Sub
子SearchForString()
Dim StartCell作为整数
作为整数的Dim EndCell
StartCell=2
端细胞=545
当开始单元格<结束单元格时执行此操作
表格(“天佑公司捐赠方式”)。激活
表格(“天佑公司捐赠方式”)。选择
与此工作簿一起使用。工作表(“天佑公司”“捐赠”“如何”)
“如果E列中的值为=“公司名称”,则将整行复制到第2页
如果范围(“B:StartCell”).Value=“George Weston Limited”,则
'选择要复制的图纸1中的行
行(StartCell)。选择
选择,复制
'将行粘贴到下一行的Sheet2中
图纸(“图纸2”)。选择
行(StartCell)。选择
活动表。粘贴
'将计数器移到下一行
'LCopyToRow=LCopyToRow+1
退出Do
如果结束
'返回到Sheet1继续搜索
表格(“天佑公司捐赠方式”)。选择
StartCell=StartCell+1
环
端接头

如果我了解您想要的输出是什么,我不确定
执行循环是否是最佳选择。也许每个
对应一个
,效果会更好。试试这个:

Sub SearchForString()

    Dim StartCell As Integer
    Dim EndCell As Integer

    StartCell = 2
    EndCell = 545

    Sheets("tian_corp_donations_Aug2019_how").Activate
    Sheets("tian_corp_donations_Aug2019_how").Select

    Dim Rng As Range
    Set Rng = Range("E2:E545")

    Dim cell As Range
        For Each cell In Rng
        'If value in column E = "Company_Name", copy entire row to Sheet2
            If Cells(StartCell, "E").Text = "George Weston Limited" Then
            'Select row in Sheet1 to copy
                Rows(StartCell).Select
                Selection.Copy
            'Paste row into Sheet2 in next row
                Sheets("Sheet2").Select
                Rows(StartCell).Select
                ActiveSheet.Paste
            'Move counter to next row
            'LCopyToRow = LCopyToRow + 1
            Else
            End If        

        'Go back to Sheet1 to continue searching
        Sheets("tian_corp_donations_Aug2019_how").Select

        StartCell = StartCell + 1

        Next cell   

End Sub

您缺少一个以
结尾的
。但实际上,您从未使用
引用过
。而且
“B:StartCell”
也有问题。变量应该在引号外。所有这些都可以通过过滤然后复制可见单元格而不是循环来简化。@MilesFett否则你会如何打破循环?你可能会从阅读中受益。•此外,行计数变量必须是
长的
。Excel的行数超过了
Integer
所能处理的行数。