在VBA中满足条件时未选择整行

时间:2017-03-20 20:07:49

标签: excel vba excel-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循环,或者如果我在代码中遗漏了某些内容。

1 个答案:

答案 0 :(得分:4)

vba变量需要在引号之外并连接:

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