Loop Through Multiple Sheets in a Workbook, Excluding a selected few, and make the same change to each.

RadheyaMansel

New Member
Joined
Oct 25, 2017
Messages
6
Hi guys,


This is a very simple code, but I'm not sure why it isn't working.

I want to loop through each worksheet in a workbook and make the same change to each: Copy a range and paste the figures into the first available empty row starting in Column C.

At the moment, when I play this Macro there aren't any errors, it just loads for about a minute but when I check the sheets after it's finished, nothing has happened.

Note: I have a macro that just selects all the sheets as a group, and makes the change, but the problem is that it doesn't past the value in the last available row if they are different for each sheet, it seems to find the furthest down free row and then paste into that for all of the sheets.


A little help would be great,

Thank you!

Code:
   Sub WorksheetLoop()


  Application.ScreenUpdating = False
 
         ' Declare Current as a worksheet object variable.
         Dim wks As Worksheet
         Dim Loc As Long


         ' Loop through all of the worksheets in the active workbook.
         For Each wks In Worksheets
         
         
         'Exclude the named sheets below, coupled with End IF
        
          If wks.Name <> "Ing_Index" And wks.Name <> "Daily Production" And wks.Name <> "StoreToDecanting" Then
              
              
          
       Range("H20:J20").Select
       Selection.Copy
    
       Loc = Range("C" & Rows.Count).End(xlUp).Row + 1
        Range("C" & Loc).PasteSpecial xlPasteValues
    


           
         
          End If
      Next wks


      Application.ScreenUpdating = True






      End Sub
[\Code]
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
The problem is that you aren't qualifying your range references.
Try

Code:
For Each wks In Worksheets

    If wks.Name <> "Ing_Index" And wks.Name <> "Daily Production" And wks.Name <> "StoreToDecanting" Then
       
        wks.Range("C" & Rows.Count).End(xlUp).Offset(1,0).Resize(1,3).Value = wks.Range("H20:J20").Value

    End If
Next wks
 
Upvote 0
Thank you mikerickson, it worked! It still takes about a minute to complete, is that simply down to processing power, and the amount of sheets (150) that it has to loop through? or is there any way I could clean up the code?

Thanks again,
Radheya
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top