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
 
Will there be any blank cells in col C other than the cut rows?
Also are all the cut rows being placed in the correct location?
Yes, the sheets have spaces within them so there are other blank cells in Col C. Also yes, the cut rows are all being placed in the correct location. The last sheet however has the "cut row" in both its original location and its intended location.
 
Last edited:
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Will you only have one cell with A & one with B per sheet, or could there be multiple instances per sheet?
 
Upvote 0
Ok, how about
Code:
Application.ScreenUpdating = False
Dim i As Long
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
   For i = ws.Range("C" & Rows.Count).End(xlUp).Row To 1 Step -1
      If ws.Range("C" & i).Value = "A" Then
         ws.Rows(i).Cut
         Set Rng = ws.Rows(i)
      ElseIf ws.Range("C" & i).Value = "B" Then
         ws.Rows(i).Insert xlDown
         Rng.Delete
         Exit For
      End If
   Next i
   Set Rng = Nothing
   With ws.UsedRange
       .Value = .Value
   End With
   Application.Goto ws.Cells(1, 1)
 Next ws
 
Last edited:
Upvote 0
Code:
Application.ScreenUpdating = False
Dim i As Long
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
   For i = ws.Range("C" & Rows.Count).End(xlUp).Row To 1 Step -1
      If ws.Range("C" & i).Value = "A" Then
         ws.Rows(i).Cut
         Set Rng = ws.Rows(i)
      ElseIf ws.Range("C" & i).Value = "B" Then
         ws.Rows(i).Insert xlDown
[U]         Rng.Delete[/U]
         Exit For
      End If
   Next i
   Set Rng = Nothing
   With ws.UsedRange
       .Value = .Value
   End With
   Application.Goto ws.Cells(1, 1)
 Next ws
Seem to run into an Object Required error at the underlined rng.delete
 
Upvote 0
Is the cell containing "B" before the cell containing "A"?
 
Upvote 0
Ok, make this change
Code:
   For i = 1 To ws.Range("C" & Rows.Count).End(xlUp).Row
 
Upvote 0
Ok, make this change
Code:
   For i = 1 To ws.Range("C" & Rows.Count).End(xlUp).Row
The code runs without errors, but only cuts out row with cell "A" and does not insert that row before the row with cell "B"

edit: I should clarify and say that the row with cell "A" is gone from its original location and is nowhere else in the sheet.
 
Last edited:
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,216,733
Messages
6,132,410
Members
449,727
Latest member
Aby2024

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