Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 15
Default Format cell based on function result

Would like to format conditionally using VB. I know how to use "conditional
format" on a cell in the spreadsheet, but want to do this in the code.

In particular, I have thousands of rows of "events". Content of first two
cells in each are strings representing time. The function "get_diff" returns
difference, in seconds. So cell A3 contains
"='PERSONAL.xls'!Module1.get_diff(A1,A2)"

Function get_diff(str0 As String, str1 As String, Optional dbg_flag As
Boolean = False) As Double
Dim xDiff As Double
xDiff = convert_to_sec(str1, dbg_flag) - convert_to_sec(str0, dbg_flag)
If dbg_flag Then
MsgBox "Diff is " & Format(xDiff, "#######.######")
End If
get_diff = xDiff
End Function

How do I format the cell that gets the result based on value returned from
get_diff? That is the A3 (or any other cell from which I called get_diff)
gets formatted based on the result. I can't figure out how to select the
cell in the code.
I need something like:
If xDiff 3.001 Then
.....Font.Bold = True
.....Font.ColorIndex = 3
Else
.....Font.Bold = False
.....Font.ColorIndex = 10
Endif


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Format cell based on function result

Functions (UDF) called from within a sheet return values... Period. They can
not modify formats or change the values of cells that they are not in. If the
function is called in code then it can do whatever it wants but not called
from a cell. Your formatting will have to be done externally via conditional
formats or some other means... Assuming that I understood your question
correctly...
--
HTH...

Jim Thomlinson


"laura_in_abq" wrote:

Would like to format conditionally using VB. I know how to use "conditional
format" on a cell in the spreadsheet, but want to do this in the code.

In particular, I have thousands of rows of "events". Content of first two
cells in each are strings representing time. The function "get_diff" returns
difference, in seconds. So cell A3 contains
"='PERSONAL.xls'!Module1.get_diff(A1,A2)"

Function get_diff(str0 As String, str1 As String, Optional dbg_flag As
Boolean = False) As Double
Dim xDiff As Double
xDiff = convert_to_sec(str1, dbg_flag) - convert_to_sec(str0, dbg_flag)
If dbg_flag Then
MsgBox "Diff is " & Format(xDiff, "#######.######")
End If
get_diff = xDiff
End Function

How do I format the cell that gets the result based on value returned from
get_diff? That is the A3 (or any other cell from which I called get_diff)
gets formatted based on the result. I can't figure out how to select the
cell in the code.
I need something like:
If xDiff 3.001 Then
.....Font.Bold = True
.....Font.ColorIndex = 3
Else
.....Font.Bold = False
.....Font.ColorIndex = 10
Endif


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 15
Default Format cell based on function result

Jim,
hmmm, what do you think if I also pass a reference to cell A3 in the UDF?


"Jim Thomlinson" wrote:

Functions (UDF) called from within a sheet return values... Period. They can
not modify formats or change the values of cells that they are not in. If the
function is called in code then it can do whatever it wants but not called
from a cell. Your formatting will have to be done externally via conditional
formats or some other means... Assuming that I understood your question
correctly...
--
HTH...

Jim Thomlinson


"laura_in_abq" wrote:

Would like to format conditionally using VB. I know how to use "conditional
format" on a cell in the spreadsheet, but want to do this in the code.

In particular, I have thousands of rows of "events". Content of first two
cells in each are strings representing time. The function "get_diff" returns
difference, in seconds. So cell A3 contains
"='PERSONAL.xls'!Module1.get_diff(A1,A2)"

Function get_diff(str0 As String, str1 As String, Optional dbg_flag As
Boolean = False) As Double
Dim xDiff As Double
xDiff = convert_to_sec(str1, dbg_flag) - convert_to_sec(str0, dbg_flag)
If dbg_flag Then
MsgBox "Diff is " & Format(xDiff, "#######.######")
End If
get_diff = xDiff
End Function

How do I format the cell that gets the result based on value returned from
get_diff? That is the A3 (or any other cell from which I called get_diff)
gets formatted based on the result. I can't figure out how to select the
cell in the code.
I need something like:
If xDiff 3.001 Then
.....Font.Bold = True
.....Font.ColorIndex = 3
Else
.....Font.Bold = False
.....Font.ColorIndex = 10
Endif


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Format cell based on function result

A UDF can know what cell it is located in something like this

Public Function Tada() As String
Tada = Application.Caller.Address
End Function

In Cell A1 add the formula =tada()

That being said even if it knows what cell it is in it can not change the
formatting of that cell. There is NO formula that you can type in a cell to
change it's format (conditional formatting is different as it is not entered
in a cell). Assuming that you do not want to do any kind of math on the
return value of the function you can make it text and the text can be
formatted however you want...

Public Function ThisDate() As String
ThisDate = Format(Now(), "Mmm dd")
End Function

Public Function ThatDate() As String
ThatDate = Format(Now(), "dddd mmmm d, yyyy")
End Function
--
HTH...

Jim Thomlinson


"laura_in_abq" wrote:

Jim,
hmmm, what do you think if I also pass a reference to cell A3 in the UDF?


"Jim Thomlinson" wrote:

Functions (UDF) called from within a sheet return values... Period. They can
not modify formats or change the values of cells that they are not in. If the
function is called in code then it can do whatever it wants but not called
from a cell. Your formatting will have to be done externally via conditional
formats or some other means... Assuming that I understood your question
correctly...
--
HTH...

Jim Thomlinson


"laura_in_abq" wrote:

Would like to format conditionally using VB. I know how to use "conditional
format" on a cell in the spreadsheet, but want to do this in the code.

In particular, I have thousands of rows of "events". Content of first two
cells in each are strings representing time. The function "get_diff" returns
difference, in seconds. So cell A3 contains
"='PERSONAL.xls'!Module1.get_diff(A1,A2)"

Function get_diff(str0 As String, str1 As String, Optional dbg_flag As
Boolean = False) As Double
Dim xDiff As Double
xDiff = convert_to_sec(str1, dbg_flag) - convert_to_sec(str0, dbg_flag)
If dbg_flag Then
MsgBox "Diff is " & Format(xDiff, "#######.######")
End If
get_diff = xDiff
End Function

How do I format the cell that gets the result based on value returned from
get_diff? That is the A3 (or any other cell from which I called get_diff)
gets formatted based on the result. I can't figure out how to select the
cell in the code.
I need something like:
If xDiff 3.001 Then
.....Font.Bold = True
.....Font.ColorIndex = 3
Else
.....Font.Bold = False
.....Font.ColorIndex = 10
Endif


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 Format based on forumula result Hendrik[_2_] Excel Discussion (Misc queries) 5 May 3rd 07 02:05 PM
IF function based on True result with large formula. naiveprogrammer Excel Programming 3 September 10th 05 11:53 AM
Can Excel operate a function based on a true or false result? SteveD Excel Worksheet Functions 1 August 26th 05 01:47 PM
format cell based on results of vlookup function Edith F Excel Worksheet Functions 1 July 21st 05 07:39 PM
how do I insert a row based on a function result chris Excel Worksheet Functions 3 May 25th 05 12:58 AM


All times are GMT +1. The time now is 01:33 AM.

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"