+ Reply to Thread
Results 1 to 11 of 11

Loop for repetitive code

Hybrid View

  1. #1
    Registered User
    Join Date
    02-13-2010
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    70

    Question Loop for repetitive code

    Hi i have the following code for a large number of cells in a column, the code is so huge that i have hit the 'procedure too big' barrier so please take a look at the code below

    
    If (Range("C39").Value < 1) and (Range("C8").Value >= Range("C9").Value) Then
    Range("C9").Select
    Res_Dark_A
    Range("C39").Value = 2
    Range("C65").Value = Range("B9").Value
    Range("C66").Value = Range("C8").Value
    Range("C67").Value = Range("C19").Value
    Range("C68").Value = Range("D9").Value
    
    End If
    
    If (Range("C40").Value < 1) and (Range("C8").Value >= Range("C10").Value) Then
    Range("C10").Select
    Res_Light_B
    Range("C40").Value = 2
    Range("C65").Value = Range("B10").Value
    Range("C66").Value = Range("C8").Value
    Range("C67").Value = Range("C19").Value
    Range("C68").Value = Range("D10").Value
    
    End If
    
    If (Range("C41").Value < 1) and (Range("C8").Value >= Range("C11").Value) Then
    Range("C11").Select
    Res_Dark_A
    Range("C41").Value = 2
    Range("C65").Value = Range("B11").Value
    Range("C66").Value = Range("C8").Value
    Range("C67").Value = Range("C19").Value
    Range("C68").Value = Range("D11").Value
    
    End If
    
    If (Range("C42").Value < 1) and (Range("C8").Value >= Range("C12").Value) Then
    Range("C12").Select
    Res_Light_B
    Range("C42").Value = 2
    Range("C65").Value = Range("B12").Value
    Range("C66").Value = Range("C8").Value
    Range("C67").Value = Range("C19").Value
    Range("C68").Value = Range("D12").Value
    
    End If
    
    If (Range("C43").Value < 1) and (Range("C8").Value >= Range("C13").Value) Then
    Range("C13").Select
    Res_Dark_A
    Range("C43").Value = 2
    Range("C65").Value = Range("B13").Value
    Range("C66").Value = Range("C8").Value
    Range("C67").Value = Range("C19").Value
    Range("C68").Value = Range("D13").Value
    
    End If
    
    If (Range("C44").Value < 1) and (Range("C8").Value >= Range("C14").Value) Then
    Range("C14").Select
    Res_Light_B
    Range("C44").Value = 2
    Range("C65").Value = Range("B14").Value
    Range("C66").Value = Range("C8").Value
    Range("C67").Value = Range("C19").Value
    Range("C68").Value = Range("D14").Value
    
    End If
    
    If (Range("C45").Value < 1) and (Range("C8").Value >= Range("C15").Value) Then
    Range("C15").Select
    Res_Dark_A
    Range("C45").Value = 2
    Range("C65").Value = Range("B15").Value
    Range("C66").Value = Range("C8").Value
    Range("C67").Value = Range("C19").Value
    Range("C68").Value = Range("D15").Value
    
    End If
    
    If (Range("C46").Value < 1) and (Range("C8").Value >= Range("C16").Value) Then
    Range("C16").Select
    Res_Light_B
    Range("C46").Value = 2
    Range("C65").Value = Range("B16").Value
    Range("C66").Value = Range("C8").Value
    Range("C67").Value = Range("C19").Value
    Range("C68").Value = Range("D16").Value
    
    End If
    
    If (Range("C47").Value < 1) And (Range("C8").Value >= Range("C17").Value) THEN
    Range("C17").Select
    Res_Dark_A
    Range("C47").Value = 2
    Range("C65").Value = Range("B17").Value
    Range("C66").Value = Range("C8").Value
    Range("C67").Value = Range("C19").Value
    Range("C68").Value = Range("D17").Value
    
    End If
    This is just one 1/40 th part of the code and i hope you can imagine the hardship that i have to go thru to make this code work .. so I hope some kind of LOOP solution will be viable .As you can see that cells are progressing and not random which might make the job easier

  2. #2
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127

    Re: Loop for repetitive code

    It may help to post a small sample of your workbook and detail your expected results.

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Loop for repetitive code

    Hello JackandJill,

    You don't say much about what this macro is supposed to do. It would be easier to propose a solution if you gave more details about what you want to accomplish. My initial thought is the workbook layout could benefit from better design, but until you post the workbook that too is an unknown.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Registered User
    Join Date
    02-13-2010
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    70

    Re: Loop for repetitive code

     
    Sub worksheet_calculate()
    If (Range("B4").Value < 2) Then
    
    
    
    Here the above code is used
    End If End Sub
    ===
    Bascially it will update when ever there is a change in Cell C 9 to C17 while condition is true with constant in C8.
    Here I am simply asking to help in writing Code as a loop as there are continues increment from 9 to 17(Column B, C, D) and another continuation number from 39 to 47 (for Column C)
    ..
    The change will happen each time there is justification of value one after another

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Loop for repetitive code

    Hello JackandJill,

    Since you are doing this formatting during the Worksheet_Calculate event, I have to ask if you have considered Conditional Formatting.

  6. #6
    Registered User
    Join Date
    02-13-2010
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    70

    Re: Loop for repetitive code

    Hi,
    Thanks for your quick reply,
    I want to do in vba as I need to take these changes for other updates in the process after this.
    so please help me to write in loop,

    I just started trying my self but not working actually

    here it is my trail and failed one

    'Dim acolumn As Long, anum As Long
    'Dim bcolumn As Long, bnum As Long
    'Dim ccolumn As Long, cnum As Long
    '
    'For bcolumn = 17 To 9 Step 1
    'For acolumn = 47 To 39 Step 1
    ''For ccolumn = 47 To 39 Step 1
    'If (Range("C" & acolumn).Value < 1) And (Range("C8").Value >= Range("C" & bcolumn).Value) Then
    'Range("C" & bcolumn).Select
    'Res_Dark_A
    'For ccolumn = 47 To 39
    'Range("C" & ccolumn).Value = 2
    'Next ccolumn
    'Range("C65").Value = Range("B" & bcolumn).Value
    'Range("C66").Value = Range("C" & bcolumn).Value
    'Range("C67").Value = Range("C19").Value
    'Range("C68").Value = Range("D" & bcolumn).Value
    ''NZDUSD_Levels_1
    'End If
    'anum = anum - 1
    'bnum = bnum - 1
    'Next acolumn
    'Next bcolumn
    in this there may be more lines not needed in for loop and place where to place the loop is not clear to me ..
    Please help me in making working loop
    Thanks,

  7. #7
    Registered User
    Join Date
    02-13-2010
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    70

    Re: Loop for repetitive code

    Is this post going to be answered or should I close?

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Loop for repetitive code

    For bcolumn = 17 To 9 Step 1
    For acolumn = 47 To 39 Step 1
    For ccolumn = 47 To 39 Step 1
    Have you ever consulted the VBEditor's help function or some VBA book ? For instance to study the properties of For...Next with decremental values.



  9. #9
    Registered User
    Join Date
    02-13-2010
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    70

    Re: Loop for repetitive code

    Hi,
    Thanks for suggestion, I was not able to understand even after I gone through it that's how I am expecting help from you all pros there, hope you can help me to resovle the issue.
    ..
    Please tell me how I can build this into loop, to make less lines of the code.
    .
    However I am also trying my self but unsucessfull till now.
    in fact I have chnaged code as below and tried
    For bcolumn = 17 To 9 Step -1
    For acolumn = 47 To 39 Step -1
    For ccolumn = 47 To 39 Step -1
    Still unsuccessful.
    ...
    It could be very simple to you but for me it is a big resolution to get, please help.
    Thanks,

  10. #10
    Registered User
    Join Date
    02-13-2010
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    70

    Re: Loop for repetitive code

    Hi,
    Please help me in builiding the code, much appreciated your help.
    Thanks,

  11. #11
    Registered User
    Join Date
    02-13-2010
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    70

    Re: Loop for repetitive code

    Is there any body could help or should I close the Thread .. pl advice

+ 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