2013-06-18 42 views
1

我對vba比較陌生。我很久以前就使用了VB,所以我從中獲得了很多信息。雖然現在我面臨着更艱鉅的任務,我不知道該怎麼做。從數組值自動創建Excel工作表

我有一個E欄軟件版本信息(即「3.1.1」,「3.1.2」等)的數據表。我創建了一個for循環E的搜索,在本作有幾個if語句像這樣的:

If Cells(r, Columns("E").Column).Value = "3.1.2" Then 'find criteria 

      'Copy the current row 
      Rows(r).Select 
      Selection.Copy 

      'Switch to the sprint where you want to paste it & paste 
      Sheets("Sprint 2").Select 
      Rows(sprint2).Select 
      ActiveSheet.Paste 

      sprint2 = sprint2 + 1 'next row 

      'Switch back to backlog & continue to search for criteria 
      Sheets("Backlog").Select 
ElseIf... 

這是對我工作的罰款,但我需要在運行宏之前創建的工作表。我想這樣做的是:

  1. 搜索通過E列
  2. 填寫在列E * [編輯]所有唯一值的數組
  3. 創建的陣列
  4. 每個值表

我很想聽聽你們的想法。

+0

您是否指E列中的唯一值?你已經創建了一個數組? – MiVoth

+0

是的,這就是我的意思。不,我還沒有製作或填充陣列,因爲我對陣列的知識非常有限。 – Philip

回答

0

也許可以幫助:

Sub ColumnE() 
Dim colE As Long, r As Long, c As Object, exists As Boolean 
Dim values As Collection, i As Long 
Set values = New Collection 
colE = Columns("E").Column 
r = Cells(Rows.Count, colE).End(xlUp).Row 
For i = 1 To r ' step 1: loop through column E 
    exists = False 
    For Each c In values ' step 2: look in collection if the element was already inserted 
     If c = Cells(i, colE) Then 
      exists = True 
      Exit For 
     End If 
    Next c 
    If Not exists Then values.Add Cells(i, colE) 
Next i 
For Each c In values ' step 3: add a sheet for every value in collection 
    Worksheets.Add ' WARNING: you should test, if there already is a sheet with that name 
    ActiveSheet.name = c 
Next c 
End Sub 

我喜歡用集合比數組更多的VBA,因爲我可以動態地添加新的元素,而無需調整。 (但這取決於具體情況...)

+0

非常感謝你! – Philip

+0

現在我應該使用什麼命令來調用工作表?換句話說:'Sheets(「Sprint 2」)。選擇「我應該怎樣改變? '表格(c)。選擇'和'表格(「c」)。選擇'兩者都不起作用。 – Philip

+0

試試'表格(CStr(c))。激活'。 (但'c'只在'for each'循環中定義。 – MiVoth