+ Reply to Thread
Results 1 to 8 of 8

Looping through worksheets

Hybrid View

The Phil Looping through worksheets 01-17-2014, 02:18 PM
Sean Thomas Re: Looping through worksheets 01-17-2014, 02:22 PM
judgeh59 Re: Looping through worksheets 01-17-2014, 02:22 PM
Norie Re: Looping through worksheets 01-17-2014, 02:24 PM
Sean Thomas Re: Looping through worksheets 01-17-2014, 02:24 PM
JOHN H. DAVIS Re: Looping through worksheets 01-17-2014, 02:25 PM
The Phil Re: Looping through worksheets 01-17-2014, 03:06 PM
Haikal_ Re: Looping through worksheets 01-19-2014, 10:21 AM
  1. #1
    Forum Contributor
    Join Date
    02-24-2010
    Location
    BC, Canada
    MS-Off Ver
    Excel 2010
    Posts
    174

    Looping through worksheets

    Hi,

    I am trying to perform the same actions on all of the worksheets in my workbook. I have tried the following two codes but neither of them work:

    Dim ws As Integer
    
    For ws = 1 To Worksheets.Count
     Sheets(ws).Select
      Cells(4, 4) = RYear
      Range("A5") = "setdefault Period=" & RYear & "00:" & RYear & "13"
      Range("A6") = "setdefault Period_from=" & RYear & "00"
      Range("A7") = "setdefault Period_to=" & RYear & "13"
      Range("B16") = Range("D3") & Range("D4")
      Range("B17") = "Report run on " & Format(Date, "mmm dd, yyyy")
    Next ws
    Dim ws As Worksheet
    
    For Each ws In ActiveWorkbook.Sheets
     Cells(4, 4) = RYear
     Range("A5") = "setdefault Period=" & RYear & "00:" & RYear & "13"
     Range("A6") = "setdefault Period_from=" & RYear & "00"
     Range("A7") = "setdefault Period_to=" & RYear & "13"
     Range("B16") = Range("D3") & Range("D4")
     Range("B17") = "Report run on " & Format(Date, "mmm dd, yyyy")
    Next
    I tried that last one with "next" and "next ws".

    Neither of these are making all of the changes that are specified. Any thoughts?

    Thank you.
    Last edited by The Phil; 01-17-2014 at 03:23 PM.

  2. #2
    Valued Forum Contributor Sean Thomas's Avatar
    Join Date
    03-25-2012
    Location
    HerneBay, Kent, UK
    MS-Off Ver
    Excel 2007,2016
    Posts
    971

    Re: Looping through worksheets

    whats not working?
    is there an error? if so where is the error occuring?
    Regards
    Sean

    Please add to my reputation if you think i helped
    (click on the star below the post)
    Mark threads as "Solved" if you have your answer
    (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code:
    [code] Your code here [code]
    Please supply a workbook containing example Data:
    It makes its easier to answer your problem & saves time!

  3. #3
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Looping through worksheets

    maybe try adding ws. to the beginning of cells and ranges...
    Ernest

    Please consider adding a * if I helped

    Nothing drives me crazy - I'm always close enough to walk....

  4. #4
    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 worksheets

    Try this.
    For ws = 1 To Worksheets.Count
        With Sheets(ws)
            .Cells(4, 4) = RYear
            .Range("A5") = "setdefault Period=" & RYear & "00:" & RYear & "13"
            .Range("A6") = "setdefault Period_from=" & RYear & "00"
            .Range("A7") = "setdefault Period_to=" & RYear & "13"
            .Range("B16") = Range("D3") & Range("D4")
            .Range("B17") = "Report run on " & Format(Date, "mmm dd, yyyy")
        End With
    Next ws
    If posting code please use code tags, see here.

  5. #5
    Valued Forum Contributor Sean Thomas's Avatar
    Join Date
    03-25-2012
    Location
    HerneBay, Kent, UK
    MS-Off Ver
    Excel 2007,2016
    Posts
    971

    Re: Looping through worksheets

    what is Ryear? you dont have anything declared? is it a variable?
    if it cant find it, it will error or ignore the code

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Looping through worksheets

    Is RYear specified in each sheet or do you need to reference a value from a specific sheet?

    Sub ThePhil()
    Dim ws As Worksheet
    
    For Each ws In ActiveWorkbook.Sheets
    ws.Activate
     Cells(4, 4) = RYear
     Range("A5") = "setdefault Period=" & RYear & "00:" & RYear & "13"
     Range("A6") = "setdefault Period_from=" & RYear & "00"
     Range("A7") = "setdefault Period_to=" & RYear & "13"
     Range("B16") = Range("D3") & Range("D4")
     Range("B17") = "Report run on " & Format(Date, "mmm dd, yyyy")
    Next ws
    End Sub

  7. #7
    Forum Contributor
    Join Date
    02-24-2010
    Location
    BC, Canada
    MS-Off Ver
    Excel 2010
    Posts
    174

    Re: Looping through worksheets

    Thank you all for responding.

    I added the "ws.activate" line and that fixed it.

    My problem is that the cells weren't being updated on each sheet. Now it is working with that extra line of code.

    Thanks!!

  8. #8
    Registered User
    Join Date
    01-17-2014
    Location
    Egypt
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Looping through worksheets

    It is now working
    i have added:
    Ws.Activate

    the final working code is
    
    Sub RunMacroOnSpecificSheets()
    Dim Ws As Worksheet, sName1 As String, sName2 As String
    
    sName1 = "Weekday"
    sName2 = "Weekdend"
    
    For Each Ws In ThisWorkbook.Worksheets
    Ws.Activate
        If Left(Ws.Name, Len(sName1)) = sName1 Or _
                Left(Ws.Name, Len(sName2)) = sName2 Then
            'Your Goes Code Here....
            
            Range("A1").Select
        ActiveCell.FormulaR1C1 = "Haikal"
            
            
        End If
    Next Ws
    
    End Sub

+ 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. Looping through worksheets
    By terkel in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-25-2008, 03:54 AM
  2. Looping through worksheets
    By terkel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-17-2008, 02:53 AM
  3. Looping through Worksheets
    By Steve in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-12-2005, 03:05 PM
  4. Looping across worksheets
    By Ramthebuffs in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-19-2005, 12:52 AM
  5. Looping through Worksheets
    By Kirk P. in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-07-2005, 02:06 PM

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