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

Hi All,


I am trying to write UDF's functions for calculating percentiles from
different sheets which I have pasted below.
if I want to calculate mean for variable1 i will give =mean(variable1)
will give me the averge value of variable from the 3 sheets.

Now can you please give me any suggestion on who to write a udf for a
expression say for example?
variable11 = variable1*variable2/variable3
variable12 = (variable4+variable5)/100

Your help will be highly appreciated.


Here

Public Function numvalidn(ByVal metric As Variant)
Application.Volatile (True)
k = 1
Do While (Len(Worksheets("DataSheet1").Cells(k, 1)) 0)
If (InStr(Trim(UCase(Worksheets("DataSheet1").Cells(k , 1))),
Trim(UCase(metric))) 0) Then
i = k
Exit Do
End If
k = k + 1
Loop
j = 1
n = 0
Cmax3 = ThisWorkbook.Worksheets("DataSheet3").UsedRange.Co lumns.Count
Cmax2 = ThisWorkbook.Worksheets("DataSheet2").UsedRange.Co lumns.Count
Cmax1 = ThisWorkbook.Worksheets("DataSheet1").UsedRange.Co lumns.Count
For m = 1 To Cmax1
If j = 256 Then Exit For
If (IsNumeric(Trim(Worksheets("DataSheet1").Cells(i, j))) And
Len(Trim(Worksheets("DataSheet1").Cells(i, j))) 0) Then n = n + 1
j = j + 1
Next
j = 1
For m = 1 To Cmax2
If j = 256 Then Exit For
If (IsNumeric(Trim(Worksheets("DataSheet2").Cells(i, j))) And
Len(Trim(Worksheets("DataSheet2").Cells(i, j))) 0) Then n = n + 1
j = j + 1
Next
j = 1
For m = 1 To Cmax3
If j = 256 Then Exit For
If (IsNumeric(Trim(Worksheets("DataSheet3").Cells(i, j))) And
Len(Trim(Worksheets("DataSheet3").Cells(i, j))) 0) Then n = n + 1
j = j + 1
Next
numvalidn = n
End Function

Private Function percentile25(ByVal metric As Variant)
Application.Volatile (True)
Dim mrange As Variant
mrange = vrange(metric)
percentile25 = Application.WorksheetFunction.Percentile(mrange, 0.25)
End Function

Private Function percentile50(ByVal metric As Variant)
Application.Volatile (True)
Dim mrange As Variant
mrange = vrange(metric)
percentile50 = Application.WorksheetFunction.Percentile(mrange, 0.5)
End Function

Private Function percentile75(ByVal metric As Variant)
Application.Volatile (True)
Dim mrange As Variant
mrange = vrange(metric)
percentile75 = Application.WorksheetFunction.Percentile(mrange, 0.75)
End Function
Private Function mmin(ByVal metric As Variant)
Application.Volatile (True)
Dim mrange As Variant
mrange = vrange(metric)
mmin = Application.WorksheetFunction.Min(mrange)
End Function
Private Function mmax(ByVal metric As Variant)
Application.Volatile (True)
Dim mrange As Variant
mrange = vrange(metric)
mmax = Application.WorksheetFunction.Max(mrange)
End Function
Private Function mean(ByVal metric As Variant)
Application.Volatile (True)
Dim mrange As Variant
mrange = vrange(metric)
mean = Application.WorksheetFunction.Average(mrange)
End Function

Public Function vrange(ByVal metric As Variant) As Variant
Application.Volatile (True)
Dim mrange()
Dim i, k, m, j As Integer
Dim n As Long
Cmax3 = ThisWorkbook.Worksheets("DataSheet3").UsedRange.Co lumns.Count
Cmax2 = ThisWorkbook.Worksheets("DataSheet2").UsedRange.Co lumns.Count
Cmax1 = ThisWorkbook.Worksheets("DataSheet1").UsedRange.Co lumns.Count
k = 1
Do While (Len(Worksheets("DataSheet1").Cells(k, 2)) 0)
If (InStr(Trim(UCase(Worksheets("DataSheet1").Cells(k , 1))),
Trim(UCase(metric))) 0) Then
i = k
Exit Do
End If
k = k + 1
Loop
n = numvalidn(metric)
ReDim mrange(1 To n)
n = 1
j = 1
For m = 1 To Cmax1
If j = 256 Then Exit For
If (Len(Trim(Worksheets("DataSheet1").Cells(i, j))) 0) Then
If (IsNumeric(Trim(Worksheets("DataSheet1").Cells(i, j)))) Then
mrange(n) = Worksheets("DataSheet1").Cells(i, j)
n = n + 1
End If
End If
j = j + 1
Next m
j = 1
For m = 1 To Cmax2
If j = 256 Then Exit For
If (Len(Trim(Worksheets("DataSheet2").Cells(i, j))) 0) Then
If (IsNumeric(Trim(Worksheets("DataSheet2").Cells(i, j)))) Then
mrange(n) = Worksheets("DataSheet2").Cells(i, j)
n = n + 1
End If
End If
j = j + 1
Next m
j = 1
For m = 1 To Cmax3
If j = 256 Then Exit For
If (Len(Trim(Worksheets("DataSheet3").Cells(i, j))) 0) Then
If (IsNumeric(Trim(Worksheets("DataSheet3").Cells(i, j)))) Then
mrange(n) = Worksheets("DataSheet3").Cells(i, j)
n = n + 1
End If
End If
j = j + 1
Next m
vrange = mrange
End Function





Regards,
Srinivas

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 asp and expressions web Healing Excel Discussion (Misc queries) 0 December 7th 07 05:11 PM
Excel and VBA - If and Find Expressions Ronan Excel Programming 2 August 28th 07 10:21 AM
Substring in excel? How about regular expressions? Samuel Excel Discussion (Misc queries) 8 May 22nd 06 04:43 PM
Regular expressions in Excel vigi98 Excel Discussion (Misc queries) 3 November 10th 05 04:40 PM
Regular expressions in Excel? a Excel Programming 8 March 4th 05 05:29 AM


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