Solved

Loop within a loop within a loop......

Posted on 2014-03-01
4
258 Views
Last Modified: 2014-03-01
Hi,
I have tried doing loops within loops but it's driving me nuts (or loopy!)

basically I have a sheet that gets copied each week from a server. The data I do sumifs on, is only in certain columns, but I need to change any quantities of bad pieces to 0 so as not to count them.

I have loops that do the job but it's very messy and I have been trying for some time without success to nest the loops.

The code I have that works is
        lastrow = WS1.Range("A1048576").End(xlUp).Row

        For a = lastrow To 3 Step -1
            If WS1.Range("G" & a).Value = "BAD PIECE" Then
                WS1.Range("G" & a).Offset(0, 1) = 0
            End If
        Next a
        
        For b = lastrow To 3 Step -1
            If WS1.Range("N" & b).Value = "BAD PIECE" Then
                WS1.Range("N" & b).Offset(0, 1) = 0
            End If
        Next b
        
        For c = lastrow To 3 Step -1
            If WS1.Range("U" & c).Value = "BAD PIECE" Then
                WS1.Range("U" & c).Offset(0, 1) = 0
            End If
        Next c
        
        For d = lastrow To 3 Step -1
            If WS1.Range("AB" & d).Value = "BAD PIECE" Then
                WS1.Range("AB" & d).Offset(0, 1) = 0
            End If
        Next d
        
        For e = lastrow To 3 Step -1
            If WS1.Range("AI" & e).Value = "BAD PIECE" Then
                WS1.Range("AI" & e).Offset(0, 1) = 0
            End If
        Next e
       
        For f = lastrow To 3 Step -1
            If WS1.Range("AP" & f).Value = "BAD PIECE" Then
                WS1.Range("AP" & f).Offset(0, 1) = 0
            End If
        Next f
        
        For g = lastrow To 3 Step -1
            If WS1.Range("AW" & g).Value = "BAD PIECE" Then
                WS1.Range("AW" & g).Offset(0, 1) = 0
            End If
        Next g
        
        For h = lastrow To 3 Step -1
            If WS1.Range("BD" & h).Value = "BAD PIECE" Then
                WS1.Range("BD" & h).Offset(0, 1) = 0
            End If
        Next h
        
        For i = lastrow To 3 Step -1
            If WS1.Range("BK" & i).Value = "BAD PIECE" Then
                WS1.Range("BK" & i).Offset(0, 1) = 0
            End If
        Next i
        
        For j = lastrow To 3 Step -1
            If WS1.Range("BR" & j).Value = "BAD PIECE" Then
                WS1.Range("BR" & j).Offset(0, 1) = 0
            End If
        Next j

Open in new window

but as I say....messy.

Any help as always is much appreciated
0
Comment
Question by:Stephen Byrom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 18

Assisted Solution

by:Jerry Miller
Jerry Miller earned 50 total points
ID: 39897285
This is a little cleaner and you only have one FOR loop.

lastrow = WS1.Range("A1048576").End(xlUp).Row

        For a = lastrow To 3 Step -1
     If WS1.Range("G" & a).Value = "BAD PIECE" Then
                WS1.Range("G" & a).Offset(0, 1) = 0
           
       ElseIf WS1.Range("N" & a).Value = "BAD PIECE" Then
                WS1.Range("N" & a).Offset(0, 1) = 0
           
      ElseIf WS1.Range("U" & a).Value = "BAD PIECE" Then
                WS1.Range("U" & a).Offset(0, 1) = 0
     
        ElseIf WS1.Range("AB" & a).Value = "BAD PIECE" Then
                WS1.Range("AB" & a).Offset(0, 1) = 0
         
       ElseIf WS1.Range("AI" & a).Value = "BAD PIECE" Then
                WS1.Range("AI" & a).Offset(0, 1) = 0
         
       ElseIf WS1.Range("AP" & a).Value = "BAD PIECE" Then
                WS1.Range("AP" & a).Offset(0, 1) = 0
           
         ElseIf WS1.Range("AW" & a).Value = "BAD PIECE" Then
                WS1.Range("AW" & a).Offset(0, 1) = 0
         
        ElseIf WS1.Range("BD" & a).Value = "BAD PIECE" Then
                WS1.Range("BD" & a).Offset(0, 1) = 0
           
        ElseIf WS1.Range("BK" & a).Value = "BAD PIECE" Then
                WS1.Range("BK" & a).Offset(0, 1) = 0
           
       ElseIf WS1.Range("BR" & a).Value = "BAD PIECE" Then
                WS1.Range("BR" & a).Offset(0, 1) = 0
     Next a
0
 
LVL 33

Accepted Solution

by:
Norie earned 350 total points
ID: 39897325
Try this.
For I = 3 To LastRow
    For J = 7 To 70 Step 7
        If WS1.Cells(I, J).Value = "BAD PIECE" Then
                WS1.Cells(I,J+1).Value =  0
        End If
    Next J
Next I

Open in new window

0
 
LVL 19

Assisted Solution

by:regmigrant
regmigrant earned 100 total points
ID: 39897376
Range("G3:BR"&lastrow).Select
    Cells.Replace What:="Bad Piece", Replacement:="0", LookAt:=xlWhole, SearchOrder _
        :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
0
 
LVL 1

Author Closing Comment

by:Stephen Byrom
ID: 39897425
Thanks for the answers people.
They all work in their own way, but the lion's share had to go to imnorie's great loop routine.
Thanks again
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel If Statement 3 26
Excel IF statement 4 21
Modify to work without control shift enter 9 33
Need take combination of numbers out of the list 6 14
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question