Macro to format date

S

sw

I am looking for some code that will look through my
spreadsheet, find all columns headers called "Date" (which
will always be in row 1) and on finding a date column
format all cells in that column to the date format
dd/mm/yy.

Can anyone help with this.

Thanks
 
E

Ecco

Try this one.

Sub Macro1()
Dim X As Long
For X = 1 To 256
If Cells(1, X) = "Date" Then
Columns(X).NumberFormat = "dd/mm/yy"
End If
Next X
End Sub

Ecco
 
S

sw

I entered this code as it is stated below and ran the
macro but nothing happened! Any idea why
 
E

Ecco

Check if title Date is really written like Data (not like
date or DATE). You could change the IF-statement like this:

If Lcase(Cells(1,X))="date" Then

That´s the only idea I come up with. Hope it works.

Ecco
 
T

Tom Ogilvy

Sub Macro1()
Dim X As Long
For X = 1 To 256
If Instr(1,Cells(1, X).Value,"date",vbTextCompare) Then
Columns(X).NumberFormat = "dd/mm/yy"
End If
Next X
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top