Date and time formats in VB

handysmurf

Board Regular
Joined
Jan 17, 2015
Messages
89
Office Version
  1. 365
Platform
  1. Windows
I have the following script

Code:
Sub ListAllFile()




Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim ws As Worksheet
Dim sPath As String
Dim lrA As Long
Dim lrB As Long




Set objFSO = CreateObject("Scripting.FileSystemObject")
Set ws = Worksheets.Add




'Get the folder object associated with the directory
sPath = InputBox("What is the full Path to Search?")
Set objFolder = objFSO.GetFolder(sPath)
ws.Cells(1, 1).Value = "The files found in " & objFolder.Name & " are:"
ws.Cells(1, 2).Value = "The files found have modified dates:"
ws.Cells(1, 3).Value = "The file Size is:"




'Loop through the Files collection
For Each objFile In objFolder.Files
'If objFile.Name Like "*.pdf" Then
lrA = Range("A" & Rows.Count).End(xlUp).Row
lrB = Range("B" & Rows.Count).End(xlUp).Row
ws.Range("A" & lrA + 1).Value = objFile.Name
ws.Range("B" & lrB + 1).Value = objFile.DateLastModified
ws.Range("C" & lrB + 1).Value = objFile.Size
ws.Range("D" & lrB + 1).Value = objFile.DateLastModified
        
'End If
Next
'ws.Cells(2, 1).Delete
'Clean up!
Set objFolder = Nothing
Set objFile = Nothing
Set objFSO = Nothing
 




End Sub

Can i get it to return Range B in Date format "mm/dd/yyyy"

and Range D in time format "hh:mm"

TIA
 
Last edited:

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi, just modify the lines with the date and time values as follows: add FORMAT ... to the code lines:
Code:
'for the date
Format(<CELL VALUE> ,"mm/dd/yyyy")
and for the time
Format(<CELL VALUE> ,"hh,mm'')
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,939
Members
449,094
Latest member
teemeren

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