+ Reply to Thread
Results 1 to 4 of 4

How to set a loop around a script?

Hybrid View

Catotjuh91 How to set a loop around a... 05-20-2015, 11:29 AM
nigelbloomy Re: How to set a loop around... 05-20-2015, 11:37 AM
Catotjuh91 Re: How to set a loop around... 05-20-2015, 12:08 PM
dipique Re: How to set a loop around... 05-20-2015, 12:21 PM
  1. #1
    Registered User
    Join Date
    05-20-2015
    Location
    Eindhoven, the Netherlands
    MS-Off Ver
    2013
    Posts
    2

    How to set a loop around a script?

    I have made the VBA script below.
    With the script I can do every row by hand.
    But I have around 5,000 rows, so I want to try to make a loop around it or something like that.
    But I don't know how.
    Can anybody help me?
    Thank you.

    Sub SetGrades()
    
        Dim score As Integer
        
        score = ActiveCell.Value
        
        Select Case score
        
            Case 0 To 50.3778337531486
                ActiveCell(1, 2).Value = "TI"
                ActiveCell(1, 2).HorizontalAlignment = xlCenter
                ActiveCell(1, 2).Interior.Color = RGB(0, 255, 0)
                ActiveCell(1, 3).Value = "No"
                ActiveCell(1, 3).HorizontalAlignment = xlCenter
                           
            Case 50.3778337531487 To 176.32241813602
                ActiveCell(1, 2).Value = "ARI"
                ActiveCell(1, 2).HorizontalAlignment = xlCenter
                ActiveCell(1, 2).Interior.Color = RGB(255, 0, 0)
                ActiveCell(1, 3).Value = "No"
                ActiveCell(1, 3).HorizontalAlignment = xlCenter
    
            Case 176.32241813603 To 251.889168765743
                ActiveCell(1, 2).Value = "PRI"
                ActiveCell(1, 2).HorizontalAlignment = xlCenter
                ActiveCell(1, 2).Interior.Color = RGB(255, 255, 0)
                ActiveCell(1, 3).Value = "No"
                ActiveCell(1, 3).HorizontalAlignment = xlCenter
    
           Case 251.889168765744 To 302.267002518892
                ActiveCell(1, 2).Value = "TI unknown"
                ActiveCell(1, 2).HorizontalAlignment = xlCenter
                ActiveCell(1, 2).Interior.Color = RGB(0, 255, 0)
                ActiveCell(1, 3).Value = "No"
                ActiveCell(1, 3).HorizontalAlignment = xlCenter
    
            Case 302.267002518892 To 428.211586901763
                ActiveCell(1, 2).Value = "ARI unknown"
                ActiveCell(1, 2).HorizontalAlignment = xlCenter
               ActiveCell(1, 2).Interior.Color = RGB(255, 0, 0)
                ActiveCell(1, 3).Value = "No"
                ActiveCell(1, 3).HorizontalAlignment = xlCenter
    
            Case 428.211586901764 To 503.778337531486
                ActiveCell(1, 2).Value = "PRI unknown"
                ActiveCell(1, 2).HorizontalAlignment = xlCenter
                ActiveCell(1, 2).Interior.Color = RGB(255, 255, 0)
                ActiveCell(1, 3).Value = "No"
                ActiveCell(1, 3).HorizontalAlignment = xlCenter
            
            Case 503.778337531487 To 508.816120906801
                ActiveCell(1, 2).Value = "TI emergency"
                ActiveCell(1, 2).HorizontalAlignment = xlCenter
                ActiveCell(1, 2).Interior.Color = RGB(0, 255, 0)
                ActiveCell(1, 3).Value = "Yes"
                ActiveCell(1, 3).HorizontalAlignment = xlCenter
                
            Case 508.816120906802 To 518.891687657431
                ActiveCell(1, 2).Value = "ARI emergency"
                ActiveCell(1, 2).HorizontalAlignment = xlCenter
                ActiveCell(1, 2).Interior.Color = RGB(255, 0, 0)
                ActiveCell(1, 3).Value = "Yes"
                ActiveCell(1, 3).HorizontalAlignment = xlCenter
                         
            Case 518.891687657432 To 528.96725440806
                ActiveCell(1, 2).Value = "PRI emergency"
                ActiveCell(1, 2).HorizontalAlignment = xlCenter
                ActiveCell(1, 2).Interior.Color = RGB(255, 255, 0)
                ActiveCell(1, 3).Value = "Yes"
                ActiveCell(1, 3).HorizontalAlignment = xlCenter
                
            Case 528.96725440807 To 609.571788413098
                ActiveCell(1, 2).Value = "SK"
                ActiveCell(1, 2).HorizontalAlignment = xlCenter
                ActiveCell(1, 2).Interior.Color = RGB(0, 0, 255)
                ActiveCell(1, 3).Value = "No"
                ActiveCell(1, 3).HorizontalAlignment = xlCenter
            
            Case 609.571788413099 To 739.478589420655
                ActiveCell(1, 2).Value = "FE"
                ActiveCell(1, 2).HorizontalAlignment = xlCenter
                ActiveCell(1, 2).Interior.Color = RGB(0, 255, 255)
                ActiveCell(1, 3).Value = "No"
                ActiveCell(1, 3).HorizontalAlignment = xlCenter
                
            Case 739.478589420656 To 881.612090680101
                ActiveCell(1, 2).Value = "PRF"
                ActiveCell(1, 2).HorizontalAlignment = xlCenter
                ActiveCell(1, 2).Interior.Color = RGB(255, 0, 255)
                ActiveCell(1, 3).Value = "No"
                ActiveCell(1, 3).HorizontalAlignment = xlCenter
                
            Case 881.612090680102 To 982.367758186398
                ActiveCell(1, 2).Value = "FRD"
                ActiveCell(1, 2).HorizontalAlignment = xlCenter
                ActiveCell(1, 2).Interior.Color = RGB(255, 153, 0)
                ActiveCell(1, 3).Value = "No"
                ActiveCell(1, 3).HorizontalAlignment = xlCenter
                
            Case 982.367758186399 To 984.886649874055
                ActiveCell(1, 2).Value = "SK emergency"
                ActiveCell(1, 2).HorizontalAlignment = xlCenter
                ActiveCell(1, 2).Interior.Color = RGB(0, 0, 255)
                ActiveCell(1, 3).Value = "Yes"
                ActiveCell(1, 3).HorizontalAlignment = xlCenter
            
            Case 984.886649874056 To 989.92443324937
                ActiveCell(1, 2).Value = "FE emergency"
                ActiveCell(1, 2).HorizontalAlignment = xlCenter
                ActiveCell(1, 2).Interior.Color = RGB(0, 255, 255)
                ActiveCell(1, 3).Value = "Yes"
                ActiveCell(1, 3).HorizontalAlignment = xlCenter
                
            Case 989.92443324938 To 994.962216624685
                ActiveCell(1, 2).Value = "PRF emergency"
                ActiveCell(1, 2).HorizontalAlignment = xlCenter
                ActiveCell(1, 2).Interior.Color = RGB(255, 0, 255)
                ActiveCell(1, 3).Value = "Yes"
                ActiveCell(1, 3).HorizontalAlignment = xlCenter
                
            Case 994.962216624686 To 1000
                ActiveCell(1, 2).Value = "FRD emergency"
                ActiveCell(1, 2).HorizontalAlignment = xlCenter
                ActiveCell(1, 2).Interios.Color = RGB(255, 153, 0)
                ActiveCell(1, 3).Value = "Yes"
                ActiveCell(1, 3).HorizontalAlignment = xlCenter
            
            Case Else
                MsgBox "No score entered"
             
       End Select
       
    End Sub
    Moderator's note: Please take the time to review our rules. There aren't many, and they are all important. Rule #3 requires code tags. I have added them for you this time because you are a new member. --6StringJazzer
    Last edited by 6StringJazzer; 05-20-2015 at 11:34 AM.

  2. #2
    Valued Forum Contributor nigelbloomy's Avatar
    Join Date
    11-06-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    1,011

    Re: How to set a loop around a script?

    You can put your code inbetween the for each and next cell lines

    Dim rng As Range
    Dim Cell As Range
    
    Set rng = ActiveSheet.Range("A1:A500")
    For Each Cell In rng
    
    Next Cell
    You could also do this if you want to select all of the rows first.

    Dim rng As Range
    Dim Cell As Range
    
    Set rng = Selection
    For Each Cell In rng
    
    Next Cell
    Last edited by nigelbloomy; 05-20-2015 at 11:39 AM.

  3. #3
    Registered User
    Join Date
    05-20-2015
    Location
    Eindhoven, the Netherlands
    MS-Off Ver
    2013
    Posts
    2

    Re: How to set a loop around a script?

    Thank you for your responds.
    Unfortunately is isn't working.

  4. #4
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Dallas, TX
    MS-Off Ver
    Office 2013
    Posts
    107

    Re: How to set a loop around a script?

    I think I see what's going on. Your code isn't looping through every cell to perform an action (like Nigel's example does), it's looping through every row and performing actions on that row. Without knowing exactly how your sheet is set up, I think this will work:

    Dim FirstScoreRow as Integer
    Dim LastScoreRow as Integer
    FirstScoreRow = 2         'You'll need to adjust these to the correct row numbers
    LastScoreRow = 5000
    
    Dim score As Integer
    Dim x as Integer
    For x = FirstScoreRow to LastScoreRow
        score = Cells(x,1) 'gets the score from the first column of this row    
        Select Case score
        
            Case 0 To 50.3778337531486
                Cell.Value = "TI"
                Cells(x, 2).HorizontalAlignment = xlCenter
                Cells(x, 2).Interior.Color = RGB(0, 255, 0)
                Cells(x, 3).Value = "No"
                Cells(x, 3).HorizontalAlignment = xlCenter
    
            '...The rest of your cases...
    
            Case Else
                MsgBox "No score entered"
             
       End Select
    Next x

+ 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. Need help with VBA script, trying to loop until end
    By ben1990 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-11-2013, 08:34 AM
  2. [SOLVED] How do I reset a for next loop in the script?
    By BrianATrease in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-18-2012, 12:22 PM
  3. VBA script for running Solver in loop
    By confusedguy in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-31-2011, 03:54 AM
  4. Charts in VBA script need to align per loop
    By 4dplane in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-24-2011, 06:37 AM
  5. Macro to loop a script
    By JFN in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-27-2010, 02:14 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