Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1
Default user defined functions format

I am attempting to use a user-defined function to work on a cell value and
change that cell value's format. IE if A1<20 format "0.00 uL" or if A12000
divide by 1000 then format "0.0 mL", and so on...
My problem is that I create a circular reference because the cell to change
is the cell the formula would be in.
I did have a multi-level if statement in A1, but was hoping that a function
in VBA would be easier.
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1,533
Default user defined functions format

Hi

Use an event code to change the format of the target cell. Change
TargetRange to refer to the range where you want to do this formatting.

As it's an event code it has to be placed in the code sheet for the desired
sheet.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim TargetRange As Range
Set TargetRange = Range("A1:A10") '<=== change to suit

Set isect = Intersect(Target, TargetRange)

If Not isect Is Nothing Then
Application.EnableEvents = False
If Target.Value < 20 Then
Target.Value = Format(Target.Value, "0.00 uL")
ElseIf Target.Value 2000 Then
Target.Value = Target.Value / 1000
Target.Value = Format(Target.Value, "0.0 mL")
End If
Application.EnableEvents = True
End If
End Sub

RegardsĀ“,
Per

"df_newtovba" skrev i meddelelsen
...
I am attempting to use a user-defined function to work on a cell value and
change that cell value's format. IE if A1<20 format "0.00 uL" or if
A12000
divide by 1000 then format "0.0 mL", and so on...
My problem is that I create a circular reference because the cell to
change
is the cell the formula would be in.
I did have a multi-level if statement in A1, but was hoping that a
function
in VBA would be easier.


  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5,939
Default user defined functions format

A function can not change the format of a cell. That is true for UDF's and
built in functons. Functions return values to the cells they are in. That
being said you could use a simple custom format something like this one...

[<20]0.0 ul;[2000] #.0," ml";General

Right click the cell and select Format Cell... - Number - Custom and add
that format.
--
HTH...

Jim Thomlinson


"df_newtovba" wrote:

I am attempting to use a user-defined function to work on a cell value and
change that cell value's format. IE if A1<20 format "0.00 uL" or if A12000
divide by 1000 then format "0.0 mL", and so on...
My problem is that I create a circular reference because the cell to change
is the cell the formula would be in.
I did have a multi-level if statement in A1, but was hoping that a function
in VBA would be easier.

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5,939
Default user defined functions format

Since it is what the user asked for this is not criticism, but if you divide
the number (2000) as opposed to just reformat it you can't add the column or
reasonably perform any calculations on the data set since this process
modifies the underlying values.
--
HTH...

Jim Thomlinson


"Per Jessen" wrote:

Hi

Use an event code to change the format of the target cell. Change
TargetRange to refer to the range where you want to do this formatting.

As it's an event code it has to be placed in the code sheet for the desired
sheet.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim TargetRange As Range
Set TargetRange = Range("A1:A10") '<=== change to suit

Set isect = Intersect(Target, TargetRange)

If Not isect Is Nothing Then
Application.EnableEvents = False
If Target.Value < 20 Then
Target.Value = Format(Target.Value, "0.00 uL")
ElseIf Target.Value 2000 Then
Target.Value = Target.Value / 1000
Target.Value = Format(Target.Value, "0.0 mL")
End If
Application.EnableEvents = True
End If
End Sub

RegardsĀ“,
Per

"df_newtovba" skrev i meddelelsen
...
I am attempting to use a user-defined function to work on a cell value and
change that cell value's format. IE if A1<20 format "0.00 uL" or if
A12000
divide by 1000 then format "0.0 mL", and so on...
My problem is that I create a circular reference because the cell to
change
is the cell the formula would be in.
I did have a multi-level if statement in A1, but was hoping that a
function
in VBA would be easier.



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
User Defined Functions CH Excel Worksheet Functions 4 September 22nd 08 10:16 AM
User defined functions without using VBA. [email protected] Excel Discussion (Misc queries) 0 June 13th 06 05:50 PM
User defined functions without using VBA. [email protected] Excel Worksheet Functions 0 June 13th 06 05:49 PM
User Defined Functions Mike McLellan Excel Worksheet Functions 2 May 4th 06 10:56 AM
About User Defined Functions linzhang426 Excel Worksheet Functions 4 October 17th 05 09:27 PM


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

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

About Us

"It's about Microsoft Excel"