How to pass a User defined type as a function parameter

Kmanbozzy

New Member
Joined
Apr 18, 2016
Messages
18
I use VBA all the time but have never tried to use Type. I haven't quite figured it out. Not sure how to pass the parameter into a function. Basically i have a function that i want to call to find a value in a range based on a condition. The condition is a type and based on the type i will find the value where the condition is met. maybe there is a better way to do it? I haven't been able to find anything similar to what I'm doing. Maybe i over looked a thread.

Code:
Type ForEachCType
    OffsetR As Integer
    OffsetC As Integer
    EqualTo As Boolean
    GreaterThan As Boolean
    GTorEqualto As Boolean
    LessThan As Boolean
    LTorEqualto As Boolean
End Type

Sub test()
a = ConditionalFind("Jon", Range("BJ:BW"), ForEachCType.OffsetR, "G")
End Sub

 'ForEachCType.OffsetR this is where i don't know how to call or assign this parameter a value
    
Function ConditionalFind(SearchValue, SearchRange As Range, ForEachConditionType As ForEachCType, ForEachConditionValue)
   'code stuffs yay
End Function
 
Last edited:

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
To use your user-defined data type, first declare a variable to hold the data type...

Code:
Dim x As ForEachCType

Then assign properties for the object...

Code:
x.OffsetR = 10
x.OffsetC = 12
'etc
'
'

Then pass the object as follows...

Code:
a = ConditionalFind("Jon", Range("BJ:BW"), x, "G")

Then, in your called function, you can access the desired property like this...

Code:
Debug.Print ForEachConditionType.OffsetR

Hope this helps!
 
Upvote 0
awesome! so this is great for calling a sub, then the function. so if i wanted to use this a worksheet function in a cell like =conditionalfind(), id probably have to call a function to dim my variables then call the actual function? since i cant really dim from a cell
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top