+ Reply to Thread
Results 1 to 7 of 7

Loop error

Hybrid View

TimlmiT Loop error 01-25-2018, 05:48 PM
xladept Re: Loop error 01-25-2018, 05:59 PM
TimlmiT Re: Loop error 01-25-2018, 07:08 PM
Norie Re: Loop error 01-25-2018, 07:17 PM
TimlmiT Re: Loop error 01-25-2018, 07:19 PM
Norie Re: Loop error 01-25-2018, 08:00 PM
TimlmiT Re: Loop error 01-26-2018, 10:27 AM
  1. #1
    Forum Contributor
    Join Date
    06-21-2013
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2010
    Posts
    186

    Loop error

    Gurus,

    I am attempting to read a list of employee names, create at worksheet, and rename the worksheet with the employees names. I get through the first loop but it stops at

    Sheets("Do Not Delete (2)").Name = Name
    On the second one. Below is the entire code. The error is run-time error '1004' Application-defined or object-defined error


    Sub Create_Tabs()
    Dim Findrow As Range
    Dim RowNum As Long
    Dim i As Integer
    Dim Name As String
    
    
    Set Findrow = Range("B:B").Find(What:="Total Hours", LookIn:=xlValues)
    RowNum = Findrow.Row - 1
    
    For i = 4 To RowNum
    Name = Cells(i, 2).Value
    
    Sheets("Do Not Delete").Select
        Sheets("Do Not Delete").Copy Before:=Sheets("Do Not Delete")
        Sheets("Do Not Delete (2)").Select
        Sheets("Do Not Delete (2)").Name = Name
    
    
    Next i
    
    End Sub
    Any Clues?

    TIA,
    Tim

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Loop error

    Maybe:
    
    Sheets("Do Not Delete").Copy Before:=Sheets("Do Not Delete")    
                    ActiveSheet.Name = Name
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Forum Contributor
    Join Date
    06-21-2013
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2010
    Posts
    186

    Re: Loop error

    xladept,

    That didn't work... However, I did find out that when it looped to cells(5,2) the value was blank. Which is why I get an error during the renaming of the worksheet. But there is a name in cell b5..

    Stumped

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Loop error

    Tim

    You have not sheet reference for Cells(5,2) so it will refer to B5 on the active sheet and since you are creating new sheets that's probably going to be one of the new, blank sheets.

    Which sheet has the list of employee names?
    If posting code please use code tags, see here.

  5. #5
    Forum Contributor
    Join Date
    06-21-2013
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2010
    Posts
    186

    Re: Loop error

    Ohhhhh that makes sense.

    Employee list is on sheet("QP")

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Loop error

    Try this.
    Sub Create_Tabs()
    Dim Findrow As Range
    Dim strName As String
    Dim RowNum As Long
    Dim I As Long
    
        With Sheets("QP")
        
            Set Findrow = .Range("B:B").Find(What:="Total Hours", LookIn:=xlValues)
            
            RowNum = Findrow.Row - 1
    
            For I = 4 To RowNum
                strName = .Cells(I, 2).Value
                Sheets("Do Not Delete").Copy Before:=Sheets("Do Not Delete")
                Sheets("Do Not Delete (2)").Name = strName
            Next I
            
        End With
        
    End Sub

  7. #7
    Forum Contributor
    Join Date
    06-21-2013
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2010
    Posts
    186

    Re: Loop error

    Norie,

    Thanks for your code. I simply added the QP sheets in front of the cell value. It works just like I wanted. Here is what I ended up with.

    Sub Create_Tabs()
    Dim Findrow As Range
    Dim RowNum As Long
    Dim i As Integer
    Dim Name As String
    
    Sheets("QP").Select
    Set Findrow = Range("B:B").Find(What:="Total Hours", LookIn:=xlValues)
    RowNum = Findrow.Row - 1
    
    For i = 4 To RowNum
    Name = Sheets("QP").Cells(i, 2).Value
    
    If Name = "" Then
    GoTo Leap
    End If
    
    'MsgBox "Name = " & Name & "   RowNum = " & i
    Sheets("Do Not Delete").Select
        Sheets("Do Not Delete").Copy Before:=Sheets("Do Not Delete")
        Sheets("Do Not Delete (2)").Select
        ActiveSheet.Name = Name
    Leap:
    Next i
    
    End Sub
    Tim

+ 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. trying to copy a worksheet to end gives error and I get stuck in a error loop
    By pongmeister in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 08-11-2017, 11:48 AM
  2. Replies: 5
    Last Post: 12-08-2016, 06:43 PM
  3. [SOLVED] Go to next loop iteration if current loop has error
    By luv2glyd in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 02-03-2016, 09:54 PM
  4. Loop Nesting is returning Run-Time Error '-2147221080 (800401a8)': Automation Error
    By ChristopherBrandonKi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-21-2014, 02:36 PM
  5. HOW TO: Pause loop, fix error on a popup UserForm, continue loop
    By AndyMachin in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-19-2014, 04:37 PM
  6. Nesting a loop within a loop error
    By PunPryde in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-17-2011, 08:07 PM
  7. Error '1004', application/object error, Do Loop
    By farzyness in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-25-2011, 12:03 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