VBA code to move row to new worksheet based upon cell value - then delete

chefpolo

New Member
Joined
Jan 25, 2022
Messages
5
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
Platform
  1. Windows
Hi,

I need some help writing code that would move each row in worksheet overview to the corresponding worksheet based upon the value in column J and keep the column headers. The values in column J are the same as the worksheets. I then would need this row to delete.

Would it also be possible to include some sort of validation that would search the file for existing entries with the same name? For instance as shown I have entry for Marco Rossi - once I run the code and the row gets exported to closed. if I input a new Marco Rossi - can you have the file tell me there is already an entry for marco rossi in x worksheet?

See picture attached
 

Attachments

  • HR Tracker - Sample Picture.JPG
    HR Tracker - Sample Picture.JPG
    180.6 KB · Views: 15

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
I need some help writing code that would move each row in worksheet overview to the corresponding worksheet based upon the value in column J and keep the column headers. The values in column J are the same as the worksheets.
According to your image it should be column K

For the above, put the following code in a module.
VBA Code:
Sub create_worksheets()
  Dim c As Range, sh As Worksheet, ky As Variant
  
  Application.ScreenUpdating = False
  Set sh = Sheets("Overview")
  With CreateObject("scripting.dictionary")
    For Each c In sh.Range("K2", sh.Range("K" & Rows.Count).End(xlUp))
      If c.Value <> "" Then .Item(c.Value) = Empty
    Next c
    For Each ky In .Keys
      If Evaluate("ISREF('" & ky & "'!A1)") Then
        sh.Range("A1").AutoFilter Columns("K").Column, ky
        sh.AutoFilter.Range.Offset(1).EntireRow.Copy Sheets(ky).Range("A" & Rows.Count).End(3)(2)
        sh.AutoFilter.Range.Offset(1).EntireRow.Delete
      End If
    Next ky
  End With
  If sh.AutoFilterMode Then sh.AutoFilterMode = False
End Sub

---
Would it also be possible to include some sort of validation that would search the file for existing entries with the same name?
For that put the following code in the events of the sheet "Overview"

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim rng As Range, c As Range, f As Range
  Dim sh As Worksheet
  Dim fname As String
  
  Set rng = Intersect(Target, Range("B2:C" & Rows.Count))
  If Not rng Is Nothing Then
    For Each c In rng
      fname = Range("D" & c.Row).Value
      If fname <> "" Then
        For Each sh In Sheets
          If sh.Name <> ActiveSheet.Name Then
            Set f = sh.Range("D:D").Find(fname, , xlValues, xlWhole, , , False)
            If Not f Is Nothing Then
              MsgBox "There is already an entry for " & fname & " in " & sh.Name & " worksheet"
              c.Select
              Exit Sub
            End If
          End If
        Next
      End If
    Next
  End If
End Sub

SHEET EVENT
Right click the tab of the sheet (overview sheet) you want this to work, select view code and paste the code into the window that opens up.
 
Upvote 0
thank you so much - i did the following but for some reason the debugger comes up. Im getting the following: Run-time error '1004': Application-defined or object defined error

See image for code:
 

Attachments

  • HR debugger picture.JPG
    HR debugger picture.JPG
    68 KB · Views: 9
Upvote 0
Something different in your data or in your sheets, from what is shown in your image?
Cells with error, hidden or protected sheets?
 
Upvote 0
Nothing hidden or protected. Column D does have concat function and Column N has a simple subtraction function. and data validation in columns F and K.
 
Upvote 0
HR Tracker - Sample.xlsm
ABCDEFGHIJKLMN
1LOCATIONFIRSTLAST NAMEFull NameDEPTACTIONDATESHORT SUMMARY NOTESNEXT STEPS (if any)NEXT STEP DUE DATE (if any)StatusQUICK NOTES (if any)Date of closedTotal amount of time
2EDTMarcoRossiMarco RossiA1/1/2022blablablabla1/5/2022In progress (HR)1/10/20229.00
3ECHJohnDoeJohn DoeA1/1/2022blablablabla1/5/2022Closed1/10/20229.00
4EDAMaryDoeMary DoeA1/1/2022blablablabla1/5/2022Action from 3rd party needed1/10/20229.00
5ELASusanDoeSusan DoeA1/1/2022blablablabla1/5/2022In progress (HR)1/10/20229.00
6
7
8
9
10
11
12
13
14
15
16
17
18
Overview
Cell Formulas
RangeFormula
N2:N5N2=M2-G2
Cells with Data Validation
CellAllowCriteria
F2:F18List='data validation'!$A$1:$A$4
K2:K18List='data validation'!$B$1:$B$3
 
Upvote 0
I did the test with your data and I have no error.
Run the following macro and type whatever appears in the message.

VBA Code:
Sub create_worksheets()
  Dim c As Range, sh As Worksheet, ky As Variant
  
  Application.ScreenUpdating = False
  Set sh = Sheets("Overview")
  With CreateObject("scripting.dictionary")
    For Each c In sh.Range("K2", sh.Range("K" & Rows.Count).End(xlUp))
      If c.Value <> "" Then .Item(c.Value) = Empty
    Next c
    For Each ky In .Keys
      If Evaluate("ISREF('" & ky & "'!A1)") Then
        sh.Range("A1").AutoFilter Columns("K").Column, ky
        MsgBox "ky " & ky
        sh.AutoFilter.Range.Offset(1).EntireRow.Copy Sheets(ky).Range("A" & Rows.Count).End(3)(2)
        sh.AutoFilter.Range.Offset(1).EntireRow.Delete
      End If
    Next ky
  End With
  If sh.AutoFilterMode Then sh.AutoFilterMode = False
End Sub
 
Upvote 0
Still getting the error on this line of code

sh.AutoFilter.Range.Offset(1).EntireRow.Copy Sheets(ky).Range("A" & Rows.Count).End(3)(2)
 
Upvote 0
But before the error, a msgbox appears, tell me what the msgbox says.
---
Some data was copied or nothing?
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

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