जैसा लिखा है, आपका फ़ंक्शन केवल दो श्रेणियों को तर्क के रूप में स्वीकार करता है।
फ़ंक्शन में उपयोग की जाने वाली श्रेणियों की एक चर संख्या की अनुमति देने के लिए, आपको अपनी तर्क सूची में पैरामरे संस्करण संस्करण घोषित करने की आवश्यकता है। फिर, आप सरणी में प्रत्येक श्रेणी को बदले में संसाधित कर सकते हैं।
उदाहरण के लिए,
Function myAdd(Arg1 As Range, ParamArray Args2() As Variant) As Double
Dim elem As Variant
Dim i As Long
For Each elem In Arg1
myAdd = myAdd + elem.Value
Next elem
For i = LBound(Args2) To UBound(Args2)
For Each elem In Args2(i)
myAdd = myAdd + elem.Value
Next elem
Next i
End Function
इस समारोह तो एक से अधिक श्रेणियों जोड़ने के लिए वर्कशीट में इस्तेमाल किया जा सकता।
अपने कार्य के लिए, वहाँ सवाल अवधियों में (या कोशिकाओं) कि समारोह के लिए पारित कर सकते हैं जिनमें से कर रहे हैं 'सत्र' और जो कर रहे हैं '' ग्राहक है।
निपटने का सबसे आसान मामला होगा यदि आपने तय किया कि पहली श्रेणी सत्र है और बाद की श्रेणियां ग्राहक हैं।
Function calculateIt(Sessions As Range, ParamArray Customers() As Variant) As Double
'This function accepts a single Sessions range and one or more Customers
'ranges
Dim i As Long
Dim sessElem As Variant
Dim custElem As Variant
For Each sessElem In Sessions
'do something with sessElem.Value, the value of each
'cell in the single range Sessions
Debug.Print "sessElem: " & sessElem.Value
Next sessElem
'loop through each of the one or more ranges in Customers()
For i = LBound(Customers) To UBound(Customers)
'loop through the cells in the range Customers(i)
For Each custElem In Customers(i)
'do something with custElem.Value, the value of
'each cell in the range Customers(i)
Debug.Print "custElem: " & custElem.Value
Next custElem
Next i
End Function
आप सत्र पर्वतमाला और ग्राहकों के किसी भी संख्या की सीमा के किसी भी संख्या शामिल करना चाहते हैं, तो आप एक तर्क है कि समारोह बता इतना है कि यह अलग कर सकते हैं सत्र पर्वतमाला से ग्राहकों लेकर जाएगा शामिल करना होगा।
यह तर्क उस कार्य के लिए पहला, संख्यात्मक, तर्क के रूप में स्थापित किया जा सकता है जो यह पहचान लेगा कि निम्नलिखित में से कितने तर्क सत्र हैं, शेष तर्कों के साथ ग्राहक सीमाएं हैं। समारोह के हस्ताक्षर तो होगा:
Function calculateIt(numOfSessionRanges, ParamAray Args() As Variant)
या यह एक "गार्ड" तर्क है कि अलग करती ग्राहकों पर्वतमाला से सत्र पर्वतमाला हो सकता है। फिर, आपके कोड को यह देखने के लिए प्रत्येक तर्क का परीक्षण करना होगा कि यह गार्ड था या नहीं।
calculateIt(sessRange1,sessRange2,...,"|",custRange1,custRange2,...)
कार्यक्रम तर्क तो की तर्ज पर हो सकता है::
Function calculateIt(ParamArray Args() As Variant)
जैसा कॉल कुछ के साथ शायद: समारोह कैसा दिखेगा
Function calculateIt(ParamArray Args() As Variant) As Double
...
'loop through Args
IsSessionArg = True
For i = lbound(Args) to UBound(Args)
'only need to check for the type of the argument
If TypeName(Args(i)) = "String" Then
IsSessionArg = False
ElseIf IsSessionArg Then
'process Args(i) as Session range
Else
'process Args(i) as Customer range
End if
Next i
calculateIt = <somevalue>
End Function
ParamArray के लिए एक विकल्प के रूप में, संबंध तोड़ना कोशिकाओं के अपने सेट करने के लिए एक परिभाषित किया जाता है नाम निर्दिष्ट करेंगे और उसे नाम पारित कर सकते हैं कर सकते हैं। –