Page 2 of 2 FirstFirst 12
Results 11 to 15 of 15

Thread: For loop with irregular step or use arrays if possible

  1. #11
    MrExcel MVP mikerickson's Avatar
    Join Date
    Jan 2007
    Location
    Davis CA
    Posts
    22,647
    Post Thanks / Like
    Mentioned
    20 Post(s)
    Tagged
    15 Thread(s)

    Default Re: For loop with irregular step or use arrays if possible

    You miss my point. I'm saying that you should use your current loop. Just add code that tests for the VLookup, if it doesn't exist then don't do anything, just go to the next loop.

  2. #12
    Board Regular kelly mort's Avatar
    Join Date
    Apr 2017
    Location
    Suhum, Ghana, West Africa
    Posts
    1,566
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Default Re: For loop with irregular step or use arrays if possible

    Delete
    Last edited by kelly mort; Oct 12th, 2019 at 08:31 PM. Reason: Deleted
    There Is Always A Better Way

  3. #13
    Board Regular kelly mort's Avatar
    Join Date
    Apr 2017
    Location
    Suhum, Ghana, West Africa
    Posts
    1,566
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Default Re: For loop with irregular step or use arrays if possible

    Hi mike,

    So I was playing around with cooler ways to test for the availability of the items in column B.

    The one I chose to use is:

    Code:
    If Application.CountIf(Range,Item)= 1 Then
           Do something
    End If
    And its working fine.

    I appreciate the clue given.

    Thanks to you @offthelip too
    There Is Always A Better Way

  4. #14
    MrExcel MVP
    Moderator
    Peter_SSs's Avatar
    Join Date
    May 2005
    Location
    Macksville, Australia
    Posts
    41,390
    Post Thanks / Like
    Mentioned
    100 Post(s)
    Tagged
    21 Thread(s)

    Default Re: For loop with irregular step or use arrays if possible

    You shouldn't have to do any loop from 1 to 100 to find a few values if you don't want to


    Quote Originally Posted by kelly mort View Post
    So let say I have the numbers 34, 5, 27 in the column B
    If they are in consecutive cells starting from, say B2 then you could use something like this to just grab the relevant values & just loop through those values.

    Code:
    Sub getvaluesintoarray1()
      Dim a As Variant, itm As Variant
      
      a = Range("B2", Range("B" & Rows.Count).End(xlUp)).Value
      For Each itm In a
        'Do something
      Next itm
    End Sub
    If the values may be scattered anywhere in, say, B2:B100 (& the rest of the cells are blank) then you can use something like this to just grab those values and loop through them.

    Code:
    Sub getvaluesintoarray2()
      Dim a As Variant, itm As Variant
      
      a = Filter(Application.Transpose(Evaluate("if(len(B2:B100),B2:B100,""%"")")), "%", False)
      For Each itm In a
        'Do something
      Next itm
    End Sub
    Hope this helps, good luck.
    Peter
    Excel 365 - Windows 10
    - Want to help your helpers by posting a small, copyable, screen shot directly in your post? Look here
    - If posting vba code, please use Code Tags - like this [code]Paste code here[/code] - or use the # key in the Reply window
    - Read: Forum Rules, Forum Use Guidelines, & FAQ

  5. #15
    Board Regular kelly mort's Avatar
    Join Date
    Apr 2017
    Location
    Suhum, Ghana, West Africa
    Posts
    1,566
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Default Re: For loop with irregular step or use arrays if possible

    Quote Originally Posted by Peter_SSs View Post
    You shouldn't have to do any loop from 1 to 100 to find a few values if you don't want to


    If they are in consecutive cells starting from, say B2 then you could use something like this to just grab the relevant values & just loop through those values.

    Code:
    Sub getvaluesintoarray1()
      Dim a As Variant, itm As Variant
      
      a = Range("B2", Range("B" & Rows.Count).End(xlUp)).Value
      For Each itm In a
        'Do something
      Next itm
    End Sub
    If the values may be scattered anywhere in, say, B2:B100 (& the rest of the cells are blank) then you can use something like this to just grab those values and loop through them.

    Code:
    Sub getvaluesintoarray2()
      Dim a As Variant, itm As Variant
      
      a = Filter(Application.Transpose(Evaluate("if(len(B2:B100),B2:B100,""%"")")), "%", False)
      For Each itm In a
        'Do something
      Next itm
    End Sub
    Your suggestions are very great points for me to look at.

    I just realised how useful the For Each Loop can be for what I am doing.
    There Is Always A Better Way

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •