1

I am relatively new to vba. I have used VB a long time ago so I derive a lot of info from that experience. Though now I'm facing a harder task and I don't quite know how to do it.

I have got a data sheet with in column E software version information (ie "3.1.1", "3.1.2" and so on). I have created a for loop the searches through E. In this for there are several if statements like this one:

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...

This is working fine for me, except that I need to create the sheets before running the macro. What I would like to do is:

  1. Search through column E
  2. Fill an array with all unique values in Column E*[edit]
  3. Create a sheet for every value in the Array

I would love to hear what you guys think.

2
  • Do you mean unique values in column E? Have you already created an array?
    – MiVoth
    Commented Jun 18, 2013 at 11:19
  • Yes that is what I mean. And no i have not yet made or filled the Array since my knowledge of Arrays is very limited.
    – Philip
    Commented Jun 18, 2013 at 11:31

1 Answer 1

1

Perhaps that helps:

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

I like to use collections more than arrays in vba, because i can dynamically add new elements without resizing. (but it depends on the situation...)

2
  • what command should I now use to call the sheet? In other words: Sheets("Sprint 2").Select what should I change that to? Sheets(c).Select and Sheets("c").Select both won't work.
    – Philip
    Commented Jun 18, 2013 at 12:23
  • Try Sheets(CStr(c)).Activate. (but c is only defined in the for each loop.
    – MiVoth
    Commented Jun 18, 2013 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.