Moves rows to sheets...

tmskipper86

New Member
Joined
Jun 16, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I am trying to move items from the "TR Retail - 2022 - GLTrans" data sheet to various cells. I want to find all the matching rows based on column D "Branch". Then move to corresponding sheet witht the name. I attached images.
 

Attachments

  • 20220907_204619.jpg
    20220907_204619.jpg
    79.9 KB · Views: 7
  • 20220907_204607.jpg
    20220907_204607.jpg
    152.6 KB · Views: 7

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.
Try this:

VBA Code:
Sub move2Shts()
'https://www.mrexcel.com/board/threads/moves-rows-to-sheets.1215896/
Dim CellValue As Range
Dim CUnique As New Collection
Dim VUnique As Variant
Dim ws As Worksheet
Dim LastRow As Long
Dim DestRows As Long

LastRow = Sheets("TR Retail - 2022 - GLTrans").Range("D" & Rows.Count).End(xlUp).Row

'get list of unique value in D. You can't have two items with the same name in a collection,
'so when it errors due to a duplicate name, we just go to the next value
On Error Resume Next

'Adding unique items to collection from defined range
For Each CellValue In Range("D2:D" & LastRow)
    CUnique.Add CellValue.Value, CStr(CellValue.Value)
Next

On Error GoTo 0

With ThisWorkbook.Sheets("TR Retail - 2022 - GLTrans")
    'look at each unique value in D
    For Each VUnique In CUnique
        
        'make sure a worksheet with that name exists.
        On Error Resume Next
        Set ws = ThisWorkbook.Sheets(VUnique)
        On Error GoTo 0
        'if not, make one
        If ws Is Nothing Then
            Worksheets.Add.Name = VUnique
            Set ws = ThisWorkbook.Sheets(VUnique)
        End If
        
        'filter the list by the Branch
        .Range("D1").AutoFilter Field:=1, Criteria1:=VUnique
        'copy only the filtered rows
        .Range("A2:H" & LastRow).SpecialCells(xlCellTypeVisible).Copy 'change H to whatever your last column is.
        'find the last row on the destination sheet
        DestRows = ws.Range("D" & Rows.Count).End(xlUp).Row
        'paste to the destination sheet
        ws.Cells(DestRows + 1, 1).PasteSpecial
    Next
    'delete everything you've copied
    .Range("A2:H" & LastRow).EntireRow.Delete 'change H to whatever your last column is.
End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,388
Messages
6,119,229
Members
448,879
Latest member
VanGirl

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