Results 1 to 4 of 4

for loop anomaly

Threaded View

  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.

    For x = 2 To lastCol
    
    On Error Resume Next
    
        If Cells(13, x).Value <= 50 And Cells(17, x).Value = "Solar" Then
            Cells(13, x).Interior.ColorIndex = 45
        ElseIf Cells(13, x).Value <= 50 And Cells(17, x).Value = "Gas" Then
            Cells(13, x).Interior.ColorIndex = 41
        ElseIf Cells(13, x).Value <= 50 And Cells(17, x).Value = "Storage" Then
            Cells(13, x).Interior.ColorIndex = 48
        ElseIf Cells(13, x).Value <= 50 And Cells(17, x).Value = "Wind" Then
            Cells(13, x).Interior.ColorIndex = 44
    
        ElseIf Cells(13, x).Value <= 100 And Cells(17, x).Value = "Solar" Then
            Range(Cells(13, x), Cells(12, x)).Interior.ColorIndex = 45
        ElseIf Cells(13, x).Value <= 100 And Cells(17, x).Value = "Gas" Then
            Range(Cells(13, x), Cells(12, x)).Interior.ColorIndex = 41
        ElseIf Cells(13, x).Value <= 100 And Cells(17, x).Value = "Storage" Then
            Range(Cells(13, x), Cells(12, x)).Interior.ColorIndex = 48
        ElseIf Cells(13, x).Value <= 100 And Cells(17, x).Value = "Wind" Then
            Range(Cells(13, x), Cells(12, x)).Interior.ColorIndex = 44
            
    'There's a bunch more of the same code blocks with values incrementing by 50.
            
        ElseIf Cells(13, x).Value > 600 And Cells(17, x).Value = "Solar" Then
            Range(Cells(13, x), Cells(1, x)).Interior.ColorIndex = 45
        ElseIf Cells(13, x).Value > 600 And Cells(17, x).Value = "Gas" Then
            Range(Cells(13, x), Cells(1, x)).Interior.ColorIndex = 41
        ElseIf Cells(13, x).Value > 600 And Cells(17, x).Value = "Storage" Then
            Range(Cells(13, x), Cells(1, x)).Interior.ColorIndex = 48
        ElseIf Cells(13, x).Value > 600 And Cells(17, x).Value = "Wind" Then
            Range(Cells(13, x), Cells(2, x)).Interior.ColorIndex = 44
        End If
        
    'This section inserts vertical lines to better view each entry
        Range(Cells(1, x), Cells(17, x)).Select
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .ColorIndex = xlAutomatic
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        Selection.Borders(xlEdgeTop).LineStyle = xlNone
        Selection.Borders(xlEdgeBottom).LineStyle = xlNone
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .ColorIndex = xlAutomatic
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .ColorIndex = xlAutomatic
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
    Next x
        
    'Color index numbers and fuel type
    'solar   - orange    - 45
    'gas     - blue      - 41
    'storage - grey      - 48
    'wind    - yellow    - 44
    
    MsgBox "COD Timeline has been updated."
    Last edited by proudgeek42; 02-24-2021 at 12:46 PM.

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. 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