Results 1 to 5 of 5

Thread: For Each ws in ActiveWorkbook.Worksheets loop trouble
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular
    Join Date
    Jul 2014
    Posts
    614
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default For Each ws in ActiveWorkbook.Worksheets loop trouble

    Greetings,

    I have the macro below giving trouble. The body of the macro will run, but does not want to seem to perform the task on a new sheet. It seems to be performing the same task on the same sheet (once for every sheet in the workbook)

    Code:
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
    
    Range.("H1").Select
    ActiveCell.value = "VR"
    Range.("I1").Select
    ActiveCell.value = "Average"
    
    Nest ws
    Any ideas why the loop isn't running the code for each worksheet in the workbook?

  2. #2
    MrExcel MVP
    Join Date
    Mar 2004
    Location
    Canada
    Posts
    18,855
    Post Thanks / Like
    Mentioned
    35 Post(s)
    Tagged
    5 Thread(s)

    Default Re: For Each ws in ActiveWorkbook.Worksheets loop trouble

    You need to qualify your range references, for example...

    Code:
    ws.Range("H1").Select
    However, there's no need to select the cell before entering a value. Try the following...

    Code:
    Dim ws As Worksheet
    
    For Each ws In ActiveWorkbook.Worksheets
        With ws
            .Range("H1").Value = "VR"
            .Range("I1").Value = "Average"
        End With
    Next ws
    Hope this helps!
    Last edited by Domenic; Sep 3rd, 2019 at 04:57 PM.

  3. #3
    Board Regular
    Join Date
    Jul 2014
    Posts
    614
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: For Each ws in ActiveWorkbook.Worksheets loop trouble

    Great, just needed to qualify the range references, now its running through each sheet.

    Thanks!

  4. #4
    MrExcel MVP
    Join Date
    Mar 2004
    Location
    Canada
    Posts
    18,855
    Post Thanks / Like
    Mentioned
    35 Post(s)
    Tagged
    5 Thread(s)

    Default Re: For Each ws in ActiveWorkbook.Worksheets loop trouble

    You're very welcome!

    Cheers!

  5. #5
    MrExcel MVP Rick Rothstein's Avatar
    Join Date
    Apr 2011
    Location
    New Jersey, USA
    Posts
    35,224
    Post Thanks / Like
    Mentioned
    92 Post(s)
    Tagged
    33 Thread(s)

    Default Re: For Each ws in ActiveWorkbook.Worksheets loop trouble

    Quote Originally Posted by Domenic View Post
    However, there's no need to select the cell before entering a value. Try the following...
    Code:
    Dim ws As Worksheet
    
    For Each ws In ActiveWorkbook.Worksheets
        With ws
            .Range("H1").Value = "VR"
            .Range("I1").Value = "Average"
        End With
    Next ws
    Hope this helps!
    Just to follow up... there is no need to loop through all of the sheets individually either. These two lines of code will put "VR" in cell H1 and "Average" in cell I1 on every sheet in the active workbook no matter what sheet is currently active.
    Code:
    Range("H1:I1") = Split("VR Average")
    Sheets.FillAcrossSheets Range("H1:I1")
    Last edited by Rick Rothstein; Sep 4th, 2019 at 11:04 AM.
    Rick's "mini" blog... http://www.excelfox.com/forum/f22/
    .
    Want to post a small screen shot? See Part B here.

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •