+ Reply to Thread
Results 1 to 3 of 3

Cycle through sheets and drag down formula - Not working

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-05-2007
    Posts
    148

    Cycle through sheets and drag down formula - Not working

    I've got a bit of code that puts the worksheet name in cell C2 which works perfectly for all sheets.

    I then want to drag this down depending on how many cells there are in cell A, again for all sheets. This doesn't seem to work, it does nothing.

    Any ideas?

    Sub SheetName()
    
    
        
        
        For Each ws In ActiveWorkbook.Worksheets
        
        If ws.Name <> ActiveSheet.Name Then
        
        ws.Range("C2").Value = ws.Name
        
         
        Dim LR As Integer
         LR = Range("A" & Rows.Count).End(xlUp).Row
         Range("C2").AutoFill Destination:=Range("C2:C" & LR), Type:=xlFillDefault
    
    
        
        
        End If
    
        Next
    Thanks

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Cycle through sheets and drag down formula - Not working

    This works for me.

    Sub SheetName()
    
        Dim LR As Integer
       For Each ws In ActiveWorkbook.Worksheets
        
           With ws
                If .Name <> ActiveSheet.Name Then
                
                  .Range("C2").Value = .Name
                
                  LR = .Range("A" & .Rows.Count).End(xlUp).Row
                 .Range("C2").AutoFill Range("C2:C" & LR), Type:=xlFillDefault
            
                End If
           End With
       Next
    End Sub

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644
    You aren't referencing the worksheets you are looping through.

    Try this.

    Sub SheetName()
    Dim LR As Long
    
        
        
        For Each ws In ActiveWorkbook.Worksheets
        
        If ws.Name <> ActiveSheet.Name Then
        
        ws.Range("C2").Value = ws.Name
    
         LR = ws.Range("A" & Rows.Count).End(xlUp).Row
         ws.Range("C2").AutoFill Destination:=ws.Range("C2:C" & LR), Type:=xlFillDefault
    
    
        
        
        End If
    
        Next
    Or even this.
    Dim LR As Long
    
         LR = ws.Range("A" & Rows.Count).End(xlUp).Row
         ws.Range("C2:C" & LR).Value = ws.Name
    If posting code please use code tags, see here.

+ 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] Cycle through a long set of output and select certain rows/columns for new sheets
    By matrix_machine in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-28-2012, 05:32 PM
  2. Cycle Through Sheets & Sorting Named Ranges
    By jordan2322 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-12-2012, 09:53 PM
  3. Cycle through my sheets and make charts static?
    By thedon_1 in forum Excel Charting & Pivots
    Replies: 8
    Last Post: 12-05-2011, 01:00 PM
  4. Sum a column drag 2 next not working
    By famreeks in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 11-06-2010, 11:01 PM
  5. Cycle sheets & copy color coding
    By Hasseldm in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-24-2008, 10:14 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