Results 1 to 6 of 6

Do While Nested Loops

Threaded View

  1. #1
    Forum Contributor
    Join Date
    12-09-2015
    Location
    Virginia, USA
    MS-Off Ver
    Excel 365
    Posts
    129

    Do While Nested Loops

    Hello,

    I'm struggling with a nested loop and could use some help/guidance on. I have a sheet with a random amount of data (see attached). My goal is to insert a row and concatenate some text based on the cell below its' value. The below code kind of does this portion, but it stops working when it gets to numbers for some reason (i.e. for some reason, when I run the code it is not determining the difference between "1" or "2", but it is recognizing all the other changes in values. This is basically part 2 of the nest.

    Part 1 of the nested loop requires me to ignore everything from "StartHeader" through "N" so any values in between will just pass a row count + 1. Once the macro reaches "N" it will know to automatically insert a row below "N" and concatenate "Page " and the value of the Cell immediately below this new line and then move on to the next row and check it's value against the previous row.

    Once it detects a change, it again adds a new line and concatenates "Page" and the value of the cell immediately below.

    This continues until we reach "StartHeader" where we basically don't do anything until we get back to "N".

    sub Nested_Starts
    
    Dim rw As Long
    Dim cl As Long
    Dim LastRw As Long
    rw = 2
    cl = 1
    
    
    
    Sheets("Sample2").Select
        LastRw = Application.CountA(Range("A:A"))
        
        Do While rw <= LastRw
        Cells(rw, cl).Select
        Cells(rw - 1, cl).Select
        
        
        'If Cells(rw, cl) = "StartHeader" Then
        'rw = rw + 1
        'Do Until Cells(rw, cl) = "N"
         '   Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
          '  Cells(rw + 1, cl).Value = "Section" & " " & Cells(rw + 1, cl).Value
        'End If
        'Loop
        'ActiveCell.Select
        If Cells(rw, cl) <> Cells(rw - 1, cl) Then
        Rows(rw).EntireRow.Insert
        Cells(rw, cl).Value = "Section" & " " & Cells(rw + 1, cl).Value
        End If
        
        rw = rw + 2
        
    Loop
        
    End Sub
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Help with nested loops
    By wishmaker in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-23-2013, 11:03 AM
  2. Nested Loops
    By christian2012 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-10-2013, 08:19 PM
  3. nested loops
    By short_n_curly in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-22-2012, 11:10 AM
  4. Nested Do Loops
    By ross88guy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-08-2010, 09:10 AM
  5. VBA - Nested loops
    By roheba in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-09-2010, 01:42 PM
  6. [SOLVED] Nested with loops
    By Clair in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-20-2006, 03:35 PM
  7. [SOLVED] nested loops
    By jer in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-15-2006, 05:15 PM

Tags for this Thread

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