+ Reply to Thread
Results 1 to 7 of 7

Loop until won't stop!?

Hybrid View

  1. #1
    Registered User
    Join Date
    06-19-2007
    Posts
    47

    Loop until won't stop!?

    I've fixed my previous problem, and this formula works except for one thing!

    it doesnt stop at the last empty cell it continues to create a new sheet but is unable to name it as ther is nothing in the cell from whihc it takes its name.

    why? what have i missed?

    thanks
     Sub Macro1()
    '
    ' Macro1 Macro
    ' Macro recorded 26/07/2007 by Michael Skinner
    '
    Application.ScreenUpdating = False
    Sheets("export").Select
    Range("Ar2").Select
    
    Do Until IsEmpty(ActiveCell)
    
    For n = 1 To 100
    Dim sSheetName As String
    sSheetName = (ActiveCell)
    
        Sheets("Template").Select
        Sheets("Template").Copy After:=Sheets(1)
        
        Sheets("Template (2)").Select
        Sheets("Template (2)").Name = sSheetName
        
        Sheets("export").Select
        Range("A1").Select
        ActiveCell.Offset(n, 0).Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets(sSheetName).Select
        Range("E2:AA2").Select
        ActiveSheet.Paste
        
       
        Sheets("export").Select
        Range("b1").Select
        ActiveCell.Offset(n, 0).Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets(sSheetName).Select
        Range("h10").Select
        ActiveSheet.Paste
        
        Sheets("export").Select
        Range("c1").Select
        ActiveCell.Offset(n, 0).Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets(sSheetName).Select
        Range("H11").Select
        ActiveSheet.Paste
        
        Sheets("export").Select
        Range("d1").Select
        ActiveCell.Offset(n, 0).Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets(sSheetName).Select
        Range("E6").Select
        ActiveSheet.Paste
    
        Sheets("export").Select
        Range("at1").Select
        ActiveCell.Offset(n, 0).Select
        Application.CutCopyMode = False
        Selection.Copy
        Sheets(sSheetName).Select
        Range("e38").Select
        ActiveSheet.Paste
        
    Sheets("export").Select
    Range("Ar2").Select
    ActiveCell.Offset(n, 0).Select
    
    Next n
    
          
    Loop
    End Sub
    Last edited by VBA Noob; 07-26-2007 at 02:17 PM.

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525
    Try changing your counter
    This bit of code will count the number of rows in column b, therefore not requiring to say 1 to 100

    For i = 1 To Cells(Rows.Count, 2).End(xlUp).Row 'the 2 means column 2
    'your code
    next i

  3. #3
    Registered User
    Join Date
    06-19-2007
    Posts
    47
    Tried this but it still goes past the loop and creates a new sheet which it can't name.

    what does the end(x1up) do?

  4. #4
    Registered User
    Join Date
    06-19-2007
    Posts
    47
    the value of x1up when it finishes is -4162 but i only have 2 row in this test example? should it not be 65536 less 2?

    i have tried clearing the cells below my data but it didnt help?

    thanks

  5. #5
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628
    The instruction
    Cells(Rows.Count, 2).End(xlUp).Row 'the 2 means column 2
    do the same thing than go to max row of column 2 (for example B65536) then press 'End' and 'Up' to go to the last row of column 2.

    To fix your problem try to do so:
    lastRow = Sheets("export").Cells(Rows.Count, 2).End(xlUp).Row 'the 2 means column 2
    
    For i = 1 To lastRow
       'your code
    next i
    Regards,
    Antonio

  6. #6
    Registered User
    Join Date
    06-19-2007
    Posts
    47
    thanks for the help.

    i sorted it buy just using

    n=1

    my coded

    n=n+1
    end sub

    thanks for all the help its mcuh appreciated

    any thoughts on the deleting sheets questions? (see post)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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