Results 1 to 13 of 13

Thread: Loop through Excel sheets [Resolved]

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2005
    Location
    Australia
    Posts
    46

    Resolved Loop through Excel sheets [Resolved]

    Ok all,
    I have a question I'm not sure where to start with this. I would like to create a macro that looks at all the sheets in a work book and returns the value of cell C7 in each work book in a list (column A) on the first worksheet.

    Can someone please point me in the right direction as to what i should be doing.
    Last edited by Pearso; Mar 16th, 2005 at 12:42 AM. Reason: Resolved

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Loop through Excel sheets

    Welcome to the Forums.

    Sure, but we need to clarify a few things first. A workbook has one or more sheets (or worksheets.
    same object, different name). From your question I gather that you have several workbooks (Book1.xls, Book2.xls, etc.)
    open that you need to get the C7 value from sheet1?

    We can do this from VBA or VB6, which one would you prefer?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2005
    Location
    Australia
    Posts
    46

    Re: Loop through Excel sheets

    I'm using VBA, What i would like to do is loop througt all the worksheets in a workbook and return the value in C7 on the first worksheet in a column.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Loop through Excel sheets

    Ok, so the results will be in sheet1 - A1.
    So all the other sheets C7 are of what data type? Numeric?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2005
    Location
    Australia
    Posts
    46

    Re: Loop through Excel sheets

    Yes all the C7 cells are Numeric (numbers).

    Thanks for this help by the way.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Loop through Excel sheets

    Ok, this assumes that sheet 1 is going to be the sheet to receive the total and that the values totaled are numeric.
    VB Code:
    1. Public Sub TotalC7s2A1()
    2.  
    3.     Dim i As Integer
    4.     Dim iTotal As Integer
    5.     Dim oWB As Workbook
    6.    
    7.     iTotal = 0
    8.     Set oWB = ActiveWorkbook
    9.     For i = 2 To oWB.Sheets.Count
    10.         iTotal = iTotal + oWB.Sheets(i).Cells(7, 3).Value
    11.     Next
    12.     oWB.Sheets(1).Cells(1, 1).Value = iTotal
    13.     Set oWB = Nothing
    14.    
    15. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2005
    Location
    Australia
    Posts
    46

    Re: Loop through Excel sheets

    Thank you very much that works a treat.

    Is it possible to list the numbers down the page it A1 = C7 from worksheet1, A2 = C7 from worksheet2 and so on.

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Loop through Excel sheets

    So no totaling but a column showing each sheets C7 value and sheet name?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    Member
    Join Date
    Mar 2005
    Location
    Australia
    Posts
    46

    Re: Loop through Excel sheets

    yeah thats it

    I'm trying to create a basic report from a series of worksheets in a single workbook.

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Loop through Excel sheets

    See if this is ok.
    VB Code:
    1. Public Sub TotalC7s2Sheet1()
    2.  
    3.     Dim i As Integer
    4.     Dim x As Integer
    5.     Dim sSheet As String
    6.     Dim oWB As Workbook
    7.    
    8.     x = 1
    9.     Set oWB = ActiveWorkbook
    10.     For i = 2 To oWB.Sheets.Count
    11.         oWB.Sheets(1).Cells(x, 1).Value = oWB.Sheets(i).Cells(7, 3).Value
    12.         oWB.Sheets(1).Cells(x, 2).Value = "From: " & oWB.Sheets(i).Name
    13.         x = x + 1
    14.     Next
    15.     Set oWB = Nothing
    16.    
    17. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11

    Thread Starter
    Member
    Join Date
    Mar 2005
    Location
    Australia
    Posts
    46

    Re: Loop through Excel sheets

    thankyou very much that works perfectly. I realy need to do some more reading on how to write VBA code.
    One last question how do a mark this thread as resolved?

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Loop through Excel sheets

    No prob. Glad to help.

    You can Resolve your thread by editing your first post and either changing
    the subject icon to the green check mark or adding "[Resolved]" to the end
    of the subject text.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13

    Thread Starter
    Member
    Join Date
    Mar 2005
    Location
    Australia
    Posts
    46

    Resolved Re: Loop through Excel sheets [Resolved]

    Cheers Robdog

Posting Permissions

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



Click Here to Expand Forum to Full Width