查找行值,复制行和下面的所有范围以减少数据

时间:2013-06-25 14:04:17

标签: excel vba excel-vba find copy

我正在尝试使用宏来清理数据文件,并且只在Sheet2上复制最相关的内容。

我编写了代码来查找要从中复制数据的行。但是我只能复制行本身,而不能复制下面的范围。请注意我需要从该行到最后一列和最后一行的范围,因为matriz的大小总是不同。

s            N      s           N       s           N      s            N       s       rpm
Linear      Real    Linear      Real    Linear      Real   Linear       Real    Linear  Amplitude
 0.0000030  9853.66 0.0000030   5951.83 0.0000030   533.48  0.0000030   476.15  0.0000030   2150.16
 0.0000226  9848.63 0.0000226   5948.19 0.0000226   557.02  0.0000226   488.60  0.0000226   2150.16
 0.0000421  9826.05 0.0000421   5956.22 0.0000421   615.94  0.0000421   480.75  0.0000421   2150.15
 0.0000616  9829.72 0.0000616   5989.72 0.0000616   642.59  0.0000616   476.77  0.0000616   2150.15

所以基本上下面的代码找到第一行并将其复制到Sheet2中。我需要宏来选择下面的范围并将其复制到Sheet2上。请问你能帮我完成剧本吗?

Sub SearchForRawData()

Dim LSearchRow As Integer
Dim LCopyToRow As Integer

On Error GoTo Err_Execute

'Start search in row 1
LSearchRow = 1

'Start copying data to row 2 in Sheet2 (row counter variable)
LCopyToRow = 2

While Len(Range("A" & CStr(LSearchRow)).Value) >= 0

  'If value in column A = "s", copy entire row to Sheet2
  If Range("A" & CStr(LSearchRow)).Value = "s" Then

     'Select row and range in Sheet1 to copy
     Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
     Selection.Copy

     'Paste row into Sheet2 in next row
     Sheets("Sheet2").Select
     Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
     ActiveSheet.Paste

     'Select all Raw Data underneath found Row to Copy


     'Paste all Raw Data into Sheet 2


     'Move counter to next row
     LCopyToRow = LCopyToRow + 1

     'Go back to Sheet1 to continue searching
     Sheets("Sheet1").Select

   End If

   LSearchRow = LSearchRow + 1

Wend

'Position on cell A1
Application.CutCopyMode = False
Range("A1").Select

MsgBox "All matching data has been copied."

Exit Sub

Err_Execute:
  MsgBox "An error has occured"

End Sub

2 个答案:

答案 0 :(得分:2)

如果要将具有“s”的行及其下面的所有内容复制到目标工作表,则不需要循环。以下子找到A列中带有“s”的行,然后将该行及其下面的所有内容复制到目标工作表。

请注意,您应始终避免在VBA代码中选择或激活任何内容,并且复制和粘贴的常规方法依赖于选择。如果您使用我在此处包含的语法,则不使用剪贴板,也不需要选择目标工作表。

Sub CopyRowAndBelowToTarget()
    Dim wb As Workbook
    Dim src As Worksheet
    Dim tgt As Worksheet
    Dim match As Range

    Set wb = ThisWorkbook
    Set src = wb.Sheets("Sheet1")
    Set tgt = wb.Sheets("Sheet2")

    Dim lastCopyRow As Long
    Dim lastPasteRow As Long
    Dim lastCol As Long
    Dim matchRow As Long
    Dim findMe As String

    ' specify what we're searching for
    findMe = "s"

    ' find our search string in column A (1)
    Set match = src.Columns(1).Find(What:=findMe, After:=src.Cells(1, 1), _
        LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)

    ' figure out what row our search string is on
    matchRow = match.Row

    ' get the last row and column with data so we know how much to copy
    lastCopyRow = src.Range("A" & src.Rows.Count).End(xlUp).Row
    lastCol = src.Cells(1, src.Columns.Count).End(xlToLeft).Column

    ' find out where on our target sheet we should paste the results
    lastPasteRow = tgt.Range("A" & src.Rows.Count).End(xlUp).Row

    ' use copy/paste syntax that doesn't use the clipboard 
    ' and doesn't select or activate
    src.Range(Cells(matchRow, 1), Cells(lastCopyRow, lastCol)).Copy _
        tgt.Range("A" & lastPasteRow)

End Sub

答案 1 :(得分:0)

Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select

首先,你不需要CStr,vba会自动将数字转换为字符串,即Range(LSearchRow & ":" & LSearchRow)应该正常工作。

要查找要使用end对象的range函数的行数:

bottomRow = Range("A" & LSearchRow).End(xldown).Row

对列

执行相同操作
lastCol = Range("A" & LSearchRow).End(xlleft).column

现在复制:

Range("A" & LSearchRow & ":" & lastCol & bottomRow).Copy

但是,如果数据中间有空单元格,而不是使用End(xldown),请从工作表底部开始查找:

bottomRow = Range("A1000000").End(xlup).Row