我对vba比较新。我很久以前就使用过VB了,所以我从那次经历中得到了很多信息。虽然现在我面临着一项艰巨的任务,但我不知道该怎么做。
我有一个包含E列软件版本信息的数据表(即“3.1.1”,“3.1.2”等)。我已经通过E创建了一个for循环搜索。在这里有几个像这样的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...
这对我来说很好,除了我需要在运行宏之前创建工作表。我想做的是:
我很想听听你们的想法。
答案 0 :(得分:1)
也许这会有所帮助:
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中使用集合而不是数组,因为我可以动态添加新元素而无需调整大小。 (但这取决于具体情况......)