#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 336
Default Format Cell

Hello,

I am new to Excel programming so I would appreciate some guidance.

I would like to loop through the second row of each column to find the cell
format and then format to a specific way. For instance, if the cell is
general or text then left alight, if it is a number then right align and
format to "0,000", if date then centre align and format dd/mm/yy.

Can someone point me in the right direction here. I have tried piece
together code from this site but cannot make much sense of it.

Many thanks,

Martin
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Format Cell

Martin,

Right click your sheet tab, view code and paste this in and run it

Sub missive()
lastcol = Range("IV2").End(xlToLeft).Address
Set myrange = Range("$A$2:" & lastcol)
For Each c In myrange
If WorksheetFunction.IsText(c) Then
c.HorizontalAlignment = xlLeft
ElseIf IsDate(c) Then
c.HorizontalAlignment = xlCenter
c.NumberFormat = "dd/mm/yyyy"
ElseIf IsNumeric(c) Then
c.NumberFormat = "0,000"
c.HorizontalAlignment = xlRight
End If
Next
End Sub

Mike

"Martin" wrote:

Hello,

I am new to Excel programming so I would appreciate some guidance.

I would like to loop through the second row of each column to find the cell
format and then format to a specific way. For instance, if the cell is
general or text then left alight, if it is a number then right align and
format to "0,000", if date then centre align and format dd/mm/yy.

Can someone point me in the right direction here. I have tried piece
together code from this site but cannot make much sense of it.

Many thanks,

Martin

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 336
Default Format Cell

Thanks Mike, worked like a treat. It also helps me to understand how it
works now.

Many thanks.

Martin

"Mike H" wrote:

Martin,

Right click your sheet tab, view code and paste this in and run it

Sub missive()
lastcol = Range("IV2").End(xlToLeft).Address
Set myrange = Range("$A$2:" & lastcol)
For Each c In myrange
If WorksheetFunction.IsText(c) Then
c.HorizontalAlignment = xlLeft
ElseIf IsDate(c) Then
c.HorizontalAlignment = xlCenter
c.NumberFormat = "dd/mm/yyyy"
ElseIf IsNumeric(c) Then
c.NumberFormat = "0,000"
c.HorizontalAlignment = xlRight
End If
Next
End Sub

Mike

"Martin" wrote:

Hello,

I am new to Excel programming so I would appreciate some guidance.

I would like to loop through the second row of each column to find the cell
format and then format to a specific way. For instance, if the cell is
general or text then left alight, if it is a number then right align and
format to "0,000", if date then centre align and format dd/mm/yy.

Can someone point me in the right direction here. I have tried piece
together code from this site but cannot make much sense of it.

Many thanks,

Martin

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 76
Default Format Cell

Martin, instead of using "0,000", you should consider "#,##0". This will
always give you a number (the zero), but not leading zeroes.
--
Brad E.

"Martin" wrote:

Thanks Mike, worked like a treat. It also helps me to understand how it
works now.

Many thanks.

Martin

"Mike H" wrote:

Martin,

Right click your sheet tab, view code and paste this in and run it

Sub missive()
lastcol = Range("IV2").End(xlToLeft).Address
Set myrange = Range("$A$2:" & lastcol)
For Each c In myrange
If WorksheetFunction.IsText(c) Then
c.HorizontalAlignment = xlLeft
ElseIf IsDate(c) Then
c.HorizontalAlignment = xlCenter
c.NumberFormat = "dd/mm/yyyy"
ElseIf IsNumeric(c) Then
c.NumberFormat = "0,000"
c.HorizontalAlignment = xlRight
End If
Next
End Sub

Mike

"Martin" wrote:

Hello,

I am new to Excel programming so I would appreciate some guidance.

I would like to loop through the second row of each column to find the cell
format and then format to a specific way. For instance, if the cell is
general or text then left alight, if it is a number then right align and
format to "0,000", if date then centre align and format dd/mm/yy.

Can someone point me in the right direction here. I have tried piece
together code from this site but cannot make much sense of it.

Many thanks,

Martin

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 336
Default Format Cell

Thank you. I reaslised my mistake when I ran it for the first time.

Martin

"Brad E." wrote:

Martin, instead of using "0,000", you should consider "#,##0". This will
always give you a number (the zero), but not leading zeroes.
--
Brad E.

"Martin" wrote:

Thanks Mike, worked like a treat. It also helps me to understand how it
works now.

Many thanks.

Martin

"Mike H" wrote:

Martin,

Right click your sheet tab, view code and paste this in and run it

Sub missive()
lastcol = Range("IV2").End(xlToLeft).Address
Set myrange = Range("$A$2:" & lastcol)
For Each c In myrange
If WorksheetFunction.IsText(c) Then
c.HorizontalAlignment = xlLeft
ElseIf IsDate(c) Then
c.HorizontalAlignment = xlCenter
c.NumberFormat = "dd/mm/yyyy"
ElseIf IsNumeric(c) Then
c.NumberFormat = "0,000"
c.HorizontalAlignment = xlRight
End If
Next
End Sub

Mike

"Martin" wrote:

Hello,

I am new to Excel programming so I would appreciate some guidance.

I would like to loop through the second row of each column to find the cell
format and then format to a specific way. For instance, if the cell is
general or text then left alight, if it is a number then right align and
format to "0,000", if date then centre align and format dd/mm/yy.

Can someone point me in the right direction here. I have tried piece
together code from this site but cannot make much sense of it.

Many thanks,

Martin

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
Lock Cell Format - Allow copy and paste of data without format change Chris12InKC Excel Worksheet Functions 2 May 9th 23 07:42 PM
cell format - remove cell format pattern without effecting colors Bas Excel Discussion (Misc queries) 1 March 23rd 09 02:54 PM
Can cell format come from and change with reference cell format jclouse Excel Discussion (Misc queries) 1 November 29th 06 03:20 AM
how do i format a cell based on format of a range of cells? Chris Hardick Excel Discussion (Misc queries) 2 April 3rd 06 08:54 AM
How do I copy data in single cell format to a merged cell format Paul Excel Discussion (Misc queries) 1 June 27th 05 11:00 AM


All times are GMT +1. The time now is 11:40 PM.

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"