Automatically move rows to another tab based on cell value

excel_newbie12

New Member
Joined
Apr 27, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello! I've seen posts that have provided similar help but I can't seem to make the codes provided work for me.

I'm looking to move information from the Master tab of my spreadsheet into different tabs based on Previous Company (if Column D has 'Google' then that row flows to the Google tab). I'd like to keep all rows in the Master tab.

Thank you for any help you can provide!
 

Attachments

  • VBA.png
    VBA.png
    19.7 KB · Views: 7

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Trebor76

Well-known Member
Joined
Jul 23, 2007
Messages
5,003
Hi excel_newbie12,

Welcome to MrExcel!!

Try this:

VBA Code:
Option Explicit
Sub Macro1()

    Dim wsSrc As Worksheet, wsDestin As Worksheet
    Dim lngRowTo As Long, lngRow As Long, lngRowPaste As Long
    Dim strDestinTab As String
   
    Application.ScreenUpdating = False
   
    Set wsSrc = ThisWorkbook.Sheets("Master")
    lngRowTo = wsSrc.Range("A:D").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
   
    For lngRow = 2 To lngRowTo
        strDestinTab = Trim(wsSrc.Range("B" & lngRow))
        On Error Resume Next
            Set wsDestin = ThisWorkbook.Sheets(strDestinTab)
            If Err.Number <> 0 Then
                If wsDestin Is Nothing Then
                    'Create tab if necessary
                    ThisWorkbook.Worksheets.Add After:=Sheets(ThisWorkbook.Sheets.Count)
                    ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count).Name = strDestinTab
                    Set wsDestin = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
                    wsSrc.Rows(1).Copy Destination:=wsDestin.Rows(1)
                End If
            End If
            lngRowPaste = wsDestin.Range("A:D").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
            wsSrc.Range("A" & lngRow & ":D" & lngRow).Copy Destination:=wsDestin.Range("A" & lngRowPaste)
        On Error GoTo 0
        Set wsDestin = Nothing
    Next lngRow
   
    Application.ScreenUpdating = True
   
    MsgBox "Records have now been copied to their respective tabs.", vbInformation

End Sub

Regards,

Robert
 

Forum statistics

Threads
1,176,119
Messages
5,901,483
Members
434,896
Latest member
Derquila

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
Top