+ Reply to Thread
Results 1 to 18 of 18

Ineffecient Looping - alternatives to coding?

  1. #1
    Registered User
    Join Date
    11-23-2013
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    38

    Ineffecient Looping - alternatives to coding?

    Hi all:

    I'm a VBA and programming newbie - if I dare say and am faced with a question that I hope someone can help me think through.

    I've read and been told to avoid looping if at all possible since it's slow; however, for my solution I don't think I can get around it since in my worksheet I need to determine if a particular value exist in a column, then copy the value of that row in Column "A" into a new worksheet in the same column "A".

    For example:
    • I have 2 columns in my worksheet: "A" and "B", where Column "A" is Name, and Column "B" is Payment Made.
    • The worksheet for this example is 2000 rows of names. Column "B" value that I'm looking for is the value "Yes".
    • If the value of "Yes" is found, copy the value in Column "A" (Name) into a new worksheet in the first column (A) and so on.

    So what is the most efficient way to code this without using For I loop, or IF Bx = "Yes", .....etc?

    Thanks!

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Ineffecient Looping - alternatives to coding?

    Hi, MelissaGregory,

    narrow the number of recorda down by using Autofilter or using WorksheetFunction.CountIf prior to looping the visible range, using Find/FindNext or Match.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Ineffecient Looping - alternatives to coding?

    Hi..

    Try this..
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    11-23-2013
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Ineffecient Looping - alternatives to coding?

    Thanks Holger:

    I'll have to read up on this AutoFilter and how code the syntax - not sure if I understand properly, so if I put in some code that will apply a filter such that it will show only rows of column A where the value is "Yes", and then perform the copy of column B to the new worksheet?

    I don't even know where to begin to write the VBA code, but will need to put some thought into it.

    Melissa

  5. #5
    Registered User
    Join Date
    11-23-2013
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Ineffecient Looping - alternatives to coding?

    Thanks apo: Sorry didn't see your posting......if you get a chance can you walk me through your sample code to help me understand?

    Melissa.

  6. #6
    Registered User
    Join Date
    11-23-2013
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Ineffecient Looping - alternatives to coding?

    Hi Holger:

    A thought came to mind: so if I go into the worksheet and apply a filter on Column "A" to show just rows with "Yes", and it returns 10 filtered rows from 2000 rows that matches "yes", and I run the macro to look for the value "Yes", does that mean that the macro won't necessary have to go through all 2000 rows, but only looks in the 10 filtered rows?

    Sorry if this sounds like a silly question.

    Appreciate it.
    Melissa

    Quote Originally Posted by HaHoBe View Post
    Hi, MelissaGregory,

    narrow the number of recorda down by using Autofilter or using WorksheetFunction.CountIf prior to looping the visible range, using Find/FindNext or Match.

    Ciao,
    Holger

  7. #7
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Ineffecient Looping - alternatives to coding?

    Hi, Melissa,

    you may take apoīs code and have a go on that one. The following code is adapted and commented so that you should get an image of what the code should be doing:
    Please Login or Register  to view this content.
    How to install your new code
    1. Copy the Excel VBA code
    2. Select the workbook in which you want to store the Excel VBA code
    3. Press Alt+F11 to open the Visual Basic Editor
    4. Choose Insert > Module
    5. Edit > Paste the macro into the module that appeared
    6. Close the VBEditor
    7. Save your workbook (Excel 2007+ select a macro-enabled file format, like *.xlsm)

    To run the Excel VBA code:
    1. Press Alt-F8 to open the macro list
    2. Select a macro in the list
    3. Click the Run button
    Ciao,
    Holger

  8. #8
    Registered User
    Join Date
    11-23-2013
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Ineffecient Looping - alternatives to coding?

    Thank you Holger:

    I think I'm starting to get it, but unfortunately, I still am a bit unclear with a few things - I appreciate any help you or anyone can offer:

    1. I'm confused why "1" is minus from the Rows.Count [ie: Resize(.Rows.Count - 1)]
    2. What does SpecialCells(12) do and why the number "12"?
    3. Why is rng need to be set to "Nothing"?

    Sorry for all the questions, but if I can understand more of code, I'll eventually get there.

    Thanks!
    Melissa

    Quote Originally Posted by HaHoBe View Post
    Hi, Melissa,

    you may take apoīs code and have a go on that one. The following code is adapted and commented so that you should get an image of what the code should be doing:
    Please Login or Register  to view this content.
    Ciao,
    Holger

  9. #9
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Ineffecient Looping - alternatives to coding?

    Hi, Melissa,

    I'm confused why "1" is minus from the Rows.Count [ie: Resize(.Rows.Count - 1)]
    Before that command is a command to offset and build the range not including the header but to exclude the header. And that range should be 1 line shorter than the number of rows included into the area in order to avoid an extra empty row to be included.
    What does SpecialCells(12) do and why the number "12"?
    The long value for the constant xlCellTypeVisible which I would only use if I where to automate Excel via Late Binding from another Application as I think the constants being used to be more informative about what any command is used for.
    Why is rng need to be set to "Nothing"?
    According to Microsoft the object doesnīt need to be reset to Nothing as the scope of the variable should end when exiting the procedure. I follow the way to end all things that I have created in reverse order to terminate them, thatīs why this command is in the code.

    Please do not fully quote posts.

    Ciao,
    Holger

  10. #10
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Ineffecient Looping - alternatives to coding?

    Hi..

    What does SpecialCells(12) do and why the number "12"?
    For further info on SpecialCells method..

    http://msdn.microsoft.com/en-us/libr...ffice.15).aspx

    I use SpecialCells(12) instead of SpecialCells(xlCellTypeVisible) because it is less to type...

  11. #11
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Ineffecient Looping - alternatives to coding?

    I do not think loop will be inefficient for this if you will load data into array and loop through this.
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  12. #12
    Registered User
    Join Date
    11-23-2013
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Ineffecient Looping - alternatives to coding?

    Oh boy.....arrays? I'm nowhere there yet, but I guess I can do some research on how to load worksheet cells into it and try. I"m not sure how an array would not be inefficient though.

    Holger/apo: thanks for the explanations...I think it's finally clear!

  13. #13
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Ineffecient Looping - alternatives to coding?

    I do not think this will be very slow:
    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    11-23-2013
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Ineffecient Looping - alternatives to coding?

    Hi Holger:

    I think I fully understand this piece of logic/code now: many thanks to you and all. I tested it and it works....however, I tried to incorporate the code into a worksheet here at work, but I'm getting an error

    I believe that it has something to do with Cells "A1" through "C1" in my worksheet, which has text/titles and descriptions, so the data doesn't really begin until "A2", which has the headers and then the data below at "A3"

    I tried changing the code:

    "With Sheets("Sheet1").Range("A1").CurrentRegion"
    to

    With Sheets("Sheet1").Range("A2:G1000").CurrentRegion

    but I"m still getting an error:

    "Run-Time Error "1004"
    AutoFilter method of Range Class Failed"

    When I remove the entire row beginning in Cell A1, which has the titles/text, the macro works.


    Sorry, can you or anyone help? I thought I defined the range correctly, and I don't understand why it's not working.

    Melissa

  15. #15
    Registered User
    Join Date
    11-23-2013
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Ineffecient Looping - alternatives to coding?

    I'm curious: Is there a function that can return the number of rows that meet the AutoFilter criteria?

  16. #16
    Registered User
    Join Date
    11-23-2013
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Ineffecient Looping - alternatives to coding?

    I just found an issue to this code:

    If there are no records found via the AutoFilter, the Macro returns an error message

    "Run-Time Error: 1004:" No Cells where found.

    Is there any logic that can be built to perhaps display a message saying "No records with Yes in field" and then exit the routine?

    Thanks
    Melissa

  17. #17
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Ineffecient Looping - alternatives to coding?

    Hi, Melissa,

    Please Login or Register  to view this content.
    means the area surrounding the cell A1 until any fully empty column or row is found. I doubt that giving an area address would make a lot of sense because if I would do so I would make sure to reference the corresponding area and not have to work with CurrentRegion in addition. Please note that if you change the address and any data would be found in Row 1 Row 1 still would be used for Autofilter. Without seeing the data in your workbook that may be one reason as well as my favourite reason (merged cells ).

    Is there a function that can return the number of rows that meet the AutoFilter criteria?
    If tehre is data found you may use
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    This should help with the run time error if no data is found.

    Ciao,
    Holger
    Last edited by HaHoBe; 03-11-2014 at 02:22 AM.

  18. #18
    Registered User
    Join Date
    11-23-2013
    Location
    Chicago
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Ineffecient Looping - alternatives to coding?

    Thanks Holger: you're a star! Works fine

    Appreciate it much.
    Melissa

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Vb macro coding for transpose - complete coding?
    By Dharani Suresh in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 08-02-2013, 11:16 PM
  2. [SOLVED] Vb macro coding for transpose - complete coding?
    By Dharani Suresh in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-02-2013, 05:04 AM
  3. [SOLVED] Creating a simplified version of existing ineffecient code.
    By vexel77 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-21-2013, 06:20 PM
  4. [SOLVED] CheckBox coding to work with ComboBox coding
    By JimExcel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-05-2013, 12:23 PM
  5. Howto write the looping coding between worksheets and workbooks?
    By cboys00 in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 07-11-2012, 07:17 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1