vba - 查找行值,复制行及其下面的所有范围以减少数据

标签 vba excel find copy

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

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

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

最佳答案

如果您想将包含“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

关于vba - 查找行值,复制行及其下面的所有范围以减少数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17299449/

相关文章:

java - 如何在 Java 中对 XML 文档进行 UTF-8 编码

java - 将 Excel 文件导入 MySQL 数据库时无法在字符串中添加撇号

python - 棘手的字符串匹配

基于文件名的 Linux 文件系统正则表达式搜索

linux - 从搜索整个系统中排除目录

json - 将 JSON 文件导入 MS Access 表

javascript - 从 Node.js 调用 VBScript

c# - 使用 .net 将 vba 宏代码注入(inject) excel

regex - Excel VBA : search a string to find the first non-text character

vba - 在工作簿关闭时禁用 Excel VBA 中的剪贴板提示