简体   繁体   English

如何使用 VBA 将遵循特定字符串的动态数据范围从一张纸复制到另一张纸?

[英]How do I copy a dynamic range of data that follows a specific string from one sheet to another using VBA?

I am trying to search Sheet1 column a for the string " Testing Test" (yes with the spaces beforehand) then copy all rows below the row containing this string until a blank row is found, then I want to paste this selected range into column A row 1 on Sheet2.我正在尝试在 Sheet1 列 a 中搜索字符串“测试测试”(是的,事先使用空格),然后复制包含该字符串的行下方的所有行,直到找到一个空白行,然后我想将此选定范围粘贴到 A 列中Sheet2 上的第 1 行。 Next I want to search for the string " CASH" (again yes with the spaces beforehand) and i want to copy just the row that includes that to be pasted 2 rows underneath the last row of the first range pasted.接下来,我想搜索字符串“CASH”(再次是,预先使用空格)并且我只想复制包含要粘贴的行的行,该行粘贴到粘贴的第一个范围的最后一行下方的 2 行。

Here is what I have so far, which does not work... I do not even address the second component of finding the second string because i can't get the first... please assist, not sure why this is not working:这是我到目前为止所拥有的,它不起作用......我什至没有解决找到第二个字符串的第二个组成部分,因为我无法得到第一个......请协助,不知道为什么这不起作用:

Sub Test()

Dim StringToFind As String

Dim i As Range

Dim cell As Range

StringToFind = "   Testing Test"

With Worksheets("Sheet1")

Set cell = .Rows(1).Find(What:=StringToFind, lookat:=xlWhole, _
                            MatchCase:=False, searchformat:=False)
                            
If Not cell Is Nothing Then

For Each i In .Range(cell.Offset(1), .Cells(.Rows.Count, cell.Column).End(xlUp))

If IsNumeric(i.Value) Then

If i.Value > 0 Then

i.EntireRow.Copy
Sheets("sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial

End If

End If

Next i


Else


End If

End With

                            

End Sub

Your question lacks a little detail.你的问题缺乏一点细节。 However, the code below will point you in the right direction.但是,下面的代码将为您指明正确的方向。 If you need help to manage it, please ask.如果您需要帮助来管理它,请询问。

Sub FindAndCopy()
    ' 221
    
    Dim WsS         As Worksheet            ' Source
    Dim WsT         As Worksheet            ' Target
    Dim Caps()      As String               ' captions to find
    Dim Fnd         As Range                ' found caption
    Dim Tgt         As Range                ' Target
    Dim Arr         As Variant              ' Value of Fnd
    Dim f           As Integer              ' loop counter: Caps
    
    With ThisWorkbook
        Set WsS = .Worksheets("Sheet1")     ' change to suit
        Set WsT = .Worksheets("Sheet2")     ' change to suit
    End With
    Caps = Split("Testing Test,CASH", ",")  ' extend to suit
    For f = 0 To UBound(Caps)
        Set Fnd = WsS.Rows(1).Find(Caps(f), LookIn:=xlValues, LookAt:=xlPart, _
                                   MatchCase:=False, SearchFormat:=False)
        If Fnd Is Nothing Then Exit For
        
        Set Fnd = Fnd.Offset(1)
        If f = 0 Then Set Fnd = Fnd.Resize(Fnd.End(xlDown).Row - 1, 1)
        Arr = Fnd.Value                     ' copies Values, not Formulas
        
        With WsT
            Set Tgt = .Cells(1, 1)
            If f Then Set Tgt = Tgt.Offset(.Cells(.Rows.Count, 1).End(xlUp).Row + 1)
            If VarType(Arr) >= vbArray Then
                Tgt.Resize(UBound(Arr), UBound(Arr, 2)).Value = Arr
            Else
                Tgt.Value = Arr
            End If
        End With
    Next f
End Sub

Observe that I discarded the leading spaces in your search criteria in favour of looking for a partial match in the Find function.请注意,我放弃了搜索条件中的前导空格,转而在Find function 中查找部分匹配项。 In that way it doesn't matter how many spaces there are but it may cause confusion if there several matches.这样,有多少空格并不重要,但如果有多个匹配项,可能会引起混乱。 In that case you might reinstate the blanks by amending the array of Caps .在这种情况下,您可以通过修改Caps数组来恢复空白。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel 2007 VBA:如何从一张纸上的动态范围复制和粘贴到另一张纸的第一行? - Excel 2007 VBA: How do I copy and paste from a dynamic range on one sheet to the first empty row of another sheet? 如何在一张纸到另一张纸中的特定单元格的范围内复制单元格数据? - How do you copy cell data in a range from one sheet to specific cells in another sheet? 如何通过使用VBA映射将数据从一张纸复制到另一张纸 - How copy data from one sheet to another by mapping Using VBA 如何使用VBA将范围内的值从一张纸复制到另一张纸上的预定义订单? - How to copy the values in a range from one sheet to and paste it another sheet on a predefied order using VBA? 如何使用输入从某张纸上的一行中复制某些数据并在另一张纸上显示 - Using an input how do I copy certain data from a row on one sheet and display it in another sheet 如何将一系列数据从一张纸转换到另一张纸 - How do I transpose a range of data from one sheet to another 如何使用VBA将特定数据从一个工作表复制到另一个工作表 - How to copy specific data from one worksheet to another using VBA 如何使用vba将多列数据从一张工作表复制并粘贴到另一张工作表 - How to copy and paste multiple column data from one sheet to another sheet using vba 使用VBA自动将一系列数据从一张纸传输到另一张纸 - Automatically transfering a range of data from one sheet to another using VBA 将数据从一张纸复制到另一张纸以使用 vba 保留日志 - Copy data from one sheet to another to keep a log using vba
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM