当某些包含错误时选择行

时间:2015-09-26 17:08:28

标签: excel vba excel-vba

我试图选择Excel工作表的行,但某些行包含错误。这是选择代码:

Dim i As Integer
i = 2

Dim dRange As String
dRange = "1:1"

While Not IsEmpty(Cells(i, 1))

    dRange = dRange & "," & i & ":" & i

    i = i + 1

Wend

Range(dRange).EntireRow.Select

这是一个有问题的行(CSV导出):

08/03/2012;20120305;05/03/2012;20120305;Silba M.; -   ; 647,50 ; -   ;
-   ; 10.330,77 ;;Incasso affitto : URS

如何解决我的问题?

更新:

对于工作表中的每一行,我必须在列中搜索值。如果我发现该值,我必须将该行导出到另一张表。我该怎么办?

1 个答案:

答案 0 :(得分:0)

请尝试使用此示例将工作表Source中的行复制到工作表Destination,这些行在已定义的列中具有已定义的值。

Sub RowsCopy()

    Dim objSrcSht As Worksheet
    Dim objDstSht As Worksheet
    Dim strSearchColumn As String
    Dim strSearchValue As String
    Dim lngDstRow As Long
    Dim lngSrcRow As Long

    Set objSrcSht = Sheets("Source") ' the sheet where to search
    strSearchColumn = "A" ' the column where to search
    strSearchValue = "sample" ' the value to be searched
    Set objDstSht = Sheets("Destination") ' the sheet where to copy the rows

    With objDstSht
        lngDstRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

    lngSrcRow = 2
    With objSrcSht
        Do Until IsEmpty(.Cells(lngSrcRow, 1))
            If .Cells(lngSrcRow, strSearchColumn) = strSearchValue Then
                objDstSht.Rows(lngDstRow).Value = .Rows(lngSrcRow).Value
                lngDstRow = lngDstRow + 1
            End If
            lngSrcRow = lngSrcRow + 1
        Loop
    End With

End Sub