Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Formatting Date Macro

I need to be able to create a macro thast looks for the
column header Date on row 1 and format this entire column
into the date format dd/mm/yy.

The only way I have been able to do this at the moment is
by recording the macro when highlighting all these columns
manually and formatting them. This however only works
because I select the columns myself.

Is there anyway this can work looking for the column
header of Date rather than me having to select them?

Any help would be appreciated - thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 101
Default Formatting Date Macro

Sub formatDateCols()
Dim c As Range

For Each c In ActiveSheet.Range("1:1")
If c = "Date" Then
c.EntireColumn.NumberFormat = "dd/mm/yy"
End If
Next
End Sub


You only need to ask once.

Chrissy.


Sarah wrote
I need to be able to create a macro thast looks for the
column header Date on row 1 and format this entire column
into the date format dd/mm/yy.

The only way I have been able to do this at the moment is
by recording the macro when highlighting all these columns
manually and formatting them. This however only works
because I select the columns myself.

Is there anyway this can work looking for the column
header of Date rather than me having to select them?

Any help would be appreciated - thanks



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Formatting Date Macro

A possible alleviation of the problem you might have as you stated in your
next post:

Sub formatDateCols()
Dim c As Range

For Each c In ActiveSheet.Range("1:1")
If Instr(1,c, "date",vbTextCompare) Then
c.EntireColumn.NumberFormat = "dd/mm/yy"
End If
Next
End Sub


or

Sub formatDateCols()
Dim c As Range

For Each c In ActiveSheet.Range("1:1")
If strcomp(c, "date",vbTextCompare) = 0 Then
c.EntireColumn.NumberFormat = "dd/mm/yy"
End If
Next
End Sub

--
Regards,
Tom Ogilvy

"Chrissy" wrote in message
...
Sub formatDateCols()
Dim c As Range

For Each c In ActiveSheet.Range("1:1")
If c = "Date" Then
c.EntireColumn.NumberFormat = "dd/mm/yy"
End If
Next
End Sub


You only need to ask once.

Chrissy.


Sarah wrote
I need to be able to create a macro thast looks for the
column header Date on row 1 and format this entire column
into the date format dd/mm/yy.

The only way I have been able to do this at the moment is
by recording the macro when highlighting all these columns
manually and formatting them. This however only works
because I select the columns myself.

Is there anyway this can work looking for the column
header of Date rather than me having to select them?

Any help would be appreciated - thanks





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 101
Default Formatting Date Macro

Tom,

Care to discuss and compare the three approaches shown here?

Chrissy.

Tom Ogilvy wrote
A possible alleviation of the problem you might have as you stated in your
next post:

Sub formatDateCols()
Dim c As Range

For Each c In ActiveSheet.Range("1:1")
If Instr(1,c, "date",vbTextCompare) Then
c.EntireColumn.NumberFormat = "dd/mm/yy"
End If
Next
End Sub


or

Sub formatDateCols()
Dim c As Range

For Each c In ActiveSheet.Range("1:1")
If strcomp(c, "date",vbTextCompare) = 0 Then
c.EntireColumn.NumberFormat = "dd/mm/yy"
End If
Next
End Sub



Sub formatDateCols()
Dim c As Range

For Each c In ActiveSheet.Range("1:1")
If c = "Date" Then
c.EntireColumn.NumberFormat = "dd/mm/yy"
End If
Next
End Sub



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Formatting Date Macro

The original is case sensative. Since the question was posted twice, a
similar answer was posted in response to the second and the OP came back and
said the code did nothing. My assumption was that it was a case issue.

first I posted is case insensitive and looks for the string "date" in
content of the cell, so it could be too broad. However, it would work with
spaces on either or both ends.

the second is case insensitive and looks for the exact term date. It would
falter if there were spaces on either or both ends


--
Regards,
Tom Ogilvy


"Chrissy" wrote in message
...
Tom,

Care to discuss and compare the three approaches shown here?

Chrissy.

Tom Ogilvy wrote
A possible alleviation of the problem you might have as you stated in

your
next post:

Sub formatDateCols()
Dim c As Range

For Each c In ActiveSheet.Range("1:1")
If Instr(1,c, "date",vbTextCompare) Then
c.EntireColumn.NumberFormat = "dd/mm/yy"
End If
Next
End Sub


or

Sub formatDateCols()
Dim c As Range

For Each c In ActiveSheet.Range("1:1")
If strcomp(c, "date",vbTextCompare) = 0 Then
c.EntireColumn.NumberFormat = "dd/mm/yy"
End If
Next
End Sub



Sub formatDateCols()
Dim c As Range

For Each c In ActiveSheet.Range("1:1")
If c = "Date" Then
c.EntireColumn.NumberFormat = "dd/mm/yy"
End If
Next
End Sub







Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
conditional formatting for cell date to equal today's date Sistereinstein Excel Worksheet Functions 2 September 10th 12 07:53 PM
Date value conditional formatting or Macro Wanna Learn Excel Discussion (Misc queries) 0 July 22nd 08 01:01 PM
Date Formatting in a Macro [email protected] Excel Discussion (Misc queries) 3 May 19th 06 02:09 AM
formatting date-time values in macro scw-tzg Excel Worksheet Functions 2 November 14th 05 09:21 PM
How do I set a date range for conditional formatting in a macro? billo Excel Worksheet Functions 3 February 7th 05 06:19 PM


All times are GMT +1. The time now is 11:00 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"