+ Reply to Thread
Results 1 to 5 of 5

How do I loop through worksheets and apply same function using for loop?

  1. #1
    Registered User
    Join Date
    05-02-2017
    Location
    Singapore
    MS-Off Ver
    2007
    Posts
    3

    How do I loop through worksheets and apply same function using for loop?

    Hello, I'm new to VBA and am trying to learn it. I've written this function to try to loop through some worksheets within the same workbook and add a column to update the week notated as 'ww' based on the cell to the left.

    I'm having issues with the For loop where it's supposed to loop through the defined sh1 to sh3. How do I declare the sheets to loop if using sheet.("sh"&i) does not work for the For loop?

    The bold part is where I receive the debug error.
    "Run time error: 9
    subscript out of range"

    I'm largely unsure of what I'm doing and experimenting a lot. On the side not, is there any good online learning courses that I can use to grow my knowledge on VBA?
    Please Login or Register  to view this content.
    Last edited by NagamiKai; 05-03-2017 at 02:46 AM. Reason: Solved by the really nice guy below <3

  2. #2
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: How do I loop through worksheets and apply same function using for loop?

    are you sure you want a function and not a subroutine?

    here is another way you could do it

    Please Login or Register  to view this content.
    Last edited by humdingaling; 05-03-2017 at 01:31 AM. Reason: OP complied with request
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

  3. #3
    Registered User
    Join Date
    05-02-2017
    Location
    Singapore
    MS-Off Ver
    2007
    Posts
    3

    Re: How do I loop through worksheets and apply same function using for loop?

    Hello, I've copied and pasted your codes. I'm not sure why it's returning with the error that the variable 'ws' is not defined. I'm assuming that ws refers to work sheet, do I need to define what ws is? And to call a sub I can just use
    Please Login or Register  to view this content.
    right? Provided that I listed the subroutine as
    Please Login or Register  to view this content.
    . Thank you so much for helping me, I'm still trying to figure my way around things.

    #Edit_1: I realized that yes, I have to declare ws as an excel worksheet.

    Please Login or Register  to view this content.
    Last edited by NagamiKai; 05-03-2017 at 01:26 AM.

  4. #4
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: How do I loop through worksheets and apply same function using for loop?

    oops that was my mistake leaving the declaration of ws out...i have inserted it now

  5. #5
    Registered User
    Join Date
    05-02-2017
    Location
    Singapore
    MS-Off Ver
    2007
    Posts
    3

    Re: How do I loop through worksheets and apply same function using for loop?

    Thank you for helping me. : ) You're great.

+ 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. Run Macro Through List and Apply to worksheets using a loop
    By kingsdime29x in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-13-2017, 04:35 PM
  2. Comparing Similiar DataSets and Calling a Function From a Loop Within a Loop
    By Mark123456789 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-23-2016, 11:27 AM
  3. [SOLVED] Loop through worksheets and apply conditional formatting
    By beenbee in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-02-2016, 07:20 AM
  4. Can I apply a loop inside a loop?
    By vibhor in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-18-2015, 11:55 AM
  5. Find function in nested loop breaking down - not on first time through loop
    By adamstarr12 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-07-2014, 04:59 PM
  6. [SOLVED] Have a loop macro loop for all visible worksheets
    By Graeme-Black in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-22-2014, 11:01 AM
  7. Call Macros before Next loop (loop creates new worksheets)
    By mantooth29 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-24-2012, 05:47 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