VBA Code to Seperate a Column into Worksheets By Date Column
I have a list of data that has a specific date value for each row, and I want to separate the data by date using VBA. I would like to create a new worksheet for each date found and paste the ENTIRE ROW to the created worksheet based on date. I know this is going to create a lot of worksheets, but that's ok -- because the next step is analyzing the time column to break the data down further into Day/Night based on time.
Re: VBA Code to Seperate a Column into Worksheets By Date Column
Sub J3v16()
Dim Data, Dict As Object, i As Long, Sht As String
Application.ScreenUpdating = False
Set Dict = CreateObject("Scripting.Dictionary")
With Sheets("Sheet1").Cells(1).CurrentRegion
Data = .Value
For i = 2 To UBound(Data)
If Not Dict.exists(Data(i, 2)) Then
Dict.Add Data(i, 2), Nothing
Sht = Format(Data(i, 2), "DDMMYYYY") '! Sheet names cannot contain "/" Character
.AutoFilter 2, CStr(Data(i, 2))
If Evaluate("ISREF('" & Data(i, 2) & "'!A1)") = False Then Sheets.Add(, Sheets(Sheets.Count)).Name = Sht
.Columns("A:B").Copy
With Sheets("" & Sht & "")
'.UsedRange.Delete ' ! If recurring code execution...Uncomment this snippet
.Range("A" & Rows.Count).End(xlUp).PasteSpecial xlPasteAll
.Columns.AutoFit
.UsedRange.Borders.Weight = 2
End With
.AutoFilter
End If
Next i
End With
Application.ScreenUpdating = True
End Sub
Good Luck...
I don't presume to know what I am doing, however, just like you, I too started somewhere...
One-day, One-problem at a time!!!
If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
Also....Add a comment if you like!!!!
And remember...Mark Thread as Solved...
Excel Forum Rocks!!!
According to your attachment a VBA demonstration as a beginner starter to paste to the Sheet13 (Sheet1) worksheet module :
PHP Code:
Sub Demo1()
Dim V, W, S$
Application.ScreenUpdating = False
With [A1].CurrentRegion
.Columns(2).AdvancedFilter 2, , [K1], True
V = [K1].CurrentRegion.Rows.Count - 1
V = Application.Small([K2].Resize(V).Value2, Evaluate("ROW(1:" & V & ")"))
For Each W In V
[K2].Value2 = W: S = Format$(W, "mm-dd-yyyy")
If Evaluate("ISREF('" & S & "'!A1)") Then Sheets(S).UsedRange.Clear Else Sheets.Add(, Sheets(Sheets.Count)).Name = S
.AdvancedFilter 2, [K1:K2], Sheets(S).[A1]
Next
End With
[K1].CurrentRegion.Clear
Application.ScreenUpdating = True
End Sub
► Do you like it ? ► ► So thanks to click on bottom left star icon « ★ Add Reputation » ! ◄ ◄
Both solutions work great. One question for Marc L --- How can I get this to copy and paste the entire row. My sample sheet didnt have the 6 other columns with data included and I need to include that data in the new sheet along with the filtered result
Re: VBA Code to Seperate a Column into Worksheets By Date Column
It's what happens when the attachment does not well reflect the real workbook …
My demonstration copies all the contiguous data so not a concern if there is no blank column between data,
but in case of non contiguous data you may use Resize or UsedRange statement …
Re: VBA Code to Seperate a Column into Worksheets By Date Column
I just need it to bring the information in columns A, B, & D to the new workbook it creates based on the date. There will be no blank rows in the date column, I am looking for/correcting for that prior to running this routine.
Re: VBA Code to Seperate a Column into Worksheets By Date Column
Another possibility...
Sub Test()
Dim rg As Range, s As Variant, k As Variant
Set rg = Worksheets("Sheet1").Cells(1, 1).CurrentRegion.Resize(, 4)
With CreateObject("Scripting.Dictionary")
For Each s In Application.Index(Application.Transpose(rg.Columns(2).Value), 1, 0)
If s <> "ONDATE" Then .Item(s) = Empty
Next s
For Each k In .keys
rg.AutoFilter 2, k
rg.SpecialCells(xlCellTypeVisible).Copy
Worksheets.Add
With ActiveSheet
.Name = Replace(k, "/", "-")
.Cells(1, 1).PasteSpecial
.Columns.AutoFit
End With
rg.AutoFilter
Next k
End With
End Sub
Bookmarks