+ Reply to Thread
Results 1 to 4 of 4

Why is activecell moving more than down 1 in my for k loop?

Hybrid View

  1. #1
    Registered User
    Join Date
    06-15-2017
    Location
    Saskatchewan, Canada
    MS-Off Ver
    Microsoft Office 2013
    Posts
    2

    Why is activecell moving more than down 1 in my for k loop?

    My for J loop works well, and the For k works ok for the first iteration then jumps to B19 and doesn't move. I want my program to work for the k loop to work identical to the J loop where it selects the next value before looping. Totalval is a sum containing the amount of rows in the data to create the number of iterations to calculate. Sorry about the message boxes in the code, It helps me to keep track of what everything is


    For k = 0 To totalvals Step 1
        
    For j = 0 To totalvals Step 1
            'MsgBox "in J for loop"
            If j = 0 Then
                'MsgBox "in for if J loop"
                Range("b3").Select
                'MsgBox ActiveCell.Value & " is the cell value"
            Else
            End If
        
            total = total + ActiveCell.Value
            ActiveCell.Offset(1, 0).Select
            'MsgBox " after offset, cell value is " & ActiveCell.Value
        
            If j = totalvals Then
                'MsgBox "j = total val now"
                profit = total * Range("h6")
                'MsgBox "Current total profit is " & profit
                total = 0
            Else
            End If
        Next j
        
       If k = 0 Then
            MsgBox " k = 0 , select cell c3 "
            MsgBox "profit from before " & profit & " " & "Starting total: " & total
            Range("c3").Select
            MsgBox "activecell.value is " & ActiveCell.Value
        Else
        End If
        
        total = total + ActiveCell.Value
        MsgBox " In K Current units sold " & total
        ActiveCell.Offset(1, 0).Select
        MsgBox " after offset, cell value is " & ActiveCell.Value
        MsgBox ActiveCell.Row & ActiveCell.Column
    
    next k
    Last edited by oveedrx; 06-15-2017 at 05:47 PM. Reason: Please use code tags!

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Why is activecell moving more than down 1 in my for k loop?

    Any chance you could upload a sample workbook?

    Click on GO ADVANCED, scroll down and click Manage Attachments.
    If posting code please use code tags, see here.

  3. #3
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,943

    Re: Why is activecell moving more than down 1 in my for k loop?

    For k = 0 To totalvals
    ' for every step in k, j is reset to 0 and B3 is reselected no matter what
    For j = 0 To totalvals
    If j = 0 Then
    Range("b3").Select
    End If

    ActiveCell.Offset(1, 0).Select

    Next j


    ' once k is incremented, C3 is never selected again
    If k = 0 Then
    Range("c3").Select
    End If


    ' first time through the k loop cell C4 is selected
    ' however, for K > 0 you remain in column B and offset from the last cell in the J loop, what ever it was.

    ActiveCell.Offset(1, 0).Select

    Next k
    Ben Van Johnson

  4. #4
    Registered User
    Join Date
    06-15-2017
    Location
    Saskatchewan, Canada
    MS-Off Ver
    Microsoft Office 2013
    Posts
    2

    Re: Why is activecell moving more than down 1 in my for k loop?

    Hi thanks Ben,

    I tried this and it seems like it is working

    For j = j To totalvals Step 1
    From what I think this means is that It takes the value of J which I haven't assigned a value, so it becomes zero and adds 1 until j > totalvals and then exits to K. When K reiterates on its second run, J for loop is still outside the range for it to operate. Unlike previously when K reiterated j was assigned to zero and the loop started agai

+ 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. Loop Autofill (based on activecell, not cell reference, varying row counts)
    By xtinct2 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-23-2015, 12:29 PM
  2. ActiveCell.FormulaR1C1 get next column in loop
    By dwr0211 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-13-2012, 09:29 PM
  3. Output contents of Activecell when Activecell may be string or numeric.
    By jfriddle in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-09-2012, 12:27 PM
  4. Moving from For...Next loop to subroutine and back to loop
    By zabrahamson in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-25-2011, 01:26 PM
  5. For each loop, 1004 error, if ... then "activecell.offset" attempt
    By AdamParker in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-22-2010, 03:30 PM
  6. Help: Activecell within Do loop problem
    By DavidShepherd in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-12-2005, 06:00 AM
  7. Loop with activecell.offset
    By mthomas in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-18-2005, 05:29 PM

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