Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Excel VBA: For Each itm in Array,

Posted on 2012-04-10
3
Medium Priority
?
609 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 49

Accepted Solution

by:
Martin Liss earned 2000 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 49

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 Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

618 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