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

I have a spreadsheet that tracks "violations" by setting background colors of
cells and by placing text in certain "custom" cells. I wrote a UDF to
calculate how many violations appear in a specified range. I then call this
UDF using a formula like...

=GetViolationCount($A1:$AF1)

It works great in a worksheet. In other words, every cell in which I call
the UDF with an appropriate range, the cell shows the correct value.
In another portion of the spreadsheet, I have UserForm that searches for
any of several, user chosen, criteria. One of the choices is "has
violations". So I want to look at the cell that contains the result of my
GetViolationCount UDF and simply test if it is greater than 0. ie....

CInt(allDataRange(curRow, kViolationCountColumnIndex).Value) 0

No matter which property I use to access that cell, I've tried Value,
Value2 and Text among others, I always get back an empty string. Other
properties like Address etc return appropriate values. Am I trying to do
something impossible or am I being stupid?

Here is the entire UDF if it matters...

Public Function GetViolationCount(inRange As Range) As Integer
Dim numViolations As Integer

numViolations = 0

Dim numRows As Integer
Dim nthRow As Integer

numRows = inRange.Rows.Count

Dim numCols As Integer
Dim nthColumn As Integer

numCols = inRange.Columns.Count

For nthRow = 1 To numRows
For nthColumn = kFirstViolationColumnIndex To numCols
Dim tStr As String
tStr = CStr(inRange(nthRow, nthColumn).Value)

If IsCustomColumn(nthColumn) Then
If inRange(nthRow, nthColumn).Value < "" Then
numViolations = numViolations + 1
End If
Else
If inRange(nthRow, nthColumn).Interior.colorIndex < xlNone
Then
numViolations = numViolations + 1
End If
End If
Next nthColumn
Next nthRow

GetViolationCount = numViolations
End Function
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default UDF Question

I've added some comments to your code.

Public Function GetViolationCount(inRange As Range) As Integer
Dim numViolations As Integer
Dim kFirstViolationColumnIndex As Long

numViolations = 0

Dim numRows As Integer
Dim nthRow As Integer

numRows = inRange.Rows.Count

Dim numCols As Integer
Dim nthColumn As Integer

numCols = inRange.Columns.Count

For nthRow = 1 To numRows
'Do you really want nthRow to start at 1 if the range starts in row
(say) 10
For nthColumn = kFirstViolationColumnIndex To numCols
'Where is kFirstViolationCOlumnIndex defined
Dim tStr As String
tStr = CStr(inRange(nthRow, nthColumn).Value)

If Iscustomcolumn(nthColumn) Then '<~~~I presume this is
another function
'Do you have this defined somewhere?
If inRange(nthRow, nthColumn).Value < "" Then
'<~~~another function?
numViolations = numViolations + 1
End If
Else
If inRange(nthRow, nthColumn).Interior.ColorIndex < xlNone
Then
numViolations = numViolations + 1
End If
End If
Next nthColumn
Next nthRow

GetViolationCount = numViolations
End Function


"dev-all" wrote:

I have a spreadsheet that tracks "violations" by setting background colors of
cells and by placing text in certain "custom" cells. I wrote a UDF to
calculate how many violations appear in a specified range. I then call this
UDF using a formula like...

=GetViolationCount($A1:$AF1)

It works great in a worksheet. In other words, every cell in which I call
the UDF with an appropriate range, the cell shows the correct value.
In another portion of the spreadsheet, I have UserForm that searches for
any of several, user chosen, criteria. One of the choices is "has
violations". So I want to look at the cell that contains the result of my
GetViolationCount UDF and simply test if it is greater than 0. ie....

CInt(allDataRange(curRow, kViolationCountColumnIndex).Value) 0

No matter which property I use to access that cell, I've tried Value,
Value2 and Text among others, I always get back an empty string. Other
properties like Address etc return appropriate values. Am I trying to do
something impossible or am I being stupid?

Here is the entire UDF if it matters...

Public Function GetViolationCount(inRange As Range) As Integer
Dim numViolations As Integer

numViolations = 0

Dim numRows As Integer
Dim nthRow As Integer

numRows = inRange.Rows.Count

Dim numCols As Integer
Dim nthColumn As Integer

numCols = inRange.Columns.Count

For nthRow = 1 To numRows
For nthColumn = kFirstViolationColumnIndex To numCols
Dim tStr As String
tStr = CStr(inRange(nthRow, nthColumn).Value)

If IsCustomColumn(nthColumn) Then
If inRange(nthRow, nthColumn).Value < "" Then
numViolations = numViolations + 1
End If
Else
If inRange(nthRow, nthColumn).Interior.colorIndex < xlNone
Then
numViolations = numViolations + 1
End If
End If
Next nthColumn
Next nthRow

GetViolationCount = numViolations
End Function

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default UDF Question

Hi Barb,
Thank you for your time and efforts on this.

