SOLVED

Moving Rows to different sheets based on future dates.

%3CLINGO-SUB%20id%3D%22lingo-sub-2646721%22%20slang%3D%22en-US%22%3EMoving%20Rows%20to%20different%20sheets%20based%20on%20future%20dates.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2646721%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20created%20a%20form%20for%20several%20people%20to%20enter%20scheduling%20requests.%26nbsp%3B%20I%20need%20a%20macro%20to%20move%20rows%20from%20the%20form%20sheet%20to%20different%20sheets%20based%20on%20the%20date%20of%20the%20schedule%20request%20without%20needing%20to%20cut%20and%20paste%20each%20individual%20row%20as%20they%20come%20in.%26nbsp%3B%20I%20recorded%20a%20macro%20to%20move%20each%20line%20but%20I%20can't%20figure%20out%20how%20to%20make%20it%20on%20going%20and%20based%20on%20the%20date.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2646721%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2646777%22%20slang%3D%22en-US%22%3ERe%3A%20Moving%20Rows%20to%20different%20sheets%20based%20on%20future%20dates.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2646777%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1122198%22%20target%3D%22_blank%22%3E%40Michelle365%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETry%20this%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3ESub%20MoveRows()%0A%20%20%20%20Dim%20ws%20As%20Worksheet%0A%20%20%20%20Dim%20wt%20As%20Worksheet%0A%20%20%20%20Dim%20tbl%20As%20ListObject%0A%20%20%20%20Dim%20n%20As%20String%0A%20%20%20%20Dim%20r%20As%20ListRow%0A%20%20%20%20Dim%20m%20As%20Long%0A%20%20%20%20Dim%20t%20As%20Long%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%20%20%20%20Set%20ws%20%3D%20Worksheets(%22Form1%22)%0A%20%20%20%20Set%20tbl%20%3D%20ws.ListObjects(1)%0A%20%20%20%20For%20Each%20r%20In%20tbl.ListRows%0A%20%20%20%20%20%20%20%20n%20%3D%20Format(r.Range(1%2C%201).Value%2C%20%22dddd%2C%20mmmm%20d%2C%20yyyy%22)%0A%20%20%20%20%20%20%20%20Set%20wt%20%3D%20Nothing%0A%20%20%20%20%20%20%20%20On%20Error%20Resume%20Next%0A%20%20%20%20%20%20%20%20Set%20wt%20%3D%20Worksheets(n)%0A%20%20%20%20%20%20%20%20On%20Error%20GoTo%200%0A%20%20%20%20%20%20%20%20If%20wt%20Is%20Nothing%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20Set%20wt%20%3D%20Worksheets.Add(After%3A%3DWorksheets(Worksheets.Count))%0A%20%20%20%20%20%20%20%20%20%20%20%20wt.Name%20%3D%20n%0A%20%20%20%20%20%20%20%20%20%20%20%20tbl.HeaderRowRange.Copy%20Destination%3A%3Dwt.Range(%22A1%22)%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20%20%20%20%20t%20%3D%20wt.Range(%22A%22%20%26amp%3B%20wt.Rows.Count).End(xlUp).Row%20%2B%201%0A%20%20%20%20%20%20%20%20r.Range.Copy%20Destination%3A%3Dwt.Range(%22A%22%20%26amp%3B%20t)%0A%20%20%20%20Next%20r%0A%20%20%20%20tbl.DataBodyRange.Delete%0A%20%20%20%20Application.CutCopyMode%20%3D%20False%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2646780%22%20slang%3D%22en-US%22%3ERe%3A%20Moving%20Rows%20to%20different%20sheets%20based%20on%20future%20dates.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2646780%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1122198%22%20target%3D%22_blank%22%3E%40Michelle365%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20are%20of%20course%20free%20to%20design%20this%20as%20you%20wish.%20But%20allow%20me%2C%20if%20you%20don't%20mind%2C%20to%20present%20a%20macro-free%20alternative.%20Excel%20over%20the%20last%20year%20or%20so%20introduced%20several%20new%20%22Dynamic%20Array%22%20functions.%20FILTER%20is%20one%20of%20them.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBy%20using%20FILTER%2C%20in%20the%20formula%20that%20appears%20below%20(and%20is%20in%20the%20attached%20revised%20version%20of%20your%20spreadsheet)%2C%20you%20can%20just%20enter%20a%20date%20and%20immediately%20see%20all%20of%20the%20entries%20from%20your%20form%20that%20pertain%20to%20that%20date.%20If%20you%20want%20to%20sort%20them%2C%20you%20could%20do%20so.%20In%20fact%2C%20you%20could%20readily%20create%20a%20more%20sophisticated%20version%20of%20the%20formula%20to%20show%20all%20of%20the%20tasks%20for%20the%20next%20three%20days.%20Here's%20the%20basic%20formula%20that%20would%20enable%20you%20to%20have%20just%20two%20spreadsheets%20in%20the%20workbook%2C%20one%20being%20your%20form%2C%20the%20other%20this%20spreadsheet.%3C%2FP%3E%3CP%3E%3CSTRONG%3E%3DFILTER(Table1%2CTable1%5BSchedule%20Date%5D%3D'FILTERED%20dates'!A2%2C%22No%20tasks%20scheduled%22)%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3EBy%20the%20way%2C%20one%20feature%20of%20Dynamic%20Array%20formulas%20is%20that%20they%20are%20only%20entered%20in%20the%20upper%20left%20cell%20of%20the%20target%20destination.%20They%20fill%2C%20below%20and%20to%20the%20right%2C%20with%20all%20relevant%20data.%20So%20this%20formula%20is%20only%20entered%20in%20cell%20C2.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFILTER%20does%20require%20the%20most%20recent%20version%20of%20Excel.%20And%20to%20set%20your%20mind%20at%20ease%2C%20I%20kept%20your%20macro-enabled%20spreadsheet%20intact%2C%20so%20it's%20still%20all%20there.%20Just%20wanted%20to%20demonstrate%20how%20the%20desired%20functionality%20could%20be%20achieved%20without%20needing%20the%20macro.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2646885%22%20slang%3D%22en-US%22%3ERe%3A%20Moving%20Rows%20to%20different%20sheets%20based%20on%20future%20dates.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2646885%22%20slang%3D%22en-US%22%3EI%20appreciate%20that%2C%20but%20there%20are%20just%20too%20many%20hands%20in%20the%20pot%20for%20that.%20We%20need%20it%20to%20be%20idiot%20proof%20for%20others%20to%20come%20in%20and%20make%20edits%20to%20each%20sheet%20as%20well.%20Moving%20things%20to%20a%20new%20sheet%20is%20what%20others%20involved%20would%20like.%20Thanks.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2664801%22%20slang%3D%22en-US%22%3ERe%3A%20Moving%20Rows%20to%20different%20sheets%20based%20on%20future%20dates.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2664801%22%20slang%3D%22en-US%22%3EThanks%20Hans%2C%20this%20is%20working%20perfectly.%3C%2FLINGO-BODY%3E
New Contributor

