+ Reply to Thread
Results 1 to 4 of 4

loop question

Hybrid View

GBoy loop question 03-11-2007, 08:05 PM
mudraker Add these commands after your... 03-11-2007, 08:46 PM
GBoy I like that, very clever! ... 03-12-2007, 04:04 PM
mudraker GBoy If you are refering... 03-12-2007, 06:43 PM
  1. #1
    Registered User
    Join Date
    03-02-2007
    Posts
    21

    loop question

    Hi all,

    Ok I am trying to establish a loop whereby after every 6th count a variable is changed.

    Therefore:-
    if count = 1 to 6 then yval = 20
    if count = 7 to 12 then yval = 160
    if count = 13 to 18 then yval = 300

    ...carries on adding 140 to yval with each count range of 6.

    The count is not fixed and continues until it reaches a blank cell.

    I thought about dividing count by 6 and rounding down to get whole numbers that I could multiply yval by each time but there is no easy way to round down in vb that i can see.

    Any ideas would be appreciated.

    Thanks,
    Gboy

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Add these commands after your counter is increased

    If (counter Mod 6) = 1 And counter > 1 Then
    yval = yval + 140
    End If
    It sounds like you are intending to use a Do type loop

    Here is a way to loop using a For type loop where you get the end row number before you start the loop

    Sub ddddd()
    Dim counter As Integer
    Dim yval As Integer
    Dim iLastRow As Integer
    
    yval = 20
    iLastRow = Cells(Rows.Count, "a").End(xlUp).Row
    
    For counter = 1 To iLastRow Step 1
    
    If (counter Mod 6) = 1 And counter > 1 Then
    yval = yval + 140
    End If
    
    Next counter
    
    For an explanation of why I recomend For loops over Do loops see my last 2 messages at
    http://www.excelforum.com/showthread.php?t=591910&page=2
    End Sub
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  3. #3
    Registered User
    Join Date
    03-02-2007
    Posts
    21
    I like that, very clever!

    Thanks for your help.

  4. #4
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    GBoy

    If you are refering to (counter Mod 6) as being very clever I thought so to when I seen it posted in another thread a couple of days ago.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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