Consulting

Results 1 to 5 of 5

Thread: for loop in vba

  1. #1

    for loop in vba

    valuea = Range("V6")
    Last = Cells(Rows.Count, 2).End(xlUp).Row
    For i = Last To 1 Step -1
    If (Cells(i, 2).Value) = Range("T6") Then
    Cells(i, "A").EntireRow.Resize(valuea, 11).Select
    Selection.Interior.ColorIndex = 0
    End If
    Next i
    Selection.Activate
    Selection.Interior.ColorIndex = 15
    
    
    valueb = Range("V7")
    Last = Cells(Rows.Count, 2).End(xlUp).Row
    For i = Last To 1 Step -1
    If (Cells(i, 2).Value) = Range("T7") Then
    Cells(i, "A").EntireRow.Resize(valueb, 11).Select
    Selection.Interior.ColorIndex = 0
    End If
    Next i
    Selection.Activate
    Selection.Interior.ColorIndex = 15
    
    
    valuec = Range("V8")
    Last = Cells(Rows.Count, 2).End(xlUp).Row
    For i = Last To 1 Step -1
    If (Cells(i, 2).Value) = Range("T8") Then
    Cells(i, "A").EntireRow.Resize(valuec, 11).Select
    Selection.Interior.ColorIndex = 0
    End If
    Next i
    Selection.Activate
    Selection.Interior.ColorIndex = 15
    please help me shorten this code.
    Last edited by Bob Phillips; 08-09-2021 at 11:30 AM. Reason: ADded code tags

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
        Last = Cells(Rows.Count, 2).End(xlUp).Row
        For i = Last To 1 Step -1
        
            If Cells(i, 2).Value = Range("T6").Value Then
            
                Cells(i, "A").EntireRow.Resize(Range("V6").Value, 11).Select
                Selection.Interior.ColorIndex = 0
            
            ElseIf Cells(i, 2).Value = Range("T7").Value Then
            
                Cells(i, "A").EntireRow.Resize(Range("V7").Value, 11).Select
                Selection.Interior.ColorIndex = 0
            ElseIf Cells(i, 2).Value = Range("T7").Value Then
            
                Cells(i, "A").EntireRow.Resize(Range("V8").Value, 11).Select
                Selection.Interior.ColorIndex = 0
            End If
        Next i
        
        Selection.Interior.ColorIndex = 15
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    sn=cells(6,20).resize(3)
    
    for each it in columns(2).specialcells(2)
      for j = 1 to 3       
        If it = sn(j,1)Then it.offset(,-1).resize(cells(5+j,22),11).interior.colorindex=0
      next
    next
    Last edited by snb; 08-09-2021 at 01:40 PM.

  4. #4
    Quote Originally Posted by Bob Phillips View Post
        Last = Cells(Rows.Count, 2).End(xlUp).Row
        For i = Last To 1 Step -1
        
            If Cells(i, 2).Value = Range("T6").Value Then
            
                Cells(i, "A").EntireRow.Resize(Range("V6").Value, 11).Select
                Selection.Interior.ColorIndex = 0
            
            ElseIf Cells(i, 2).Value = Range("T7").Value Then
            
                Cells(i, "A").EntireRow.Resize(Range("V7").Value, 11).Select
                Selection.Interior.ColorIndex = 0
            ElseIf Cells(i, 2).Value = Range("T7").Value Then
            
                Cells(i, "A").EntireRow.Resize(Range("V8").Value, 11).Select
                Selection.Interior.ColorIndex = 0
            End If
        Next i
        
        Selection.Interior.ColorIndex = 15
    thanks Bob,
    from your code coloring the rows are missed but i have just tried like this

    Sub grey()
    
    
        Last = Cells(Rows.Count, 2).End(xlUp).Row
        For i = Last To 1 Step -1
        
            If Cells(i, 2).Value = Range("T6").Value Then
            
                Cells(i, "A").EntireRow.Resize(Range("V6").Value, 11).Select
                Selection.Interior.ColorIndex = 0
            
            End If
        Next i
        
        Selection.Interior.ColorIndex = 15
        
            Last = Cells(Rows.Count, 2).End(xlUp).Row
        For i = Last To 1 Step -1
        
            If Cells(i, 2).Value = Range("T7").Value Then
            
                Cells(i, "A").EntireRow.Resize(Range("V7").Value, 11).Select
                Selection.Interior.ColorIndex = 0
            
            End If
        Next i
        
        Selection.Interior.ColorIndex = 15
        
        Last = Cells(Rows.Count, 2).End(xlUp).Row
        For i = Last To 1 Step -1
        
            If Cells(i, 2).Value = Range("T8").Value Then
            
                Cells(i, "A").EntireRow.Resize(Range("V8").Value, 11).Select
                Selection.Interior.ColorIndex = 0
                
                
                
            
            End If
        Next i
        
        Selection.Interior.ColorIndex = 15
    
    
    End Sub
    its failed because of V8 have no values but other two lines worked well
    thanks for your support.
    Last edited by Paul_Hossler; 08-09-2021 at 01:27 PM. Reason: My turn to add CODE tags

  5. #5
    For testloop=6 to 8
    
    valueabc = Range("V" & testloop)
    Last = Cells(Rows.Count, 2).End(xlUp).Row
    For i = Last To 1 Step -1
    If (Cells(i, 2).Value) = Range("T" & testloop) Then
    Cells(i, "A").EntireRow.Resize(valueabc, 11).Select
    Selection.Interior.ColorIndex = 0
    End If
    Next i
    Selection.Activate 
    Selection.Interior.ColorIndex = 15
    
    Next testloop
    Can't you use something as simple as this?
    Last edited by Mogzie; 08-15-2021 at 04:22 PM. Reason: typo

Posting Permissions

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