+ Reply to Thread
Results 1 to 2 of 2

Debug: For,next does not start at specified row

Hybrid View

  1. #1
    Registered User
    Join Date
    05-27-2013
    Location
    Middle, Nowhere
    MS-Off Ver
    Excel 2003
    Posts
    13

    Debug: For,next does not start at specified row

    I have the following code that when run starts at row 1 instead of 84. For the life of me I cannot figure out why this is so.

    Sub modulenter()
    
    Dim appWD As Word.Application
        Dim Name As String
        Set appWD = CreateObject("Word.Application")
        appWD.Visible = False
    
    
        Sheets("Sheet1 (3)").Select
        lastrow = Range("F9999").End(xlUp).Row
        For i = 84 To lastrow
            j = Sheets("MasterList").Cells(Rows.Count, 4).End(xlUp).Row
            Sheets("MasterList").Cells(j + 1, 1).Value = Sheets("Sheet1 (3)").Cells(i, 3).Value
            Sheets("MasterList").Cells(j + 1, 2).Value = Sheets("Sheet1 (3)").Cells(i, 4).Value
            Sheets("MasterList").Cells(j + 1, 3).Value = Sheets("Sheet1 (3)").Cells(i, 5).Value
        
        Next i
        
        
    End Sub
    The odd things is, when I run my entire macro, it starts at row 84 and stops right there. It doesn't move on to the next row. So I'm stuck with only 1 miserly entry. My full code is as follows:

    Sub modulenter()
    
    Dim appWD As Word.Application
        Dim Name As String
        Set appWD = CreateObject("Word.Application")
        appWD.Visible = False
    
    
        Sheets("Sheet1 (3)").Select
        lastrow = Range("F9999").End(xlUp).Row
        For i = 84 To lastrow
            j = Sheets("MasterList").Cells(Rows.Count, 4).End(xlUp).Row
            Sheets("MasterList").Cells(j + 1, 1).Value = Sheets("Sheet1 (3)").Cells(i, 3).Value
            Sheets("MasterList").Cells(j + 1, 2).Value = Sheets("Sheet1 (3)").Cells(i, 4).Value
            Sheets("MasterList").Cells(j + 1, 3).Value = Sheets("Sheet1 (3)").Cells(i, 5).Value
        
            Sheets("Sheet1 (3)").Select
            Cells(i, 6).Copy
            ' Tell Word to create a new document
            appWD.Documents.Add
            ' Tell Word to paste the contents of the clipboard into the new document
            appWD.Selection.Paste
            ' Save the new document with a sequential file name
            appWD.Selection.Find.Replacement.ClearFormatting
            With appWD.Selection.Find
            
                .Text = "@"
                .Replacement.Text = "^n"
                .Forward = True
                .Wrap = wdFindContinue
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
        End With
        appWD.Selection.Find.Execute Replace:=wdReplaceAll
        appWD.Selection.WholeStory
        appWD.Selection.Cut
        Sheets("Masterlist").Select
        Range("D" & j + 1).Select
        ActiveSheet.PasteSpecial _
            Format:="Text", Link:=False, DisplayAsIcon:=False
        Application.Run "deleterow"
        appWD.ActiveDocument.SaveAs Filename:="Doc_" & i
        appWD.ActiveDocument.Close
            
        
        
        Next i
        
        ' Close the Word application
        appWD.Quit
    End Sub
    Anybody have any ideas?

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,689

    Re: Debug: For,next does not start at specified row

    Well, you didn't attach a workbook, so, ...
    --------------------------------
    Notice that the line j = Sheets("MasterList").Cells(Rows.Count, 4).End(xlUp).Row is inside your For i... loop; therefore, it will be re-initialized to the number of the last used row in Sheets("MasterList") column D each time your I-loop increments.

    Say, Sheets("Sheet1 (3)"), is using 200 rows and Masterlist is using 1200 rows. Then your i loop will increment from 84 to 200, and each time, j will be initialized to 1200. Notice, also that j is never incremented! Thus the lines:

    Sheets("MasterList").Cells(j + 1, 1).Value = Sheets("Sheet1 (3)").Cells(i, 3).Value
    Sheets("MasterList").Cells(j + 1, 2).Value = Sheets("Sheet1 (3)").Cells(i, 4).Value
    Sheets("MasterList").Cells(j + 1, 3).Value = Sheets("Sheet1 (3)").Cells(i, 5).Value


    will write in Masterlist row 1201 for each increment of i. J never changes it always points at row 1201 ( in this case).
    Ben Van Johnson

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Copy variable amount of rows between Start and End to Start
    By EDo1981 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-19-2013, 07:37 AM
  2. Replies: 0
    Last Post: 05-06-2006, 04:35 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