Consulting

Results 1 to 7 of 7

Thread: Can Someone help with this For Loop?

  1. #1

    Can Someone help with this For Loop?

    I keep getting the error: "Next without For" where I have highlighted in Yellow.

    I am trying to check the first cell of each row in the "Raw Data" sheet and if it is "Power" then write that entire row to the first available row in "Layout" sheet.

    This might not be the best way, but I just want to know what is wrong with my For/Next couples?

    [vba]Sub WriteEquipmentType()
    Dim i, k, h As Integer
    For i = 1 To 1000
    If Worksheets("Raw Data").Cells(i, 1) = "Power" Then

    For k = 1 To 1000
    For h = 1 To 20
    Worksheets("Layout").Cells(k, h) = _
    Worksheets("Raw Data").Cells(i, h)
    Next h
    Next k

    Next i
    End Sub[/vba]

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    End If missing:[vba]Sub WriteEquipmentType2()
    Dim i, k, h As Integer
    For i = 1 To 1000
    If Worksheets("Raw Data").Cells(i, 1) = "Power" Then
    For k = 1 To 1000
    For h = 1 To 20
    Worksheets("Layout").Cells(k, h) = Worksheets("Raw Data").Cells(i, h)
    Next h
    Next k
    End If
    Next i
    End Sub [/vba](I can't highlight the End If in the code above without the www.thecodenet.com VBA tags going wrong)
    Last edited by p45cal; 08-25-2009 at 10:15 AM.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    Hi p45scal! Thanks! Now I have a new problem :/ I am in an endless loop now.

    It just keeps rewriting the same row over again.

    I have attached a sample file.

    [VBA]Sub WriteLayouts()
    Worksheets("Layouts").Cells.Clear
    Dim i, k, h As Integer
    For i = 1 To 1000
    If Worksheets("Raw Data").Cells(i, 1) = "Power" Then

    For k = 1 To 1000
    For h = 1 To 20
    Worksheets("Layouts").Cells(k, h) = _
    Worksheets("Raw Data").Cells(i, h)
    Next h
    Next k
    End If
    Next i

    End Sub[/VBA]

  4. #4
    Update: If you go to the "Layouts" worksheet and then run the Macro by Alt+F8, you can actually see what it is really doing.

    It is not an endless loop.

  5. #5
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    It's not a never ending loop, just a very long one; you have 62 rows with 'Power' in the first column, and for each of those you have 1000 * 20 iterations which makes for about 1.25 million copy instructions.
    Is this what you're looking for?[vba]Sub WriteLayouts()
    Worksheets("Layouts").Cells.Clear
    Dim i, k, h As Integer
    k = 1
    For i = 1 To 1000
    If Worksheets("Raw Data").Cells(i, 1) = "Power" Then
    For h = 1 To 20
    Worksheets("Layouts").Cells(k, h) = _
    Worksheets("Raw Data").Cells(i, h)
    Next h
    k = k + 1
    End If
    Next i
    End Sub[/vba]
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  6. #6
    Yes. Except that I don't even want the word "Power" to be transfered over. Just the Values. So do I just need to add an "h+1" in the cells argument? Let's see now...

    Thanks for your help p45scal!!

  7. #7
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    [vba]For h = 1 To 20
    Worksheets("Layouts").Cells(k, h) = _
    Worksheets("Raw Data").Cells(i, h)
    Next h [/vba]becomes either[vba]For h = 2 To 20
    Worksheets("Layouts").Cells(k, h) = _
    Worksheets("Raw Data").Cells(i, h)
    Next h [/vba]or[vba]For h = 2 To 20
    Worksheets("Layouts").Cells(k, h-1) = _
    Worksheets("Raw Data").Cells(i, h)
    Next h[/vba]depending on which result you prefer.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

Posting Permissions

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