• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 625
  • Last Modified:

Excel VBA: For Each itm in Array,

Hi Guys,

Ok, I have a text box with a bunch of Document Numbers in it, delimited with "|", I have a macro that passes the contents of this text box into an array using MyArray = Split(Textbox value, "|")

Ok, so now I want to run a section of code against each item in that array, how do I do this?

Private Sub CommandButton18_Click()
' Add Distribution to each Doc list in invisible, demlimited with Pipe, text box 25
Dim sList As String, sDocNo As String, CurCol As String, CurCol1 As String
Dim ColTemp As Long
Dim MyArrayX As Variant
Dim itm
Dim iRow As Integer, iRow1 As Integer

sDocNo = TextBox21.Value ' Document code will copy data from

' Find First Column of range to copy
        Rows("8:8").Select
        Set RangeToSearch = Selection
        Set FoundCell = RangeToSearch.Find(What:="DistStart", lookat:=xlWhole)
        RangeToSearch.Find(What:="DistStart", lookat:=xlWhole).Activate
            ColTemp = ActiveCell.Column
            ColTemp = ColTemp + 2
            CurCol = ColumnName(ColTemp)

' Find Last Column of range to copy
        Rows("8:8").Select
        Set RangeToSearch = Selection
        Set FoundCell = RangeToSearch.Find(What:="Do Not Delete", lookat:=xlWhole)
        RangeToSearch.Find(What:="Do Not Delete", lookat:=xlWhole).Activate
            ColTemp = ActiveCell.Column
            CurCol1 = ColumnName(ColTemp)

' Find Row of Doc to copy from
        Range("C:C").Select
        Set RangeToSearch = Selection
        Set FoundCell = RangeToSearch.Find(sDocNo, lookat:=xlWhole)
        RangeToSearch.Find(sDocNo, lookat:=xlWhole).Activate
            iRow = ActiveCell.Row
            

sList = TextBox25.Value  ' Contains my Document Numbers separated by "|"

MyArrayX = Split(sList, "|")

For Each itm In MyArrayX
' Find row of Document in array
        Range("C:C").Select
        Set RangeToSearch = Selection
        Set FoundCell = RangeToSearch.Find(itm.Value, lookat:=xlWhole)
        RangeToSearch.Find(itm.Value, lookat:=xlWhole).Activate
            iRow1 = ActiveCell.Row

' Copy Values from sDocNo document row
        Range(CurCol & iRow & ":" & CurCol1 & iRow).Select
        Selection.Copy
        Range(CurCol & iRow1 & ":" & CurCol1 & iRow1).Select
        Selection.Paste

Next itm


End Sub

Open in new window


itm never takes on a value, not sure what I should declare it as, a string?
But even then not sure if my code is correct for what I want to do.
0
Conor_Newman
Asked:
Conor_Newman
  • 2
1 Solution
 
Martin LissRetired ProgrammerCommented:
Always give your variables a type and don't use Variant unless you have to, so…

Dim MyArrayX() As String
Dim itm As Long

For Itm = 0 to UBound(MYArrayX)
    Debug.Print MyArrayX(itm)
Next
0
 
Conor_NewmanAuthor Commented:
Excellent Martin, thank you very much, that's fixed that issue. :)
0
 
Martin LissRetired ProgrammerCommented:
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2012
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now