Macro help! Move a row of data to separate sheet based off the value in column M and delete from original sheet

kaneL2019

New Member
Joined
Jun 12, 2023
Messages
1
Office Version
  1. 2021
Platform
  1. Windows
New to VBA. Thanks ahead of time for any help.

I have a workbook with one sheet. Data from columns A to X. What I'm looking for is a macro that will move a row of data to separate sheet based off the value in column M and delete from original sheet.

Example: If "Person" in M:M, create sheet named "Person" and cut and paste row into that sheet. If "Company" in M:M, create sheet named "Company" and cut and paste row into that sheet. If "Fin Company" in M:M, create sheet named "Fin company" and cut and paste row into that sheet. If none of those things leave row(s) on Sheet1. Thanks again.

1686586409291.png
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hi @kaneL2019
Welcome to the MrExcel forum. Please accept my warmest greetings and sincere hope that all is well.

Set the following in the macro.

1. Put in this line of the macro the name of your sheet that contains the information.
Set sh = Sheets("Data")​

2. If you want to copy the header to the new sheets, then change this line:
sh.AutoFilter.Range.Offset(1).Copy Range("A1")​

By this line:
sh.AutoFilter.Range.Copy Range("A1")​

---------------​
Macro:
VBA Code:
Sub CopyDataToSheets()
  Dim sh As Worksheet
  Dim ary As Variant
  Dim lr As Long
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  
  Set sh = Sheets("Data")
  For Each ary In Array("Person", "Company", "Fin Company")
    sh.Range("A:M").AutoFilter 13, ary
    lr = sh.Range("F" & Rows.Count).End(3).Row
    If lr > 1 Then
      On Error Resume Next: Sheets(ary).Delete: On Error GoTo 0
      Sheets.Add(, Sheets(Sheets.Count)).Name = ary
      sh.AutoFilter.Range.Offset(1).Copy Range("A1")
      sh.AutoFilter.Range.Offset(1).Delete
    End If
  Next
  sh.Select
  If sh.AutoFilterMode Then sh.AutoFilterMode = False
  
  Application.DisplayAlerts = True
  Application.ScreenUpdating = True
End Sub


--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,180
Members
453,021
Latest member
Justyna P

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