Results 1 to 3 of 3

Thread: [RESOLVED] Loop problem

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    393

    Resolved [RESOLVED] Loop problem

    Hello experts
    I need to the following algorithm:

    List of parts some prefixed ND/SD and others not prefixed ND/SD. For only 1 of each part (the same part can occur muultiple times sheet 1 "start with") i need to create 4 levels for parts not prefixed ND/SD and 2 levels for parts prefixed ND/SD for a standard partern depending on the starting.

    Please see the start with and what I need to create (end with) programmatically. Please see the code so far for the small program and attached spreadsheet - please view the start with (data we need to manipulate) and end with sheet (what it needs to look like). I don't have anyything on levels within the code but this needs to be included.



    VB Code:
    1. Sub fullstructure()
    2. Dim i As Long
    3. Dim usedrange As Long
    4. Dim rngdata As Range
    5. Dim counter As Long
    6.  
    7. With Worksheets(1)
    8. usedrange = .usedrange.Rows.Count
    9. Set rngdata = .Range(.Cells(1, 1), .Cells(usedrange, 1))
    10. End With
    11.  
    12. counter = 1
    13. With rngdata
    14. For i = 1 To .Rows.Count
    15. If .Cells(i, 1) <> Cells(i + 1, 1) Then
    16. 'If Left(i, 2) <> "ND" Or _
    17. 'Left(i, 2) <> "SD" Then
    18. Worksheets(2).Cells(counter, 1) = "CN-" & Cells(i, 1)
    19. 'Worksheets(2).Cells(counter + 1, 1) = "KIT-CN-" & Cells(i, 1)
    20. 'Worksheets(2).Cells(counter + 2, 1) = "IK-" & Cells(i, 1)
    21. 'Worksheets(2).Cells(counter + 3, 1) = "EBS-" & Cells(i, 1)
    22. counter = counter + 1
    23. 'Else
    24. 'Worksheets(2).Cells(counter, 1) = "CN-" & Cells(i, 1)
    25. 'Worksheets(2).Cells(counter + 1, 1) = Cells(i, 1)
    26. 'counter = counter + 1
    27. 'End If
    28. End If
    29. Next i
    30. End With
    31.  
    32. finish:
    33. End Sub

    Borris.
    Attached Files Attached Files

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Loop problem

    VB Code:
    1. Sub fullstructure()
    2. Dim i As Long
    3. Dim usedrange As Long
    4. Dim rngdata As Range
    5. Dim counter As Long, thiscell As String
    6.  
    7. With Worksheets(1)
    8. usedrange = .usedrange.Rows.Count
    9. Set rngdata = .Range(.Cells(1, 1), .Cells(usedrange, 1))
    10. End With
    11.  
    12. counter = 2
    13. Worksheets(1).Activate
    14. With rngdata
    15.     For i = 1 To .Rows.Count
    16.         If .Cells(i, 1) <> .Cells(i + 1, 1) Then
    17.             thiscell = .Cells(i, 1)
    18.             If Left(thiscell, 2) = "ND" Or Left(thiscell, 2) = "SD" Then
    19.                
    20.                  Worksheets(2).Cells(counter, 2) = "CN-" & Cells(i, 1)
    21.                  Worksheets(2).Cells(counter, 1) = 1
    22.                 Worksheets(2).Cells(counter + 1, 2) = Cells(i, 1)
    23.                 Worksheets(2).Cells(counter + 1, 1) = 2
    24.                 counter = counter + 2
    25.                
    26.                 Else
    27.                 Worksheets(2).Cells(counter, 2) = "CN-" & Cells(i, 1)
    28.                 Worksheets(2).Cells(counter, 1) = 1
    29.                 Worksheets(2).Cells(counter + 1, 2) = "KIT-CN-" & Cells(i, 1)
    30.                 Worksheets(2).Cells(counter + 1, 1) = 2
    31.                 Worksheets(2).Cells(counter + 2, 2) = "IK-" & Cells(i, 1)
    32.                 Worksheets(2).Cells(counter + 2, 1) = 3
    33.                 Worksheets(2).Cells(counter + 3, 2) = "EBS-" & Cells(i, 1)
    34.                 Worksheets(2).Cells(counter + 3, 1) = 4
    35.                 counter = counter + 4
    36.             End If
    37.         End If
    38. '        counter = counter + 1
    39.     Next i
    40. End With
    41.  
    42. finish:
    43. End Sub

    i have made a few changes to your code, i think this is what you are asking for

    pete

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    393

    Re: Loop problem

    Thanks pete , your a legend on excel visual basic!

Posting Permissions

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



Click Here to Expand Forum to Full Width