?
Solved

Excel VBA: For Each itm in Array,

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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
New style of hardware planning for Microsoft Exchange server.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

764 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