2013-09-23 52 views
1

我在Excel中遇到了這個問題,我想用VBA中的宏來解決這個問題。我有一個包含這種格式的數據表:用分隔符複製新表格中的單元格VBA

separator 
1 
2 
6 
3 
8 
342 
532 
separator 
72 
28 
10 
21 
separator 
38 
23 
234 

我想要做的就是創建一個用於創建爲每個系列的數據(一系列從「分隔符」開始和結束一個新的工作表中的VBA宏在下一個或最初的工作表結束時),並複製新工作表中的相應數據。 例子:

1 
2 
6 
3 
8 
342 
532 

在Sheet1

72 
28 
10 
21 

在Sheet2的等 非常感謝你,我很感激!從開始到第一個分離器(「Q」) 此份數據:

Sub macro1() 
Dim x As Integer 
x = 1 

Sheets.Add.Name = "Sheet2" 

'Get cells until first q 

Do Until Sheets("Sheet1").Range("A" & x).Value = "q" 
Sheets("Sheet2").Range("A" & x).Value = Sheets("Sheet1").Range("A" & x).Value 
x = x + 1 
Loop 


End Sub 
+0

'我想要做的是創建一個VBA宏,爲每個數據系列創建一個新表......'Kool!你能告訴我們你到目前爲止所嘗試的是什麼,你究竟在哪裏得到錯誤? –

+0

'子宏1() 昏暗X爲整數 X = 1 Sheets.Add.Name = 「Sheet 2中」 「獲取細胞,直到第一Q 做,直到表( 「工作表Sheet」)。範圍( 「A」 &x).Value =「q」 表(「Sheet2」)。Range(「A」&x).Value = Sheets(「Sheet1」)。Range(「A」&x).Value x = x + 1 Loop End Sub '分隔符是「q」,這隻創建一個新工作表(工作表2)並添加所有數據直到該工作表中的第一個「q」。下一個? –

+1

你能用代碼更新你的問題嗎?在評論中閱讀代碼真的很難...... –

回答

1

嘗試......(未經測試)

Const sep As String = "q" 

Sub Sample() 
    Dim ws As Worksheet, wsNew As Worksheet 
    Dim lRow As Long, i As Long, rw As Long 

    '~~> Set this to the relevant worksheet 
    Set ws = ThisWorkbook.Sheets("Sheet1") 
    '~~> Add a new temp sheet 
    Set wsNew = ThisWorkbook.Sheets.Add 

    '~~> Set row for the new output sheet 
    rw = 1 

    With ws 
     '~~> Get the last row 
     lRow = .Range("A" & .Rows.Count).End(xlUp).Row 

     '~~> Loop through the cells from row 2 
     '~~> assuming that row 1 has a spearator 
     For i = 2 To lRow 
      If .Range("A" & i).Value = sep Then 
       Set wsNew = ThisWorkbook.Sheets.Add 
       rw = 1 
      Else 
       wsNew.Cells(rw, 1).Value = .Range("A" & i).Value 
       rw = rw + 1 
      End If 
     Next i 
    End With 
End Sub 
+0

它的工作,非常感謝你! :) –

0

你可以利用這一點避免循環每一行。只要你想刪除原始數據。

SubSample() 
Dim x As Integer 
Dim FoundCell As Range 
Dim NumberOfQs As Long 
Dim SheetWithData As Worksheet 
Dim CurrentData As Range 

Set SheetWithData = Sheets("Sheet4") 
NumberOfQs = WorksheetFunction.CountIf(SheetWithData.Range("A:A"), "q") 

x = 1 


Set FoundCell = SheetWithData.Range("A1", SheetWithData.Range("A" & Rows.Count)).Find("q", , , , , xlPrevious) 

If Not FoundCell Is Nothing Then 
    Set LastCell = FoundCell.End(xlDown) 
    Set CurrentData = SheetWithData.Range(FoundCell, LastCell) 
    Sheets.Add.Name = "QSheetNumber" & x 'Get cells until first q 
    CurrentData.Cut Sheets("QSheetNumber" & x).Range("A1") 
    Sheets("QSheetNumber" & x).Rows(1).Delete 
    x = x + 1 
    Set FoundCell = SheetWithData.Range("A1", SheetWithData.Range("A" & Rows.Count)).Find("q", FoundCell, , , , xlPrevious) 
    If Not FoundCell Is Nothing Then 
     Set LastCell = FoundCell.End(xlDown) 
     Set CurrentData = SheetWithData.Range(FoundCell, LastCell) 
     Sheets.Add.Name = "QSheetNumber" & x 'Get cells until first q 
     CurrentData.Cut Sheets("QSheetNumber" & x).Range("A1") 
     Sheets("QSheetNumber" & x).Rows(1).Delete 
     x = x + 1 
    Else 
     Exit Sub 
    End If 
Else 
    Exit Sub 
End If 

End Sub 
相關問題