如何根据条件将所需的数据行从一个Excel工作表复制到另一个工作表?

时间:2009-11-20 07:05:13

标签: excel-vba vba excel

我有一张包含7k行数据的excel表,我需要根据列(组名)将这些数据分成三个不同的表。

所以,我需要根据组名(有3组)将数据分成三个不同的表。

请告诉我有关...

提前致谢...

1 个答案:

答案 0 :(得分:0)

你可以尝试这样的东西,把它放在一个宏中并按需运行

Sub Macro1()
Dim rowCounter As Integer
Dim usedRange As Range
Dim sourceSheet As Worksheet, dest1 As Worksheet, dest2 As Worksheet, dest3 As Worksheet
Dim dest1Row As Integer, dest2Row As Integer, dest3Row As Integer

    dest1Row = 1
    dest2Row = 1
    dest3Row = 1
    Set sourceSheet = Sheets("Sheet1")
    Set dest1 = Sheets("Sheet2")
    Set dest2 = Sheets("Sheet3")
    Set dest3 = Sheets("Sheet4")
    Set usedRange = sourceSheet.usedRange

    For rowCounter = 1 To usedRange.Rows.Count
        If (usedRange(rowCounter, 1) = 1) Then
            usedRange.Range(Cells(rowCounter, 1), Cells(rowCounter, usedRange.Columns.Count)).Copy dest1.Range("A" & dest1Row)
            dest1Row = dest1Row + 1
        ElseIf (usedRange(rowCounter, 1) = 2) Then
            usedRange.Range(Cells(rowCounter, 1), Cells(rowCounter, usedRange.Columns.Count)).Copy dest2.Range("A" & dest2Row)
            dest2Row = dest2Row + 1
        ElseIf (usedRange(rowCounter, 1) = 3) Then
            usedRange.Range(Cells(rowCounter, 1), Cells(rowCounter, usedRange.Columns.Count)).Copy dest3.Range("A" & dest3Row)
            dest3Row = dest3Row + 1
        End If
    Next rowCounter
End Sub