Last sheet does not follow the code

ukanez

New Member
Joined
Dec 7, 2017
Messages
21
Hi everyone, I have a workbook that has 12 worksheets for each province. I'm trying to select a row based on a value of a cell, cut that row, and insert it before another row lower in the worksheet.

I'm trying to do this for each worksheet in the workbook and have been having problems because the code executes with no errors, but the final sheet in the workbook remains unchanged. Thanks again for your help.

Code:
Application.ScreenUpdating = False
Dim rng1, cell, cella As Range
Dim ws As Worksheet

Workbooks("Provincial - INTERNAL" & ".xls").Activate

     Sheets(Array("BC", "AB", "SK", "MB", "Prairies", "ON", "QC", "NB", "PE", "NS", "NF", "Atlantic")).Copy
  
 For Each ws In ActiveWorkbook.Worksheets
     Set rngA = Range("C1", Range("C65536").End(xlUp))
         For Each cell In rngA
             If cell.Value = "A" Then
                 cell.EntireRow.Cut
             End If
         Next cell
         For Each cella In rngA
             If cella.Value = "B" Then
                 cella.EntireRow.Insert Shift:=xlDown
             End If
         Next
 With ws.UsedRange
     .Value = .Value
  
 End With
     Application.Goto ws.Cells(1, 1)
 Next
 
As you only have one A & one B, lets try a different tack.
Code:
Dim FndA As Range
Dim FndB As Range
Dim ws As Worksheet

Workbooks("Provincial - INTERNAL" & ".xls").Activate

     Sheets(Array("BC", "AB", "SK", "MB", "Prairies", "ON", "QC", "NB", "PE", "NS", "NF", "Atlantic")).Copy
  
 For Each ws In ActiveWorkbook.Worksheets
   Set FndA = ws.Range("C:C").Find("A", , , xlWhole, , , False, , False)
   Set FndB = ws.Range("C:C").Find("B", , , xlWhole, , , False, , False)
   FndA.EntireRow.Copy
   FndB.EntireRow.Insert
   FndA.EntireRow.Delete
   With ws.UsedRange
       .Value = .Value
   End With
   Application.Goto ws.Cells(1, 1)
 Next ws
So the code as is results in #REF errors in the Prairies and Atlantic sheets, I don't know why but I tried using Cut and Insert instead of Copy, Insert, and Delete and it seemed to work! Thank you so much!
 
Upvote 0

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Glad it's sorted & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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