Consulting

Results 1 to 3 of 3

Thread: Closed: My Macro keeps crashing!

  1. #1

    Closed: My Macro keeps crashing!

    I'm using Excel 2003 SP3. The goal is to only account for data as values are increasing. If the number in a cell is 0 it is to be ignored. the data is in columns Q to AI. A 10 cell example is shown below

    0, 100, 200, 0, 400, 0, 0, 200, 0, 300 (initial)
    0, 100, 200, 0, 400, 0, 0, 0, 0, 0 (final)

    Sub Jc_Ic_autocutoffzz()
    Dim zp As Integer
    Dim z2 As Integer
    Dim x As Integer
    Dim jcrit1 As Integer
    Dim jcrit2 As Integer
    Dim jcrit3 As Integer
    For zp = 3 To 200
        If Cells(zp, 17) = 0 Then
            Cells(zp, 17) = 0.000001
        Else
            Cells(zp, 17).Value = Cells(zp, 17)
        End If
        For jcrit2 = 18 To 36
            If Cells(zp, jcrit2) = 0 Then
                jcrit2 = jcrit2 - 1
            End If
            Next jcrit2
                x = 18
                Do While x < 37
                jcrit3 = x - 1
                If Cells(zp, x) < Cells(zp, jcrit3) Then
                    Cells(zp, x + 20).Value = 0
                Else
                    Cells(zp, x + 20).Value = Cells(zp, x)
                End If
                x = x + 1
                Loop
                For z2 = 17 To 36
                    If Cells(zp, z2) = 0.000001 Then
                         Cells(zp, z2 + 20).Value = 0
                     End If
                Next z2
            Next zp
    End Sub

    Right now it keeps crashing so I can't even know if it is working. The next step would be to turn any repeated cells to zero and but leave the left most one. Dang, i stank at this.
    Last edited by Aussiebear; 04-10-2023 at 11:31 PM. Reason: Adjust the code tags

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    I see a potential problem with

    For jcrit2 = 18 To 36
        If Cells(zp, jcrit2) = 0 Then
            jcrit2 = jcrit2 - 1
        End If
      Next jcrit2
    if/when cells(zp,jcrit2).value = 0, since it decrements the loop control variable jcrit2 when the Next statement increments it again - an endless loop.
    Last edited by Aussiebear; 04-10-2023 at 11:32 PM. Reason: Adjusted the code tags
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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