+ Reply to Thread
Results 1 to 7 of 7

Loop through of sheets and execute calculations based on dynamic sheet references

Hybrid View

  1. #1
    Registered User
    Join Date
    01-27-2014
    Location
    Fresno, USA
    MS-Off Ver
    Excel 2007
    Posts
    47

    Loop through of sheets and execute calculations based on dynamic sheet references

    Greetings,

    Is it possible to loop through a list of sheets and execute some calculations that have sheet references from a different list of worksheets? For example, you have a list (list 1) of your worksheets, which will be the destinations of the calculations, and you have a second list (list 2) of worksheets that the calculations are based on. So, lets say there are sheet1 and sheet2 in list 1, and sheetA and sheetB in list 2. The calculations based on sheetA would appear in sheet1, and calculations based on sheetB would appear in sheet2. I thought the code would look something like this:

     Sub LoopthroughWorksheets()
        Dim sheet_name As Range
        Dim sheet_name2 As Range
        Set sheet_name2 = Sheets("WS").Range("F:F")
        For Each sheet_name In Sheets("WS").Range("C:C")
            If sheet_name.Value = "" Then
                Exit For
            Else
                With Sheets(sheet_name.Value)
                        .Range("K1") = .Range("sheet_name2.Value!A14").Value
                End With
            End If
            Next sheet_name
        End Sub
    I'm getting a "Run-time error '1004: Application-defined or object defined error" at this line:
    .Range("K1") = .Range("sheet_name2.Value!A14").Value
    Any help is much appreciated.

    Regards,

  2. #2
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: Loop through of sheets and execute calculations based on dynamic sheet references

    Replace your code with this below one

    .Range("K1") = Sheets(sheet_name2.Value).Range("A14").Value
    Thanks - Naveed
    -----------------------------
    If the suggestion helps you, then Click * to Add Reputation
    To Attach File: Go Advanced>>Manage Attachments>>Add Files (In Top Right Corner)>>SelectFiles>>.........Locate Your File(s)>>Upload Files>>Done (In Bottom Right)
    1. Use [code] code tags [\code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    2. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

  3. #3
    Registered User
    Join Date
    01-27-2014
    Location
    Fresno, USA
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: Loop through of sheets and execute calculations based on dynamic sheet references

    Thanks for the reply Naveed. With this code:
    .Range("K1") = Sheets("sheet_name2").Range("A14").Value
    I get a run time error 9: subscript out of range. With this code:
    .Range("K1") = Sheets(sheet_name2).Range("A14").Value
    I get a run time error 13: type mismatch. With this code:
    .Range("K1") = Sheets(sheet_name2.Value).Range("A14").Value
    I get a run time error 13: type mismatch. Any thoughts as to what could still be going wrong? The worksheet list for list one is A1:A51 and for list 2 is F1:F51. Cell A14 contains a number and not text.

  4. #4
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: Loop through of sheets and execute calculations based on dynamic sheet references

    Plz provide sample workbook with desire output
    Sent from my RM-914_im_india_269 using Tapatalk

  5. #5
    Registered User
    Join Date
    01-27-2014
    Location
    Fresno, USA
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: Loop through of sheets and execute calculations based on dynamic sheet references

    Here is a test workbook that populates cell K5 on worksheets in list 1 with the cell A14 value from the worksheets in list 2.Test1.xlsx. List 1 is on the worksheet "WS" in column A (instead of column C) and list 2 is on the worksheet "WS" in column B (instead of column F). Populating K5 on list 1 worksheets with A14 from list 2 worksheets is of no particular importance. It's more about being able to loop through a list of worksheets and perform calculations using sheet references based on a second list of worksheets. Thanks again for your help.
    Last edited by surePac; 07-01-2014 at 10:51 AM.

  6. #6
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: Loop through of sheets and execute calculations based on dynamic sheet references

    Hi SurePac

    i didnt get your query what exactly u want the output

  7. #7
    Registered User
    Join Date
    01-27-2014
    Location
    Fresno, USA
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: Loop through of sheets and execute calculations based on dynamic sheet references

    Thanks for you help Naveed. This is the code I was trying to produce.

    Sub LoopthroughWorksheets()
        Dim sheet_name As Range
        Dim sheet_name2 As Range
        Set sheet_name2 = Sheets("WS").Range("F:F")
        ' NEW
        Dim counter As Long
        counter = 1
    
        For Each sheet_name In Sheets("WS").Range("C:C")
            If sheet_name.Value = "" Then
                Exit For
            Else
                With Sheets(sheet_name.Value)
                    .Range("K1").Value = Sheets(sheet_name2(counter, 1).Value).Range("A14").Value
                    ' NEW                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                   
                    counter = counter + 1
                End With
            End If
        Next sheet_name
    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. [SOLVED] Rename Sheets by using Dynamic Loop for the rows
    By desolatori in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-30-2014, 09:36 AM
  2. Replies: 1
    Last Post: 01-25-2014, 02:10 PM
  3. Dynamic array, info one sheet to another based on criteria then macro to print sheets
    By jmendenhall22 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-22-2013, 10:25 AM
  4. Replies: 1
    Last Post: 02-18-2012, 10:31 AM
  5. Replies: 1
    Last Post: 05-28-2010, 07:19 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