Loop through sheets problem

bujaman

Board Regular
Joined
Apr 2, 2009
Messages
56
I am trying to loop through all the sheets in my workbook and rearrange some data. So far I have been unsucessful for some reason. I think I have the loop and everything set up correctly, but the macro gets hung up on one sheet and executes the code over and over again on the same sheet without looping. Here is what I have:

Code:
Sub DBOrderIt()Application.ScreenUpdating = False


Dim TblRng As Range
Dim topcell As Range
Dim bottcell As Range
Dim ws As Worksheet


For Each ws In Worksheets


Columns("B:B").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
Selection.Cut Destination:=Range("B2")
Range("B2").Select
Selection.AutoFill Destination:=Range("B2:B" & Range("A65536").End(xlUp).Row)
Range("B2:B" & Range("A65536").End(xlUp).Row).Select




Set TblRng = Range("D1:X1")


For Each cell In TblRng
On Error Resume Next


Range(cell.Offset(1, 0), cell.Offset(Range("A65536").End(xlUp).Row, 0)).Copy
Range("C" & Range("C65536").End(xlUp).Row + 1).Select
ActiveSheet.Paste
cell.Copy
Range("B" & Range("B65536").End(xlUp).Row + 1).Select
ActiveSheet.Paste
'AutoFill Here
Selection.AutoFill Destination:=Range("B" & (Range("B65536").End(xlUp).Row) & ":B" & (Range("C65536").End(xlUp).Row))




Next cell




Next ws
Application.ScreenUpdating = True


End Sub

I really can't figure out why it won't loop through the sheets, so any help would be awesome, thanks!
 
Hi,

As the code loops through all sheets i think you should qualify all the ranges. Something like (UNTESTED)

Code:
.....
For each ws in Worksheets
[COLOR=#ff0000]With wk[/COLOR]
    [COLOR=#ff0000].[/COLOR]Columns("B:B").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
   [COLOR=#ff0000].[/COLOR]Range("C1").Select
   Selection.Cut Destination:=[COLOR=#ff0000].[/COLOR][COLOR=#000000]Ra[/COLOR]nge("B2")
  'and so on
   ....
   ....
[COLOR=#ff0000]End with[/COLOR]
Next ws

M.
 
Upvote 0

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