从数组值自动创buildExcel工作表

我对vba比较陌生。 我很久以前就用VB了,所以我从这个经验中获得了很多信息。 虽然现在我面临着一个更难的任务,我不知道该怎么做。

我在E栏软件版本信息(即“3.1.1”,“3.1.2”等)中有一个数据表。 我创build了一个for循环通过E的search。在这里有几个像这样的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... 

这对我来说工作得很好,除了我需要在运行macros之前创build工作表。 我想要做的是:

  1. 通过列Esearch
  2. 用列E中的所有唯一值填充一个数组* [edit]
  3. 为数组中的每个值创build一个工作表

我很想听听你们的想法。

也许这有助于:

 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中使用比数组更多的集合,因为我可以在不resize的情况下dynamic添加新元素。 (但这取决于情况…)