I have created a form for several people to enter scheduling requests.  I need a macro to move rows from the form sheet to different sheets based on the date of the schedule request without needing to cut and paste each individual row as they come in.  I recorded a macro to move each line but I can't figure out how to make it on going and based on the date.

5 Replies
best response confirmed by Michelle365 (New Contributor)
Solution

@Michelle365 

Try this:

Sub MoveRows()
    Dim ws As Worksheet
    Dim wt As Worksheet
    Dim tbl As ListObject
    Dim n As String
    Dim r As ListRow
    Dim m As Long
    Dim t As Long
    Application.ScreenUpdating = False
    Set ws = Worksheets("Form1")
    Set tbl = ws.ListObjects(1)
    For Each r In tbl.ListRows
        n = Format(r.Range(1, 1).Value, "dddd, mmmm d, yyyy")
        Set wt = Nothing
        On Error Resume Next
        Set wt = Worksheets(n)
        On Error GoTo 0
        If wt Is Nothing Then
            Set wt = Worksheets.Add(After:=Worksheets(Worksheets.Count))
            wt.Name = n
            tbl.HeaderRowRange.Copy Destination:=wt.Range("A1")
        End If
        t = wt.Range("A" & wt.Rows.Count).End(xlUp).Row + 1
        r.Range.Copy Destination:=wt.Range("A" & t)
    Next r
    tbl.DataBodyRange.Delete
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
End Sub

@Michelle365 

 

You are of course free to design this as you wish. But allow me, if you don't mind, to present a macro-free alternative. Excel over the last year or so introduced several new "Dynamic Array" functions. FILTER is one of them. 

 

By using FILTER, in the formula that appears below (and is in the attached revised version of your spreadsheet), you can just enter a date and immediately see all of the entries from your form that pertain to that date. If you want to sort them, you could do so. In fact, you could readily create a more sophisticated version of the formula to show all of the tasks for the next three days. Here's the basic formula that would enable you to have just two spreadsheets in the workbook, one being your form, the other this spreadsheet.

=FILTER(Table1,Table1[Schedule Date]='FILTERED dates'!A2,"No tasks scheduled")

By the way, one feature of Dynamic Array formulas is that they are only entered in the upper left cell of the target destination. They fill, below and to the right, with all relevant data. So this formula is only entered in cell C2.

 

FILTER does require the most recent version of Excel. And to set your mind at ease, I kept your macro-enabled spreadsheet intact, so it's still all there. Just wanted to demonstrate how the desired functionality could be achieved without needing the macro.

I appreciate that, but there are just too many hands in the pot for that. We need it to be idiot proof for others to come in and make edits to each sheet as well. Moving things to a new sheet is what others involved would like. Thanks.

@Michelle365 

 

Well, I wish you well. What you're describing would raise red flags in my mind. i spend many years as the director of  a major database project for a major corporation. One of the key goals in database design--which is what  you have--is something called "data integrity." One of the best ways to weaken or, as the worst case, destroy data integrity is to have what is supposed to be the same entry--e.g., an address--appear in multiple places with the ability to edit it one place but remain as it was in the other(s). 

 

The more idiot proof approach, frankly (IMHO), is to allow edits only in the master database.

Thanks Hans, this is working perfectly.