Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 28
Default changeNumberFormat macro

Hi, I have a set of worksheets that i want to change the
number formatting on. In some columns there are
percentages, always with a header "%" and the rest I want
to be in accounting format but is in several different
number formats. Some cells are text only. I am working
on a macro to search the worksheet for cells that aren't
percentages and make them in the accounting format. Heres
what I have, can you see where I am going wrong?

Sub ChangeNumberFormat()
For Each cell In sht.UsedRange
IF Selection.NumberFormat = "0.00%"
End If
With Selection
Selection.SpecialCells(xlCellTypeFormulas, 23).Select
Selection.NumberFormat = "_(* #,##0.00_);_(* (#,##0.00);_
(* ""-""??_);_(@_)"
Next cell
MsgBox "Process Complete"
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default changeNumberFormat macro

Sub ChangeNumberFormat()
Dim sh as Worksheet
Dim cell as Range
set sh = worksheets("Sheet1")
For Each cell In sht.UsedRange.SpecialCells(xlFormulas)
IF instr(cell.NumberFormat,"%") = 0 then
if cell.hasFormula then
if isnumeric(cell) then
cell.NumberFormat = "_(* #,##0.00_);" & _
"_(* (#,##0.00);_(* ""-""??_);_(@_)"
End If
End If
Next cell
MsgBox "Process Complete"
End Sub

--
Regards,
Tom Ogilvy

Todd wrote in message
...
Hi, I have a set of worksheets that i want to change the
number formatting on. In some columns there are
percentages, always with a header "%" and the rest I want
to be in accounting format but is in several different
number formats. Some cells are text only. I am working
on a macro to search the worksheet for cells that aren't
percentages and make them in the accounting format. Heres
what I have, can you see where I am going wrong?

Sub ChangeNumberFormat()
For Each cell In sht.UsedRange
IF Selection.NumberFormat = "0.00%"
End If
With Selection
Selection.SpecialCells(xlCellTypeFormulas, 23).Select
Selection.NumberFormat = "_(* #,##0.00_);_(* (#,##0.00);_
(* ""-""??_);_(@_)"
Next cell
MsgBox "Process Complete"
End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default changeNumberFormat macro

You've kind of got a mixture of things going on.

Maybe this'll give you some more ideas:

Option Explicit
Sub ChangeNumberFormat()
Dim myCell As Range

For Each myCell In Selection.Cells 'sht.usedrange.cells
If myCell.NumberFormat = "0.00%" Then
'do nothing
Else
If IsNumeric(myCell.Value) Then
myCell.NumberFormat _
= "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)"
End If
End If
Next myCell

MsgBox "Process Complete"
End Sub

I used selection (instead of .usedrange). I figured it would be easier for you
to limit the range by selecting the portions that could have number formats that
should be changed.



Todd wrote:

Hi, I have a set of worksheets that i want to change the
number formatting on. In some columns there are
percentages, always with a header "%" and the rest I want
to be in accounting format but is in several different
number formats. Some cells are text only. I am working
on a macro to search the worksheet for cells that aren't
percentages and make them in the accounting format. Heres
what I have, can you see where I am going wrong?

Sub ChangeNumberFormat()
For Each cell In sht.UsedRange
IF Selection.NumberFormat = "0.00%"
End If
With Selection
Selection.SpecialCells(xlCellTypeFormulas, 23).Select
Selection.NumberFormat = "_(* #,##0.00_);_(* (#,##0.00);_
(* ""-""??_);_(@_)"
Next cell
MsgBox "Process Complete"
End Sub


--

Dave Peterson

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
Macro recorded... tabs & file names changed, macro hangs Steve Excel Worksheet Functions 3 October 30th 09 11:41 AM
Macro Help Needed - Excel 2007 - Print Macro with Auto Sort Gavin Excel Worksheet Functions 0 May 17th 07 01:20 PM
My excel macro recorder no longer shows up when recording macro jack Excel Discussion (Misc queries) 1 February 5th 07 09:31 PM
My excel macro recorder no longer shows up when recording macro jack Excel Discussion (Misc queries) 3 February 5th 07 08:22 PM
using a cell value to control a counter inside a macro and displaying macro value ocset Excel Worksheet Functions 1 September 10th 06 05:32 AM


All times are GMT +1. The time now is 12:36 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"