Yes, I do want nthRow to start at 1. I want to be able to support ranges
like $A1:$AF1 as well as ranges like $A1:$AF20 where the difference would be
counting violations from one or more incidents. I also want nthColumn to
start at 1 too but for debugging purposes, I have "optimized" it for my
current data set.
Given 2 example ranges of $A10:$AF10 and $A5:$AF5 2, when nthRow = 1, the
code will start at row 10 and 5 respectively. Please correct me if I am
wrong but I have a lot of code that shows that to be correct.
kFirstViolationColumnIndex is defined in a Module (the only Module in my
project) as...

Public Const kFirstViolationColumnIndex = 8

The GetViolationCount function is defined there as well. I apologize for
the tStr related code. That is remnant debugging code.

Yes, IsCustomColumn is another function also defined in that Module. It
is...

Public Function IsCustomColumn(inColumnIndex As Integer) As Boolean
Select Case inColumnIndex
Case kCustom1ColumnIndex, kCustom2ColumnIndex, kCustom3ColumnIndex,
kCustom4ColumnIndex
IsCustomColumn = True
Case Else
IsCustomColumn = False
End Select
End Function

For completeness...

Public Const kCustom1ColumnIndex = 14
Public Const kCustom2ColumnIndex = 23
Public Const kCustom3ColumnIndex = 27
Public Const kCustom4ColumnIndex = 31

inRange(nthRow, nthColumn).Value is not another function. I am accessing
cell(nthRow, nthColumn) of the range that was passed in. Assuming the Range
$A1:$AF1 was passed in and nthRow = 1 and nthColumn = 3, then that point of
the code will access the value of cell $C$1.

"Barb Reinhardt" wrote:

I've added some comments to your code.

Public Function GetViolationCount(inRange As Range) As Integer
Dim numViolations As Integer
Dim kFirstViolationColumnIndex As Long

numViolations = 0

Dim numRows As Integer
Dim nthRow As Integer

numRows = inRange.Rows.Count

Dim numCols As Integer
Dim nthColumn As Integer

numCols = inRange.Columns.Count

For nthRow = 1 To numRows
'Do you really want nthRow to start at 1 if the range starts in row
(say) 10
For nthColumn = kFirstViolationColumnIndex To numCols
'Where is kFirstViolationCOlumnIndex defined
Dim tStr As String
tStr = CStr(inRange(nthRow, nthColumn).Value)

If Iscustomcolumn(nthColumn) Then '<~~~I presume this is
another function
'Do you have this defined somewhere?
If inRange(nthRow, nthColumn).Value < "" Then
'<~~~another function?
numViolations = numViolations + 1
End If
Else
If inRange(nthRow, nthColumn).Interior.ColorIndex < xlNone
Then
numViolations = numViolations + 1
End If
End If
Next nthColumn
Next nthRow

GetViolationCount = numViolations
End Function


"dev-all" wrote:

I have a spreadsheet that tracks "violations" by setting background colors of
cells and by placing text in certain "custom" cells. I wrote a UDF to
calculate how many violations appear in a specified range. I then call this
UDF using a formula like...

=GetViolationCount($A1:$AF1)

It works great in a worksheet. In other words, every cell in which I call
the UDF with an appropriate range, the cell shows the correct value.
In another portion of the spreadsheet, I have UserForm that searches for
any of several, user chosen, criteria. One of the choices is "has
violations". So I want to look at the cell that contains the result of my
GetViolationCount UDF and simply test if it is greater than 0. ie....

CInt(allDataRange(curRow, kViolationCountColumnIndex).Value) 0

No matter which property I use to access that cell, I've tried Value,
Value2 and Text among others, I always get back an empty string. Other
properties like Address etc return appropriate values. Am I trying to do
something impossible or am I being stupid?

Here is the entire UDF if it matters...

Public Function GetViolationCount(inRange As Range) As Integer
Dim numViolations As Integer

numViolations = 0

Dim numRows As Integer
Dim nthRow As Integer

numRows = inRange.Rows.Count

Dim numCols As Integer
Dim nthColumn As Integer

numCols = inRange.Columns.Count

For nthRow = 1 To numRows
For nthColumn = kFirstViolationColumnIndex To numCols
Dim tStr As String
tStr = CStr(inRange(nthRow, nthColumn).Value)

If IsCustomColumn(nthColumn) Then
If inRange(nthRow, nthColumn).Value < "" Then
numViolations = numViolations + 1
End If
Else
If inRange(nthRow, nthColumn).Interior.colorIndex < xlNone
Then
numViolations = numViolations + 1
End If
End If
Next nthColumn
Next nthRow

GetViolationCount = numViolations
End Function

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default UDF Question


Function AnyViolations() As Boolean
Dim x As Boolean
'The cell checked is 4 down and 1 to the right of the active cell.
x = CBool(ActiveCell(5, 2).Value 0)
AnyViolations = x
End Function

