Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 21
Default Number Format based on Cell Content

Thanks in advance, been looking at other post and couldn't quite get it to
work.

So what I need is such;

Cell A1 = "Number" then cells A2 through A14 are formatted as numbers with 2
decimal places. Otherwise they are numbers with no decimal places. I also
need both number formats to have the "," seperator.

I also keep the worksheet locked with some of the cells unlocked. In order
for this macro to work would I need to have the entire sheet unlocked, just
the cells, or neither.
  #2   Report Post  
Posted to microsoft.public.excel.programming
Ken Ken is offline
external usenet poster
 
Posts: 207
Default Number Format based on Cell Content

Rob

Assuming the sheet is protected, wtih password "test" and cell A1 not
protected, if you put the following code in the worksheet_change
event, it will do what I think you want.


Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = "$A$1" Then

ActiveSheet.Unprotect Password:="test"

If Target.Value = "Number" Then
Range("A2:A14").NumberFormat = "#,##0.00"
Else
Range("A2:A14").NumberFormat = "#,##0"
End If

ActiveSheet.Protect Password:="test"

End If

End Sub

If cell A1 is changed programmatically or if it is a formula, that
sometimes returns "Number" then you will have to trigger the macro
some other way. In this event code, it runs everytime the worksheet
changes, but, if the cell that triggers the event is not A1, it gets
out right away. If it is A1 that has been changed, it formats cells
A2:A14 appropriately.

If you want the macro to run less often, or more often, you can use
the same basic code in a regular module, attached to a button, or a
shortcut, or stick it in another event code. If you do any of those
you just need to get rid of the outer (if target=) loop.

Good luck.

Ken
Norfolk, Va



On Jan 5, 3:28*pm, AirgasRob
wrote:
Thanks in advance, been looking at other post and couldn't quite get it to
work.

So what I need is such;

Cell A1 = "Number" then cells A2 through A14 are formatted as numbers with 2
decimal places. Otherwise they are numbers with no decimal places. I also
need both number formats to have the "," seperator.

I also keep the worksheet locked with some of the cells unlocked. In order
for this macro *to work would I need to have *the entire sheet unlocked, just
the cells, or neither.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 21
Default Number Format based on Cell Content

Thank you Ken, works perfect.

"Ken" wrote:

Rob

Assuming the sheet is protected, wtih password "test" and cell A1 not
protected, if you put the following code in the worksheet_change
event, it will do what I think you want.


Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = "$A$1" Then

ActiveSheet.Unprotect Password:="test"

If Target.Value = "Number" Then
Range("A2:A14").NumberFormat = "#,##0.00"
Else
Range("A2:A14").NumberFormat = "#,##0"
End If

ActiveSheet.Protect Password:="test"

End If

End Sub

If cell A1 is changed programmatically or if it is a formula, that
sometimes returns "Number" then you will have to trigger the macro
some other way. In this event code, it runs everytime the worksheet
changes, but, if the cell that triggers the event is not A1, it gets
out right away. If it is A1 that has been changed, it formats cells
A2:A14 appropriately.

If you want the macro to run less often, or more often, you can use
the same basic code in a regular module, attached to a button, or a
shortcut, or stick it in another event code. If you do any of those
you just need to get rid of the outer (if target=) loop.

Good luck.

Ken
Norfolk, Va



On Jan 5, 3:28 pm, AirgasRob
wrote:
Thanks in advance, been looking at other post and couldn't quite get it to
work.

So what I need is such;

Cell A1 = "Number" then cells A2 through A14 are formatted as numbers with 2
decimal places. Otherwise they are numbers with no decimal places. I also
need both number formats to have the "," seperator.

I also keep the worksheet locked with some of the cells unlocked. In order
for this macro to work would I need to have the entire sheet unlocked, just
the cells, or neither.



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
automatically format cell based on content of another cell Blustreaker Excel Worksheet Functions 2 July 15th 07 11:52 AM
Cond Format & helper-cell based "duplicate rec" tricked by content Dennis Excel Discussion (Misc queries) 3 December 16th 05 06:55 PM
Set number format based on cell contents nospaminlich Excel Discussion (Misc queries) 5 December 8th 05 06:41 PM
Number format based on number format of another cell in another workbook Rob Excel Programming 9 January 9th 05 04:30 PM
Format Row based on Cell Content SFrongillo_Lib Excel Programming 3 October 10th 03 01:48 PM


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