VBA to Convert Time Format and Filter Columns By Time

dljjackson

New Member
Joined
Dec 21, 2014
Messages
12
Office Version
  1. 2013
Platform
  1. Windows
Macro to Convert Time Format and Filter Into Columns By Time

Hello,

I'm looking for some assistance in using vba to convert the format of time in a column from standard to military timing.

Background
In the linked document 'Activity Detail' the time I'm looking to be converting is in Column P 'Time Submitted'. The current VBA I use is in the attached file 'SOD Reporting'. I copy and paste activity detail into cell A1 of 'SOD Reporting' then hit unique contacts button to get my results. I received a lot of help with this but now I'm looking to expand it and will need some more assistance.

Next Steps
I would like to add the following columns based on the 'Time Submitted' and 'Form Name' columns in the 'Activity Detail document'

which require analysis of the data.
Knocks by 1PM
-Count of 'Form Name' up to 1pm
Knocks by 3pm
-Count of 'Form Name' up to 3pm
Knocks by 5pm
-Count of 'Form Name' up to 5pm
Knocks after 5pm
-Count of 'Form Name' after 5pm
Knocks by 7pm
-Count of 'Form Name' after 7pm
Knocks by after 7pm
-Count of 'Form Name' after 7pm

Here is my current code:
Code:
<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">Sub UniqueContactsReport()
    Dim cn As Object, rs As Object, i As Long, temp As String
    With CreateObject("Scripting.FileSystemObject")
        temp = ThisWorkbook.Path & "" & Replace(.GetTempName, "tmp", .GetExtensionName(ThisWorkbook.Name))
    End With
    ThisWorkbook.SaveCopyAs temp
    With Sheets("Activity Detail")
        .Cells.ClearContents
        Set cn = CreateObject("ADODB.Connection")
        Set rs = CreateObject("ADODB.Recordset")
        With cn
            .Provider = "Microsoft.Ace.OLEDB.12.0"
            .Properties("Extended Properties") = "Excel 12.0;HDR=Yes;IMEX=1;"
            .Open temp
        End With
        rs.Open "Select `Team`,`Supervisor`, `D2D Rep`,  Sum(`Sold`) As `Sold`, " & _
                "Sum(`Contact`) As `Unique Contacts` From `Activity Detail$` " & _
                "Where `D2D Rep` Is Not Null Group By `D2D Rep`, " & _
                "`Supervisor`, `Team` Order By `Team`, Sum(`Sold`), Sum(`Contact`)", cn
        For i = 0 To rs.Fields.Count - 1
            .Cells(1, i + 1).Value = rs.Fields(i).Name
        Next
        .[a2].CopyFromRecordset rs
 With .Cells(1).CurrentRegion
            .FormatConditions.Delete
            .FormatConditions.Add Type:=xlExpression, Formula1:="=and(isnumber($d1),$D1=1)"
            .FormatConditions(1).Interior.Color = 5296274
            .FormatConditions.Add Type:=xlExpression, Formula1:="=and(isnumber($d1),$D1>1)"
            .FormatConditions(2).Interior.Color = RGB(0, 153, 51)
        End With
        .Columns("A:B").ColumnWidth = 25
        .Columns("C").ColumnWidth = 20
        .Columns("D").ColumnWidth = 8
        .Columns("E").ColumnWidth = 17
        .Rows("2:200").RowHeight = 15
    End With
    Set cn = Nothing: Set rs = Nothing
    Kill temp
End Sub</code>

 
Last edited:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Forum statistics

Threads
1,215,002
Messages
6,122,652
Members
449,092
Latest member
peppernaut

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