Sub FindOut()
MsgBox AnyViolations
End Sub
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware



"dev-all"
wrote in message
I have a spreadsheet that tracks "violations" by setting background colors of
cells and by placing text in certain "custom" cells. I wrote a UDF to
calculate how many violations appear in a specified range. I then call this
UDF using a formula like...

=GetViolationCount($A1:$AF1)

It works great in a worksheet. In other words, every cell in which I call
the UDF with an appropriate range, the cell shows the correct value.
In another portion of the spreadsheet, I have UserForm that searches for
any of several, user chosen, criteria. One of the choices is "has
violations". So I want to look at the cell that contains the result of my
GetViolationCount UDF and simply test if it is greater than 0. ie....

CInt(allDataRange(curRow, kViolationCountColumnIndex).Value) 0

No matter which property I use to access that cell, I've tried Value,
Value2 and Text among others, I always get back an empty string. Other
properties like Address etc return appropriate values. Am I trying to do
something impossible or am I being stupid?

Here is the entire UDF if it matters...

Public Function GetViolationCount(inRange As Range) As Integer
Dim numViolations As Integer

numViolations = 0

Dim numRows As Integer
Dim nthRow As Integer

numRows = inRange.Rows.Count

Dim numCols As Integer
Dim nthColumn As Integer

numCols = inRange.Columns.Count

For nthRow = 1 To numRows
For nthColumn = kFirstViolationColumnIndex To numCols
Dim tStr As String
tStr = CStr(inRange(nthRow, nthColumn).Value)

If IsCustomColumn(nthColumn) Then
If inRange(nthRow, nthColumn).Value < "" Then
numViolations = numViolations + 1
End If
Else
If inRange(nthRow, nthColumn).Interior.colorIndex < xlNone
Then
numViolations = numViolations + 1
End If
End If
Next nthColumn
Next nthRow

GetViolationCount = numViolations
End Function
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default UDF Question

Hi Jim,
Thank you for your time and efforts on this problem. This is very
interesting. Accessing the value through ActiveCell produces the correct
result. My code is highly selection and worksheet independent so I can't use
this directly but I will try accessing the cells via alternate means to see
if I can find something compatible.

"Jim Cone" wrote:


Function AnyViolations() As Boolean
Dim x As Boolean
'The cell checked is 4 down and 1 to the right of the active cell.
x = CBool(ActiveCell(5, 2).Value 0)
AnyViolations = x
End Function

Sub FindOut()
MsgBox AnyViolations
End Sub
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware



"dev-all"
wrote in message
I have a spreadsheet that tracks "violations" by setting background colors of
cells and by placing text in certain "custom" cells. I wrote a UDF to
calculate how many violations appear in a specified range. I then call this
UDF using a formula like...

=GetViolationCount($A1:$AF1)

It works great in a worksheet. In other words, every cell in which I call
the UDF with an appropriate range, the cell shows the correct value.
In another portion of the spreadsheet, I have UserForm that searches for
any of several, user chosen, criteria. One of the choices is "has
violations". So I want to look at the cell that contains the result of my
GetViolationCount UDF and simply test if it is greater than 0. ie....

CInt(allDataRange(curRow, kViolationCountColumnIndex).Value) 0

No matter which property I use to access that cell, I've tried Value,
Value2 and Text among others, I always get back an empty string. Other
properties like Address etc return appropriate values. Am I trying to do
something impossible or am I being stupid?

Here is the entire UDF if it matters...

Public Function GetViolationCount(inRange As Range) As Integer
Dim numViolations As Integer

numViolations = 0

Dim numRows As Integer
Dim nthRow As Integer

numRows = inRange.Rows.Count

Dim numCols As Integer
Dim nthColumn As Integer

numCols = inRange.Columns.Count

For nthRow = 1 To numRows
For nthColumn = kFirstViolationColumnIndex To numCols
Dim tStr As String
tStr = CStr(inRange(nthRow, nthColumn).Value)

If IsCustomColumn(nthColumn) Then
If inRange(nthRow, nthColumn).Value < "" Then
numViolations = numViolations + 1
End If
Else
If inRange(nthRow, nthColumn).Interior.colorIndex < xlNone
Then
numViolations = numViolations + 1
End If
End If
Next nthColumn
Next nthRow

GetViolationCount = numViolations
End Function



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
Excel 2007 Macro/VB Question DDE Question MadDog22 Excel Worksheet Functions 1 March 10th 10 01:47 AM
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good davegb Excel Programming 1 May 6th 05 06:35 PM
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good you [email protected] Excel Programming 0 April 27th 05 07:46 PM
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good you [email protected] Excel Programming 23 April 23rd 05 09:26 PM
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good you [email protected] Excel Programming 0 April 22nd 05 03:30 PM


All times are GMT +1. The time now is 04:23 AM.

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"