Solved

Excel VBA: For Each itm in Array,

Posted on 2012-04-10
3
546 Views
Last Modified: 2012-04-10
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
Comment
Question by:Conor_Newman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 47

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 37828184
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
 
LVL 2

Author Closing Comment

by:Conor_Newman
ID: 37828287
Excellent Martin, thank you very much, that's fixed that issue. :)
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 37828304
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2012
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

My experience with Windows 10 over a one year period and suggestions for smooth operation
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question