+ Reply to Thread
Results 1 to 14 of 14

Looping through unkown sheet names

Hybrid View

  1. #1
    Registered User
    Join Date
    08-22-2012
    Location
    Townsville
    MS-Off Ver
    Excel 2010
    Posts
    38

    Looping through unkown sheet names

    Hi All,

    Searched and searched and couldn't find anything. Could you please help.

    I've got this code below which looks at my data and when data changes in the row it grabs it all and adds a new sheet and pastes the data. Problem I have now is I want to format each sheet (the same way) but I don't know the sheet names so can't reference them. Is there anyway I can loop through uknown names and perform a code?

    Dim lastrow As Long, LastCol As Integer, I As Long, iStart As Long, iEnd As Long
    Dim ws As Worksheet
    Application.ScreenUpdating = False
    With ActiveSheet
        lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
        LastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
        .Range(.Cells(2, 1), Cells(lastrow, LastCol)).Sort Key1:=Range("A1"), Order1:=xlAscending, _
        header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
        iStart = 2
        For I = 2 To lastrow
        If .Range("A" & I).Value <> .Range("A" & I + 1).Value Then
            iEnd = I
            Sheets.Add after:=Sheets(Sheets.Count)
            Set ws = ActiveSheet
            On Error Resume Next
            ws.Name = .Range("A" & iStart).Value
            On Error GoTo 0
            ws.Range(Cells(1, 1), Cells(1, LastCol)).Value = .Range(.Cells(1, 1), .Cells(1, LastCol)).Value
            .Range(.Cells(iStart, 1), .Cells(iEnd, LastCol)).Copy
            ws.Range("A2").Select
            ActiveSheet.Paste
            Cells.Select
        Cells.EntireColumn.AutoFit
        Range("A1").Select
            iStart = iEnd + 1
        End If
        Next I
    End With
    I've tried this code but it only seems to do it on the first sheet - but does it at the count of the other sheets.

    Dim z As Integer
    Dim lrow As Long
    
    For z = 1 To Sheets.Count
        With Worksheets(z)
            Rows("1:2").Select
        Selection.Insert Shift:=xlDown
        Range("A1:K1").Select
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        Selection.merge
        End With
        Next z
    Any help greatly appreciated

  2. #2
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: Looping through unkown sheet names

    Dim ws as Worksheet
    
    For Each ws in ActiveWorkbook.Worksheets
    
    'code here
    
    Next
    And if you want to exclude a particular sheet:

    Dim ws as Worksheet
    
    For Each ws in ActiveWorkbook.Worksheets
    
    If ws.Name <> "Important Sheet Name" Then
    
    'code here
    
    End If
    
    Next
    Make Mom proud: Add to my reputation if I helped out!

    Make the Moderators happy: Mark the Thread as Solved if your question was answered!

  3. #3
    Registered User
    Join Date
    08-22-2012
    Location
    Townsville
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Looping through unkown sheet names

    Thanks for the reply daffodil - but it didn't work either, does the same as my other code. Just does it for the first sheet. At the moment I've got 9 different sheets and it does it 9 times on the first sheet.

    Any other thoughts?

  4. #4
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: Looping through unkown sheet names

    Sub PinkPonies()
    
    Dim lastrow As Long, LastCol As Integer, I As Long, iStart As Long, iEnd As Long
    Dim ws As Worksheet
    
    Application.ScreenUpdating = False
    
    For Each ws In ActiveWorkbook.Worksheets
    
        With ws
            lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
            LastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
            .Range(.Cells(2, 1), Cells(lastrow, LastCol)).Sort Key1:=Range("A1"), Order1:=xlAscending, _
            Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
            iStart = 2
        
            For I = 2 To lastrow
                If .Range("A" & I).Value <> .Range("A" & I + 1).Value Then
                    iEnd = I
                    Sheets.Add after:=Sheets(Sheets.Count)
                    On Error Resume Next
                    ws.Name = .Range("A" & iStart).Value
                    On Error GoTo 0
                    ws.Range(Cells(1, 1), Cells(1, LastCol)).Value = .Range(.Cells(1, 1), .Cells(1, LastCol)).Value
                    .Range(.Cells(iStart, 1), .Cells(iEnd, LastCol)).Copy
                    ws.Range("A2").Select
                    ws.Paste
                    Cells.Select
                    Cells.EntireColumn.AutoFit
                    Range("A1").Select
                    iStart = iEnd + 1
                End If
            Next I
        
        End With
    
    Next
    
    End Sub

  5. #5
    Registered User
    Join Date
    08-22-2012
    Location
    Townsville
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Looping through unkown sheet names

    Sorry daffodil - pinkponies didn't work :P

    failed at this
    ws.Range(Cells(1, 1), Cells(1, LastCol)).Value = .Range(.Cells(1, 1), .Cells(1, LastCol)).Value
    I'm not having any problems with this part of the code though. Once this part finishes its all good, it's the next part when I call
    Dim z As Integer
    Dim lrow As Long
    
    For z = 1 To Sheets.Count
        With Worksheets(z)
            Rows("1:2").Select
        Selection.Insert Shift:=xlDown
        Range("A1:K1").Select
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        Selection.merge
        End With
        Next z

  6. #6
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: Looping through unkown sheet names

    Is your code on the sheet, or is it out in a Module?

    Also, in your code you call ActiveSheet a number of times which will only refer to the top active sheet.

  7. #7
    Registered User
    Join Date
    08-22-2012
    Location
    Townsville
    MS-Off Ver
    Excel 2010
    Posts
    38
    It is in a module. I will try that code when I get back in the office, thanks

  8. #8
    Registered User
    Join Date
    08-22-2012
    Location
    Townsville
    MS-Off Ver
    Excel 2010
    Posts
    38
    Also both the codes I use are in different subs, I use another to call them.

  9. #9
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Looping through unkown sheet names

    The bit that you have at the end of post #8 does not reference the worksheet(z) when selectin Range("A1:K1") so it will always reference the activesheet rather than looping through them. Try something like:
    Sub macro_2()
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Sheets
        ws.Rows("1:2").Insert shift:=xlDown
        With ws.Range("A1:K1")
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
            .Merge
        End With
    Next ws
    End Sub

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

    Re: Looping through unkown sheet names

    You need the dot before the Range i.e.

    Dim z As Integer
    Dim lrow As Long
    
    For z = 1 To Sheets.Count
        With Worksheets(z)
            Rows("1:2").Select
        Selection.Insert Shift:=xlDown
        .Range("A1:K1").Select
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        Selection.merge
        End With
        Next z
    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

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

    Re: Looping through unkown sheet names

    Try this.
    Dim z As Integer
    Dim lrow As Long
    
    For z = 1 To Sheets.Count
        With Worksheets(z)
            .Rows("1:2").Insert Shift:=xlDown
        With .Range("A1:K1")
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
            .Merge
        End With
      
        End With
    Next z
    If posting code please use code tags, see here.

  12. #12
    Registered User
    Join Date
    08-22-2012
    Location
    Townsville
    MS-Off Ver
    Excel 2010
    Posts
    38

    Re: Looping through unkown sheet names

    Thanks all for replying and helping me, I started at the bottom with Norie's and it worked a charm
    Cheers!!

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

    Re: Looping through unkown sheet names

    Hey Amoxia,

    Did you try your own code with that one small change??

  14. #14
    Registered User
    Join Date
    08-22-2012
    Location
    Townsville
    MS-Off Ver
    Excel 2010
    Posts
    38
    Yes mate, and the . Before the rows to, also made some other changes, just more formatting really. Thanks

+ 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] Create an Array which has Sheet Names excluding a Few Sheet Names to be used in a MACRO
    By e4excel in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 11-29-2013, 03:24 PM
  2. SUMIF with a list of unkown criteria
    By londbrit in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-18-2011, 05:57 PM
  3. Looping through object names
    By pdauction in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-22-2011, 05:01 AM
  4. looping over combobox names
    By lottesfog in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-23-2007, 01:35 AM
  5. Looping through range names
    By Craig in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-16-2005, 01:05 AM

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