在VBA中满足条件时整行不被选中

我有一些代码应该读取A列中的每个单元格,然后将整行复制并粘贴到另一个工作表上,直到单元格包含特定的文本。 请参阅下面的代码:

Public Sub CiscoPrimeReport() Dim rowCounter As Long Dim colCounter As Long rowCounter = 9 colCounter = 1 MsgBox ("Please do the following before pressing the OK BUtton on this Popup Window!!:" & vbNewLine & vbNewLine & _ "1. Open the CISCO PRIME AP Report you want to use" & vbNewLine & _ "2. Select all the data (Ctrl + A)" & vbNewLine & _ "3. Copy ALL the content (Ctrl + C)" & vbNewLine & _ "4. NOW YOU CAN PRESS THE OK BUTTON!") Application.DisplayAlerts = False Call createCiscoSheet With ThisWorkbook.Sheets("Cisco Raw") .Range("A1").PasteSpecial (xlPasteValues) Do While rowCounter < 2200 If Cells(rowCounter, colCounter).Value <> "AP Statistics Summary" Then ThisWorkbook.ActiveSheet.Range("rowCounter:colCounter").EntireRow.Copy With ThisWorkbook.Sheets("Throughput Per AP") .Range("A2").PasteSpecial (xlPasteValues) End With End If rowCounter = rowCounter + 1 Loop End With End Sub 

代码运行良好,直到我到这一行:

 ThisWorkbook.ActiveSheet.Range("rowCounter:colCounter").EntireRow.Copy 

然后我得到一个运行时错误1004:应用程序定义或对象定义的错误。

我不知道如果使用Do While循环是问题,我应该使用For循环,而不是如果我在代码中丢失的东西。

vbavariables需要在引号之外并连接:

 Range(rowCounter & ":" & colCounter) 

但我不认为这是你想要的,因为它将从行1(colCounter)的每一行复制到任何rowCounter目前是。

我想你想要:

 ThisWorkbook.ActiveSheet.Rows(rowCounter).Copy 

另外如果你只想要值,那么完全跳过剪贴板并直接赋值:

  If Cells(rowCounter, colCounter).Value <> "AP Statistics Summary" Then ThisWorkbook.Sheets("Throughput Per AP").Rows(2).Value = ThisWorkbook.ActiveSheet.Rows(rowCounter).Value End If rowCounter = rowCounter + 1