【问题标题】:Selecting rows when some contain errors当某些行包含错误时选择行
【发布时间】:2015-10-02 04:08:09
【问题描述】:

我正在尝试选择 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

我该如何解决我的问题?

更新:

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

【问题讨论】:

  • 请说明您遇到的错误,并在调试窗口中检查i 值是什么,注意Integer 类型最大值为32767。
  • 你想跳过错误的行吗?

标签: vba excel


【解决方案1】:

试试这个例子,将工作表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

【讨论】:

  • 谢谢,现在它选择行没有问题。不幸的是,当我导出选定的行时,我收到此错误:“该命令不能是多选用户”。指令是“Selection.Copy”
  • @Loris,我已经编辑了代码以满足您的要求。
  • 不幸的是,我必须选择来自工作表不同部分的行,而不是连续选择行
  • 你的意图是不可行的,因为.Copy方法... cannot be user on multiple selection。考虑另一种算法。为您的问题添加更多详细信息。也许逐行复制。请注意,通常您不必在.Copy 之前添加.Select 单元格。
猜你喜欢
  • 1970-01-01
  • 2015-10-13
  • 1970-01-01
  • 2016-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-05
相关资源
最近更新 更多