+ Reply to Thread
Results 1 to 4 of 4

for loop anomaly

  1. #1
    Registered User
    Join Date
    02-09-2021
    Location
    Milwaukee, WI
    MS-Off Ver
    365
    Posts
    6

    for loop anomaly

    Hello, I have a code where, when I click the button, the "for" loop goes through some of the data and suddenly jumps out of the loop and gives me the dialog box saying that it's done. In an effort to find out why, I stepped through using F8. Doing that allowed it to finish the "for" loop for all of the data. When holding down F8, it jumped out again. The code is below. I could not find anything like this online. Do I need to slow the code down somehow? Doesn't that defeat the purpose of a macro? Please let me know if you need the full code and a sample spreadsheet. It will take me time to put that together, but hopefully the code below shows some obvious error that I missed.

    Please Login or Register  to view this content.
    Last edited by proudgeek42; 02-24-2021 at 12:46 PM.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: for loop anomaly

    Hi proudgeek,

    Looks like you need to learn about "Select Case" in your code. Attach the workbook and we can shorten your code.
    https://docs.microsoft.com/en-us/off...case-statement
    Read the Yellow banner to learn how.
    Also "DoEvents" may be needed to allow other OS things to happen and not to jump out of your code.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Registered User
    Join Date
    02-09-2021
    Location
    Milwaukee, WI
    MS-Off Ver
    365
    Posts
    6

    Re: for loop anomaly

    Attached is a sample spreadsheet. I have a lot more data (labeled as A-Z in this sheet to limit the amount of data).
    I ran the macro on this sheet (button click) and it worked perfectly. I assume the problem comes with a lot more data.
    I appreciate help to shorten my code too. I'm not very proficient or efficient at VBA yet.
    In the interest of learning, if possible and not too much trouble, please explain very basic theory behind modified code.
    I know the Microsoft link does that, but I often don't understand what they are saying as they don't put it in laymen's terms that I can understand.

  4. #4
    Registered User
    Join Date
    02-09-2021
    Location
    Milwaukee, WI
    MS-Off Ver
    365
    Posts
    6

    Re: for loop anomaly

    I think I may have fixed it on my own. What I did was to move the lastCol variable to after calling another function. Also, I took the formatting out of the loop and highlighted the entire set of data to format the lines between them. I will mark this as solved for now. I did some research on select case and it might clean the code up a little bit, but based on other people's studies it doesn't seem to speed up the code very much more than if statements. Another thing that I may have benefited from would have been to use an array to copy/paste the data (which wasn't the problem, but I want people to consider that). It saves the screen flashing and performs much faster. Eh, next time.

+ 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. Another counting anomaly
    By Alan Beban in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-05-2018, 07:22 PM
  2. Summation Anomaly
    By dups1822 in forum Excel General
    Replies: 3
    Last Post: 07-14-2017, 08:39 AM
  3. COUNTIF Anomaly
    By shg in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 05-24-2017, 02:35 PM
  4. [SOLVED] UserForm textbox save loop anomaly
    By radddogg in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-09-2016, 06:16 AM
  5. IF function anomaly
    By BBS in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 12-07-2007, 11:51 AM
  6. DATEDIF anomaly?
    By cruisy in forum Excel General
    Replies: 5
    Last Post: 05-25-2007, 12:10 AM
  7. [SOLVED] Formatting Anomaly
    By Christopher Weaver in forum Excel General
    Replies: 2
    Last Post: 05-13-2005, 06:06 PM

Tags for this Thread

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