作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直收到编译错误: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/
我是一名优秀的程序员,十分优秀!