循环和偏移以找出并复制所有出现次数

时间:2016-05-13 17:56:41

标签: excel vba

我试图让这段代码复制整个行,在该行中找到所有出现的Target变量。然后它需要将它传递到一个与变量同名的新工作表中。该表已由整个程序的不同部分创建。

Sub Copy_Data()

 Dim NumberofFunctions As Integer
 Dim X As Integer
 Dim Target As String
 Dim Last_Row As Long
 Dim Last_Column As Long

 Sheets("Calculations").Select
 NumberofFunctions = ActiveWorkbook.Worksheets("Calculations").Range("B3", Worksheets("Calculations").Range("B3").End(xlDown)).Rows.Count





     For X = 1 To NumberofFunctions
        Sheets("Calculations").Select
        Target = Range("B2").Offset(X, 0)

        Sheets("Raw Data").Select
        NumberofFunctions = ActiveWorkbook.Worksheets("Calculations").Range("B3", Worksheets("Calculations").Range("B3").End(xlDown)).Rows.Count

       '************This code finds the last row used in the sheet******
        Last_Row = Cells.Find(What:="*", _
                After:=Range("A1"), _
                LookAt:=xlPart, _
                LookIn:=xlFormulas, _
                SearchOrder:=xlByRows, _
                SearchDirection:=xlPrevious, _
                MatchCase:=False).Row

        '***********This code finds the last column used in the sheet****
        Last_Column = Cells.Find(What:="*", _
                After:=Range("A1"), _
                LookAt:=xlPart, _
                LookIn:=xlFormulas, _
                SearchOrder:=xlByRows, _
                SearchDirection:=xlPrevious, _
                MatchCase:=False).Column

        '*************This portion of the code finds the occurrences******
          ActiveWorkbook.Worksheets("Raw Data").Range("F:F", Worksheets("Raw Data")).Find(What:=(Target), _
                After:=ActiveCell, _
                LookIn:=xlFormulas, _
                LookAt:=xlPart, _
                SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, _
                MatchCase:=False, _
                SearchFormat:=False).Activate


      Next X

 End Sub

0 个答案:

没有答案