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: 10

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
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
 
Upvote 0

Forum statistics

Threads
1,214,615
Messages
6,120,538
Members
448,970
Latest member
kennimack

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