Skip to content

Commit

Permalink
Added date/time split macro.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitpusher2k committed May 4, 2024
1 parent 311bb5c commit ca10ffe
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
37 changes: 36 additions & 1 deletion ExcelMacros.vba
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ Sub SaveWorksheetAsXLSX()
BaseFileName = Join(FileNameArray, ".")
ActiveFilePath = ActiveWorkbook.Path
NewFullPath = ActiveFilePath & "\" & BaseFileName & ".xlsx"
MsgBox NewFullPath
'MsgBox NewFullPath
ActiveWorkbook.SaveAs Filename:=NewFullPath, FileFormat:=51
End Sub

Expand All @@ -371,4 +371,39 @@ Sub UnhideAllRowsColumns()
End Sub


Public Sub SplitDateAndTimeToNewColumns()
Dim MyDateTime As Date
Dim SelectedColumn As Range
Dim LastRow As Long
Dim i As Long

' Check if a column is selected
On Error Resume Next
Set SelectedColumn = Selection.EntireColumn
On Error GoTo 0

If SelectedColumn Is Nothing Then
MsgBox "Please select a column containing date and time data.", vbExclamation, "Error"
Exit Sub
End If

' Find the last row in the selected column
LastRow = Cells(Rows.Count, SelectedColumn.Column).End(xlUp).row

' Insert new columns to the right
SelectedColumn.Offset(, 1).Insert Shift:=xlToRight
SelectedColumn.Offset(, 2).Insert Shift:=xlToRight

' Loop through each row (skip header row)
For i = 2 To LastRow
MyDateTime = SelectedColumn.Cells(i, 1).Value

' Extract date and time
SelectedColumn.Cells(i, 2).Value = Int(MyDateTime) ' Date
SelectedColumn.Cells(i, 3).Value = MyDateTime - Int(MyDateTime) ' Time

' Format the new columns
SelectedColumn.Cells(i, 2).NumberFormat = "YYYY-MM-DD"
SelectedColumn.Cells(i, 3).NumberFormat = "hh:mm:ss"
Next i
End Sub
Binary file modified PERSONAL.XLSB
Binary file not shown.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ https://github.com/bitpusher2k

# ExcelMacros.vba - By Bitpusher/The Digital Fox

## v1.0 last updated 2024-03-29
## v1.1 last updated 2024-05-04

## Simple Excel macro set.

Expand Down Expand Up @@ -64,6 +64,7 @@ If PERSONAL.XLSB cannot be loaded from default location a custom location can be
* AddFilter - Adds filter to top row. Easy enough to do with the Ctrl+Shift+L shortcut, but fits in with the flow when using other related macros.
* HideEmptyColumns - Hides all columns with data only in the first row (which is assumed to be the header row).
* HideGuidColumns - Hide all columns with a GUID in the second row (the first is assumed to be the header). Be sure to enable "Microsoft VBScript Regular Expression 5.5" under "Tools" > "References..." for this to work.
* SplitDateAndTimeToNewColumns - If a column containing *date* *space* *time* is selected: creates two new columns to the right, copies *date* into the first, and copies *time* into the second.
* HighlightRowsWithSelectedValue - Highlights all lines that have a cell which contains the value in the currently selected cell. Can then use filter by color to limit view to highlighted entries. Separate macros for yellow/green/red highlighting included.
* BlankIfError - Surround formulas in all selected cells with =IFERROR(,"").
* ConvertSelectedToValues - Converts formulas in selected cells to values.
Expand Down
Binary file modified Ribbon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ca10ffe

Please sign in to comment.