how to remove Hours and minute from text date. ..

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

I have seen one post where in dates column. hours and minute to remove and format the date.

I have one question if the given date is in text format, 2017-08-10 : 04:30 , what would be the code,
to remove hours and mintues from it. I have similar kind of situation , One of my colleague told use trim function . but I don't know in vba how to do that. plz help.



Sub RemoveTimeValue()
Dim X As Long, SheetNames As Variant, Cols As Variant
SheetNames = Array("Sheet1", "Sheet2", "Sheet4", "Sheet5")
Cols = Array("J", "L", "P", "R")
For X = LBound(SheetNames) To UBound(SheetNames)
Worksheets(SheetNames(X)).Columns(Cols(X)).Replace " *", "", xlPart
Worksheets(SheetNames(X)).Columns(Cols(X)).NumberFormat = "yyyy-mm-dd"
Next
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Assume your data is in column A, then in column B =left(A1,10)
 
Upvote 0
Try:

Code:
Public Sub RemoveTimeValue()
Dim SheetNames      As Variant, _
    wsname          As Variant
    
Dim Cols            As Variant, _
    xCol            As Variant
    
Dim i               As Long, _
    LR              As Long, _
    strTmp          As String
    
With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
End With
    
SheetNames = Array("Sheet1", "Sheet2", "Sheet4", "Sheet5")
Cols = Array("J", "L", "P", "R")

For Each wsname In SheetNames
    For Each xCol In Cols
        LR = Sheets(wsname).Cells(Rows.Count, xCol).End(xlUp).Row
        For i = 2 To LR
            With Sheets(wsname).Cells(i, xCol)
                strTmp = .Value
                'Strip off the time by only returning the string which appears before the first space
                strTmp = Left(strTmp, InStr(" ", strTmp) - 1)
                'Convert the returned string to a date and store in the cell's value
                .Value = CDate(strTmp)
            End With
        Next i
    Next xCol
Next wsname

With Application
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,471
Messages
6,124,999
Members
449,201
Latest member
Lunzwe73

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