gpt4 book ai didi

excel - 编译错误: loop without do. But Do is in the code what is wrong?的代码

转载 作者:行者123 更新时间:2023-12-02 10:57:34 25 4
gpt4 key购买 nike

我一直收到编译错误: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

最佳答案

如果我了解您想要的输出是什么,则不确定Do Loop是最佳选择。也许For Each会更好。尝试这个:

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

关于excel - 编译错误: loop without do. But Do is in the code what is wrong?的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57913971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com