6

I am trying to find a way to:

  1. Loop through a column (B column)
  2. Take the values, store them in an array
  3. Loop through that array and do some text manipulation

However, I cannot think of a way to loop through a column and take those values, storing them in an array. I have looked through Stack Overflow and google but have not found a successful solution.

In advance, thank you for your help.

Sub collectNums()

Dim eNumStorage() As String ' initial storage array to take values
Dim i as Integer
Dim j as Integer
Dim lrow As Integer

lrow = Cells(Rows.Count, "B").End(xlUp).Row ' The amount of stuff in the column

For i = lrow To 2 Step -1
    If (Not IsEmpty(Cells(i, 2).Value)) Then ' checks to make sure the value isn't empty
    i = eNumStorage ' I know this isn't right
Next i

If (IsEmpty(eNumStorage)) Then
    MsgBox ("You did not enter an employee number for which to query our database. Quitting")
    Exit Sub
End If

End Sub
Vityata
  • 42,633
  • 8
  • 55
  • 100
artemis
  • 6,857
  • 11
  • 46
  • 99
  • What in particular are you struggling with - there is lots online about creating an array from a range? – SJR Dec 18 '17 at 14:09
  • I have tried a few different methods, but using a test data sample of 10 employee numbers entered, I've yet to find a way to get it to store to an array. @SJR – artemis Dec 18 '17 at 14:11

2 Answers2

6

This is the easiest way to get column to array:

Public Sub TestMe()

    Dim myArray     As Variant
    Dim cnt         As Long

    myArray = Application.Transpose(Range("B1:B10"))

    For cnt = LBound(myArray) To UBound(myArray)
        myArray(cnt) = myArray(cnt) & "something"
    Next cnt
    For cnt = LBound(myArray) To UBound(myArray)
        Debug.Print myArray(cnt)
    Next cnt
End Sub

It takes the values from B1 to B10 in array and it gives possibility to add "something" to this array.

The Transpose() function takes the single column range and stores it as an array with one dimension. If the array was on a single row, then you would have needed a double transpose, to make it a single dimension array:

With Application
    myArray = .Transpose(.Transpose(Range("A1:K1")))
End With
Community
  • 1
  • 1
Vityata
  • 42,633
  • 8
  • 55
  • 100
  • Can you explain what the Transpose function is doing here? Is it by default storing each cell as a value in the array? – artemis Dec 18 '17 at 14:10
  • 1
    Thank you for explaining that. I saw the MSDN article shortly after you posted it. That is very helpful. – artemis Dec 18 '17 at 14:20
3

Just adding a variation on Vityata's which is the simplest way. This method will only add non-blank values to your array. When using your method you must declare the size of the array using Redim.

Sub collectNums()

Dim eNumStorage() As String ' initial storage array to take values
Dim i As Long
Dim j As Long
Dim lrow As Long

lrow = Cells(Rows.Count, "B").End(xlUp).Row ' The amount of stuff in the column
ReDim eNumStorage(1 To lrow - 1)

For i = lrow To 2 Step -1
    If (Not IsEmpty(Cells(i, 2).Value)) Then ' checks to make sure the value isn't empty
        j = j + 1
        eNumStorage(j) = Cells(i, 2).Value
    End If
Next i

ReDim Preserve eNumStorage(1 To j)

'Not sure what this bit is doing so have left as is
If (IsEmpty(eNumStorage)) Then
    MsgBox ("You did not enter an employee number for which to query our database. Quitting")
    Exit Sub
End If

For j = LBound(eNumStorage) To UBound(eNumStorage)  ' loop through the previous array
    eNumStorage(j) = Replace(eNumStorage(j), " ", "")
    eNumStorage(j) = Replace(eNumStorage(j), ",", "")
Next j

End Sub
SJR
  • 22,986
  • 6
  • 18
  • 26
  • 1
    Hey, @SJR. It looks like your solution is working. Thanks a lot for your patience and explaining that to me. – artemis Dec 18 '17 at 14:21