VBA Copy, Move and Rename based on Cell Value

unknownymous

Board Regular
Joined
Sep 19, 2017
Messages
249
Office Version
  1. 2016
Platform
  1. Windows
Hi Guys,

I have below data on a sheet with tab name "Data"

DateNUMNAMETYPEDATESubjectNote 1Note 2
10/1/2000​
1​
BenStudent-Math--
10/1/2000​
2​
AnnStudent-Science--
10/1/2000​
2​
CherStudent-Math--
11/1/2000​
2​
JoyStudent-Science--
11/1/2000​
2​
SamStudent-History--
11/1/2000​
2​
KenStudent-History--
12/2/2000​
2​
KarlStudent-Math--
12/2/2000​
2​
SenStudent-Science--
12/2/2000​
2​
LenStudent-Math--

I wanted to create a macro where it will copy the data on another tab based on the date in column A as well as the header for all tabs.

So for this example, a new tab will be created for 10/1/2000 with a tab name "10.1.2000" since we can't use "/" in tab name.

RUN DATENUMNAMETYPEDATESubjectNote 1Note 2
10/1/2000​
1​
BenStudent-Math--
10/1/2000​
2​
AnnStudent-Science--
10/1/2000​
2​
CherStudent-Math--

Another tab sheet will be created for 11/1/2000 (same case with 12/02/2000) with tab name 11.1.2000 and 12.02.2000

Tab "11.1.2000"

RUN DATENUMNAMETYPEDATESubjectNote 1Note 2
11/1/2000​
2​
JoyStudent-Science--
11/1/2000​
2​
SamStudent-History--
11/1/2000​
2​
KenStudent-History--

Tab "12.2.2000"

RUN DATENUMNAMETYPEDATESubjectNote 1Note 2
12/2/2000​
2​
KarlStudent-Math--
12/2/2000​
2​
SenStudent-Science--
12/2/2000​
2​
LenStudent-Math--

Any help will be much appreciated. :)
 

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.
Try:
VBA Code:
Sub CreateSheets()
    Application.ScreenUpdating = False
    Dim LastRow As Long, rDate As Range, rngList As Object, key As Variant, srcWS As Worksheet
    Set srcWS = Sheets("Data")
    LastRow = srcWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Set rngList = CreateObject("Scripting.Dictionary")
    For Each rDate In srcWS.Range("A2:A" & LastRow)
        If Not rngList.Exists(rDate.Value) Then
            rngList.Add rDate.Value, Nothing
        End If
    Next rDate
    For Each key In rngList
        With srcWS.Cells(1, 1).CurrentRegion
            .AutoFilter 1, key
        End With
        srcWS.UsedRange.SpecialCells(xlCellTypeVisible).Copy
        Worksheets.Add(After:=Sheets(Sheets.Count)).Name = Replace(key, "/", ".")
        Cells(1, 1).PasteSpecial
    Next key
    Application.CutCopyMode = False
    srcWS.Range("A1").AutoFilter
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try:
VBA Code:
Sub CreateSheets()
    Application.ScreenUpdating = False
    Dim LastRow As Long, rDate As Range, rngList As Object, key As Variant, srcWS As Worksheet
    Set srcWS = Sheets("Data")
    LastRow = srcWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Set rngList = CreateObject("Scripting.Dictionary")
    For Each rDate In srcWS.Range("A2:A" & LastRow)
        If Not rngList.Exists(rDate.Value) Then
            rngList.Add rDate.Value, Nothing
        End If
    Next rDate
    For Each key In rngList
        With srcWS.Cells(1, 1).CurrentRegion
            .AutoFilter 1, key
        End With
        srcWS.UsedRange.SpecialCells(xlCellTypeVisible).Copy
        Worksheets.Add(After:=Sheets(Sheets.Count)).Name = Replace(key, "/", ".")
        Cells(1, 1).PasteSpecial
    Next key
    Application.CutCopyMode = False
    srcWS.Range("A1").AutoFilter
    Application.ScreenUpdating = True
End Sub

This is is awesome! Thank you :)
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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