How to change cells format?

alee001

Board Regular
Joined
Sep 9, 2014
Messages
154
Office Version
  1. 2010
Platform
  1. Windows
There are many cells are "yyyy-mmm-dd" format in anywhere on worksheet, how to auto detect their change to "dd/mm/yyyy hh:mm" format using VBA?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
This should do that for all cells down to the last populated cell on your worksheet.
Code:
Sub MyFormatChange()

    Dim lcell As Range
    Dim cell As Range
    
    Application.ScreenUpdating = False
    
'   Find last cell on worksheet
    Set lcell = Range("A1").SpecialCells(xlLastCell)
    
'   Loop through all cells in workbook
    For Each cell In Range("A1:" & lcell.Address)
'       Change date format
        If cell.NumberFormat = "yyyy-mm-dd" Then cell.NumberFormat = "dd/mm/yyyy hh:mm"
    Next cell
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
the newer Excel versions have the option under find (CTRL-F) or find & replace (CTRL-H) to find & replace formats
using the macro recorder on that gives some code such as below. as you can see, no loops needed
Code:
Application.FindFormat.NumberFormat = "yyyy-mm-dd"
Application.ReplaceFormat.NumberFormat = "dd/mm/yyyy hh:mm"
Cells.Replace What:="*", Replacement:="", LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=True, ReplaceFormat:=True
 
Upvote 0
the newer Excel versions have the option under find (CTRL-F) or find & replace (CTRL-H) to find & replace formats
That's pretty cool. I was unaware of that.
 
Upvote 0
Thks Fazza, that is smart and quick code form marco...
Code:
Sub chg_fmt()
    Application.ReplaceFormat.NumberFormat = "dd/mmm/yyyy hh:mm"
    Cells.Replace What:="", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=True, ReplaceFormat:=True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,271
Members
452,628
Latest member
dd2

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