+ Reply to Thread
Results 1 to 25 of 25

VBA to Loop through list of sheets and apply macro

Hybrid View

  1. #1
    Registered User
    Join Date
    06-18-2012
    Location
    united states
    MS-Off Ver
    Excel 2003
    Posts
    19

    VBA to Loop through list of sheets and apply macro

    Please help, I've been stuck on this for hours. I have one workbook that is saved as macrobook.xlsm in which column A has a list of worksheet names that I would like to apply a macro to. The worksheets however, are in another workbook called wb05012013.xlsx. The "05012013" refers to a date since the file date is updated and that date value will be in a named cell called "date" in the macrbook.xlsm. I would like the macro to loop through each sheet in wb05012013.xlsx, select all cells and paste values. Then, once the macro comes to a blank cell or end of the list if you will, I'd like it to delete the sheets that are not in the list. Also both of the workbooks are in a shared drive but in the same folder, not sure if that detail makes a difference because the code loops at home, but I get errors at work maybe because the files are stored in shared drives? This is what I have so far...


    
    Sub Newweek()
        Dim mywb As Workbook
        Set mywb = ActiveWorkbook
        Dim wb2 As Workbook
        Workbooks.Open Filename:="E:\archive\wb" & date & ".xlsx"
        Set wb2 = ActiveWorkbook
        
        Dim rngCell As Range
        Dim strSheetActive As String: strSheetActive = ActiveSheet.Name
        Application.ScreenUpdating = False
         
           
            For Each rngCell In .Range("A1:A" & .Range("A" & .Rows.Count).End(xlUp).Row)
                If Trim(rngCell.Value) <> vbNullString Then Call MacroToDoTheWork(rngCell.Value)
            Next rngCell
        
         
        Application.Goto mywb.Worksheets(strSheetActive).Cells(1)
        Application.ScreenUpdating = True
        Set rngCell = Nothing
         
    End Sub
     
    Private Sub MacroToDoTheWork(strSheetName As String)
        Application.DisplayAlerts = False
        With Worksheets(strSheetName)
            Application.Goto .Cells(1)
            Cells.Select
            Cells.Select
            Selection.Copy
            Selection.PasteSpecial Paste:=xlPasteValues
            
            
        End With
         
    End Sub

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

    Re: VBA to Loop through list of sheets and apply macro

    attach please 2 sample files for testing on local drive
    If solved remember to mark Thread as solved

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: VBA to Loop through list of sheets and apply macro

    Hi, hungryhobo,

    give this code a try (untested):
    Sub NewWeek_2()
        Dim mywb As Workbook
        Dim wb2 As Workbook
        Dim rngCell As Range
        Dim lngCounter As Long
        Dim lngLast As Long
        Dim ws As Worksheet
        
        Application.ScreenUpdating = False
        Set mywb = ActiveWorkbook
        Set ws = mywb.ActiveSheet
        lngLast = ws.Cells(Rows.Count, "A").End(xlUp).Row
        
        For lngCounter = 2 To lngLast
          Set wb2 = Workbooks.Open(Filename:="E:\archive\wb" & ws.Cells(lngCounter, "A").Value & ".xlsx")
          With wb2.ActiveSheet
              For Each rngCell In .Range("A1:A" & .Range("A" & .Rows.Count).End(xlUp).Row)
                  If Trim(rngCell.Value) <> vbNullString Then rngCell.Value = Trim(rngCell.Value)
              Next rngCell
          End With
          wb2.Close True
        Next lngCounter
        
        Application.Goto ws.Cells(1)
        Application.ScreenUpdating = True
        Set rngCell = Nothing
        Set wb2 = Nothing
        Set ws = Nothing
        Set mywb = Nothing
         
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  4. #4
    Registered User
    Join Date
    06-18-2012
    Location
    united states
    MS-Off Ver
    Excel 2003
    Posts
    19

    Re: VBA to Loop through list of sheets and apply macro

    Thanks Holger, your code doesn't loop through the sheets specified in column A. It just stays on the first sheet and doesn't do anything repeatedly.

  5. #5
    Registered User
    Join Date
    06-18-2012
    Location
    united states
    MS-Off Ver
    Excel 2003
    Posts
    19

    Re: VBA to Loop through list of sheets and apply macro

    I've attached two sample workbooks. After the macro comes to a stop, I need it to delete all sheets not listed in Column A, which is in the macrobook. Thanks for your help thus far guys.
    Attached Files Attached Files

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: VBA to Loop through list of sheets and apply macro

    Hi, hungryhobo,

    you correct about not looping the sheets in the opened workbook as I misread and wrote code to open several workbooks instead.

    Please give this a try:
    Sub NewWeek_3()
        Dim myWb As Workbook
        Dim wb2 As Workbook
        Dim rngCell As Range
        Dim lngCounter As Long
        Dim lngLast As Long
        Dim wsAct As Worksheet
        
        Application.ScreenUpdating = False
        Set myWb = ActiveWorkbook
        Set wsAct = myWb.ActiveSheet
        lngLast = wsAct.Cells(Rows.Count, "A").End(xlUp).Row
        
        Set wb2 = Workbooks.Open(Filename:="E:\archive\wb" & wsAct.Range("D2").Value & ".xlsx")
        For lngCounter = 2 To lngLast
          With wb2.Sheets(wsAct.Cells(lngCounter, "A").Value)
              For Each rngCell In .Range("A1:A" & .Range("A" & .Rows.Count).End(xlUp).Row)
                  If Trim(rngCell.Value) <> vbNullString Then rngCell.Value = Trim(rngCell.Value)
              Next rngCell
          End With
        Next lngCounter
        wb2.Close True
        
        Application.Goto wsAct.Cells(1)
        Application.ScreenUpdating = True
        Set rngCell = Nothing
        Set wb2 = Nothing
        Set wsAct = Nothing
        Set myWb = Nothing
         
    End Sub
    Please consider to use this instead of the loop
        For lngCounter = 2 To lngLast
          With wb2.Sheets(wsAct.Cells(lngCounter, "A").Value)
              With .Range("A1:A" & .Range("A" & .Rows.Count).End(xlUp).Row)
                  .Value = .Value
              End With
          End With
        Next lngCounter
    Ciao,
    Holger

  7. #7
    Registered User
    Join Date
    06-18-2012
    Location
    united states
    MS-Off Ver
    Excel 2003
    Posts
    19

    Re: VBA to Loop through list of sheets and apply macro

    Quote Originally Posted by HaHoBe View Post
    Hi, hungryhobo,

    you correct about not looping the sheets in the opened workbook as I misread and wrote code to open several workbooks instead.


    Holger
    Thanks for the code, how do I get it to delete sheets not in the list once the loop comes to a blank cell or end of list?
    Last edited by hungryhobo; 05-05-2013 at 11:24 PM.

  8. #8
    Registered User
    Join Date
    06-18-2012
    Location
    united states
    MS-Off Ver
    Excel 2003
    Posts
    19

    Re: VBA to Loop through list of sheets and apply macro

    Also your code is selecting column A of each sheet, I need it to select all cells in the sheet and paste values. Thanks so much...the code is nearly complete..

  9. #9
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: VBA to Loop through list of sheets and apply macro

    Hi, hungryhobo,

    only suggestion would be to have a look at the References at work where a broken link may be found if itīs a truted location and macros are enabled.

    Sorry I overread that and just had a look at your code which was showing code to work for Column A.

    Change
            With ws2
              For Each rngCell In .Range("A1:A" & .Range("A" & .Rows.Count).End(xlUp).Row)
                  If Trim(rngCell.Value) <> vbNullString Then rngCell.Value = Trim(rngCell.Value)
              Next rngCell
            End With
    to
            With ws2
              For Each rngCell In .UsedRange
                  If Trim(rngCell.Value) <> vbNullString Then rngCell.Value = Trim(rngCell.Value)
              Next rngCell
            End With
    Ciao,
    Holger

  10. #10
    Registered User
    Join Date
    06-18-2012
    Location
    united states
    MS-Off Ver
    Excel 2003
    Posts
    19

    Re: VBA to Loop through list of sheets and apply macro

    Thank you so much for your patience Holger! The macro works as intended at home! I'll try to figure out what happens at work.

    Best Regards

  11. #11
    Registered User
    Join Date
    06-18-2012
    Location
    united states
    MS-Off Ver
    Excel 2003
    Posts
    19

    Re: VBA to Loop through list of sheets and apply macro

    Quote Originally Posted by HaHoBe View Post

    With ws2
    For Each rngCell In .UsedRange
    "If Trim(rngCell.Value) <> vbNullString Then" rngCell.Value = Trim(rngCell.Value) ' The quoted part gives error
    Next rngCell
    End With




    I receive the following error message when testing it at work and all the cells in each sheet still have formulas and haven't been pasted over as values.

    run-time error '13':

    Type mismatch

  12. #12
    Registered User
    Join Date
    06-18-2012
    Location
    united states
    MS-Off Ver
    Excel 2003
    Posts
    19

    Re: VBA to Loop through list of sheets and apply macro

    Quote Originally Posted by HaHoBe View Post

    With ws2
    For Each rngCell In .UsedRange
    "If Trim(rngCell.Value) <> vbNullString Then" rngCell.Value = Trim(rngCell.Value) ' The quoted part gives error
    Next rngCell
    End With




    I receive the following error message when testing it at work and all the cells in each sheet still have formulas and haven't been pasted over as values.

    run-time error '13':

    Type mismatch

  13. #13
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: VBA to Loop through list of sheets and apply macro

    Hi, hungryhobo,

    as you may have noticed the code does what itīs supposed to do, and without having a look at your computer at work I think I wonīt be able to determine why the code doesnīt work there.

    Ciao,
    Holger

  14. #14
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA to Loop through list of sheets and apply macro


+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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