Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi NG
I have made a function, that works perfectly allright, doing some calculation based on the contents of two ranges, but I would like to pass the two ranges as arguments in stead of "hard-coding" them into the function. However i can'øt get it to work. This is the working code. Function JaNej() As String n = 0 m = 0 Application.Volatile For Each c In Range("d28:i28").Cells If c.Value = "nej" Then n = n + 1 Else n = n + 0 End If Next c For Each c In Range("d30:i30").Cells If c.Value = "nej" Then m = m + 1 Else m = m + 0 End If Next c If n 0 Then JaNej = "Nej" Else JaNej = "Ja" End If End Function Jan |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi
try this. Alos it's better the declare your variables explicitly Function JaNej(rng1 As Range, rng2 As Range) As String Dim c As Range Dim n As Integer, m As Integer n = 0 m = 0 Application.Volatile For Each c In rng1.Cells If c.Value = "nej" Then n = n + 1 Else n = n + 0 End If Next c For Each c In rng2.Cells If c.Value = "nej" Then m = m + 1 Else m = m + 0 End If Next c If n 0 Then JaNej = "Nej" Else JaNej = "Ja" End If 'clean up Set c = Nothing End Function -----Original Message----- Hi NG I have made a function, that works perfectly allright, doing some calculation based on the contents of two ranges, but I would like to pass the two ranges as arguments in stead of "hard-coding" them into the function. However i can'øt get it to work. This is the working code. Function JaNej() As String n = 0 m = 0 Application.Volatile For Each c In Range("d28:i28").Cells If c.Value = "nej" Then n = n + 1 Else n = n + 0 End If Next c For Each c In Range("d30:i30").Cells If c.Value = "nej" Then m = m + 1 Else m = m + 0 End If Next c If n 0 Then JaNej = "Nej" Else JaNej = "Ja" End If End Function Jan . |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Thanks. I know about the declaration part, but I just did it the "easy" way.
But now it works. Jan "Richard Daniels" skrev i en meddelelse ... Hi try this. Alos it's better the declare your variables explicitly Function JaNej(rng1 As Range, rng2 As Range) As String Dim c As Range Dim n As Integer, m As Integer n = 0 m = 0 Application.Volatile For Each c In rng1.Cells If c.Value = "nej" Then n = n + 1 Else n = n + 0 End If Next c For Each c In rng2.Cells If c.Value = "nej" Then m = m + 1 Else m = m + 0 End If Next c If n 0 Then JaNej = "Nej" Else JaNej = "Ja" End If 'clean up Set c = Nothing End Function -----Original Message----- Hi NG I have made a function, that works perfectly allright, doing some calculation based on the contents of two ranges, but I would like to pass the two ranges as arguments in stead of "hard-coding" them into the function. However i can'øt get it to work. This is the working code. Function JaNej() As String n = 0 m = 0 Application.Volatile For Each c In Range("d28:i28").Cells If c.Value = "nej" Then n = n + 1 Else n = n + 0 End If Next c For Each c In Range("d30:i30").Cells If c.Value = "nej" Then m = m + 1 Else m = m + 0 End If Next c If n 0 Then JaNej = "Nej" Else JaNej = "Ja" End If End Function Jan . |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
You can also make this a little cleaner. Since you're adding 0 to
the value, you can eliminate the Elses: Function JaNej(rng1 As Range, rng2 As Range) As String Dim c As Range Dim n As Integer, m As Integer Application.Volatile n = 0 m = 0 For Each c In rng1.Cells If c.Value = "nej" Then n = n + 1 Next c For Each c In rng2.Cells If c.Value = "nej" Then m = m + 1 Next c If n 0 Then JaNej = "Nej" Else JaNej = "Ja" End If 'clean up Set c = Nothing End Function Also, based on the above, since you're passing the ranges as arguments, I don't think you need the Application.Volatile any longer. You could also use the built-in COUNTIF() function: Function JaNej(rng1 As Range, rng2 As Range) As String Dim c As Range Dim n As Integer, m As Integer n = Application.CountIf(rng1, "nej") m = Application.CountIf(rng2, "nej") If n 0 Then JaNej = "Nej" Else JaNej = "Ja" End If 'clean up Set c = Nothing End Function In article , "Richard Daniels" wrote: i try this. Alos it's better the declare your variables explicitly Function JaNej(rng1 As Range, rng2 As Range) As String Dim c As Range Dim n As Integer, m As Integer n = 0 m = 0 Application.Volatile For Each c In rng1.Cells If c.Value = "nej" Then n = n + 1 Else n = n + 0 End If Next c For Each c In rng2.Cells If c.Value = "nej" Then m = m + 1 Else m = m + 0 End If Next c If n 0 Then JaNej = "Nej" Else JaNej = "Ja" End If 'clean up Set c = Nothing End Function |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Passing a range name as an argument to the Index Function | Excel Discussion (Misc queries) | |||
Passing a UDF as an argument to a UDF | Excel Discussion (Misc queries) | |||
passing a variable as an argument to a function | Excel Programming | |||
Passing an argument to a quote | Excel Programming | |||
Passing an argument to a quote | Excel Programming |