+ Reply to Thread
Results 1 to 6 of 6

Looping Through Sheets in a workbook in Excel 2010

  1. #1
    Registered User
    Join Date
    10-15-2012
    Location
    Ireland
    MS-Off Ver
    Excel 2010
    Posts
    8

    Looping Through Sheets in a workbook in Excel 2010

    Hi All,

    Im new to this so please bear with me. I dont have a lot of experiance in VBA.
    Currently i am looking to modify over 1200 workbooks. all with the same name in different folders. inside the workbooks i have different sheets. possibly up to 30 sheets. some of the workbooks would have the same sheets with the same name. Ie Job sheet, Tool List etc. how ever not all these sheets are inside everyone of the workbooks. is there a code that I could use to call the sheet and if that sheet is missing it would skip over the that sepific sheet and find the next piece of code with out erroring out.

    Many thanks for your help.

    John

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Looping Through Sheets in a workbook in Excel 2010

    to loop through Sheets
    Please Login or Register  to view this content.
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    11-19-2012
    Location
    Wilmington Delaware
    MS-Off Ver
    Excel 2003 2007 2010
    Posts
    23

    Re: Looping Through Sheets in a workbook in Excel 2010

    John, you can do something like the following:

    Please Login or Register  to view this content.
    Basically, the above sets wS to the worksheet if it exists. Otherwise it remains set to "nothing". The if test uses a "Not" statement to test for the opposite..

    Lots of things to consider. You need to create your list of workbooks before you modify any. Otherwise, if you are querying for them, you will not get them all, as opening a workbook changes the file list order. Use an array to list the sheets that way you can loop through all the sheets. Use subroutines. Don't put hundreds of lines of code between the if..end if above

    Robert Flanagan
    http://www.add-ins.com
    Productivity add-ins and downloadable books on VB macros for Excel

  4. #4
    Registered User
    Join Date
    10-15-2012
    Location
    Ireland
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Looping Through Sheets in a workbook in Excel 2010

    Hi Patel45 and Bob, may thanks for yor speedy replies. I have not had a chance to get back on this VBA project today but i will be back on to it tomorrow. I will reply with feedback tomrw. many thanks again

    John

  5. #5
    Registered User
    Join Date
    10-15-2012
    Location
    Ireland
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Looping Through Sheets in a workbook in Excel 2010

    Hi Guys, I have tried doing the code as suggested. I have run with your Bob, however when I run the macro and it comes across a sheet that doesnt excist it give me a "subscript out of range" I persume this is because the sheet is not in the workbook i am going through?
    Here is a look at some of the code i am running.

    40

    Set wS = Nothing
    On Error Resume Next
    Set wS = Sheets("Tracking Label")
    On Error GoTo 50
    If Not wS Is Nothing Then
    'code to run if sheet exists
    Sheets("Tracking Label").Select
    'ActiveSheet.Unprotect Password:="Floor#01"
    With ActiveSheet.PageSetup
    '.LeftFooter = "QD 7.5.1-001-08"
    '.RightFooter = "Rev A"
    Range("C3:E3,C4:E4,G3").Select
    Range("G3").Activate
    Selection.ClearContents
    'Selection.UnMerge
    Range("C3:E3").Select
    Selection.NumberFormat = "General"
    ActiveCell.Value = "='Cover Sheet'!D3" 'PN
    Range("C4:E4").Select
    Selection.NumberFormat = "General"
    ActiveCell.Value = "='Cover Sheet'!S1" 'Job#
    'Range("C5:E5").Select
    'Selection.NumberFormat = "General"
    'ActiveCell.Value = "='Cover Sheet'!O3" 'Descrip
    Range("G3").Select
    Selection.NumberFormat = "General"
    ActiveCell.Value = "='Cover Sheet'!L1" 'Rev#
    'Range("B3").Select
    'Rows("5:100").Select
    Selection.Locked = False
    Selection.FormulaHidden = False
    Range("A1").Select
    'ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:="Floor#01"

    End With
    On Error GoTo 50
    End If
    50


    'Then i have code going from 50 on wards. Many thanks for your help.

    John

  6. #6
    Registered User
    Join Date
    10-15-2012
    Location
    Ireland
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Looping Through Sheets in a workbook in Excel 2010

    Hi Guys,

    Just to let you know i got this eventually to work however your code was perfect and worked really well but for some reason it would bom out on certain workbooks and would throw up an error looking for the sheet to select.

    Many thanks for you help on this one.

    John

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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