Solved

Excel VBA: For Each itm in Array,

Posted on 2012-04-10
3
524 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
  • 2
3 Comments
 
LVL 45

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 45

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
Outlook Free & Paid Tools
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now