I'm creating a function that allows me to transition properties of controls, that are in a userform, from one state to another, creating an animation effect such as a custom drop-down list.
However, I can't seem to figure out if there is a way to pass the specific property as an argument, to keep the function more dynamic.
Private Sub test()
' Transition the height of Frame1 additional 150
transition Frame1, "Height", 13, 0.2, 150
End Sub
Private Function transition(obj As Object, objProperty As String, _
framesPerSec As Integer, sec As Double, increment As Double)
' Calculate increment steps\time steps
increment = increment / framesPerSec
sec = (sec * 1000) / framesPerSec
' I tried the code below. Might be misunderstanding how `CallByName` works.
' I would use this in place of objProperty below
'Dim Prop As Variant
'Prop = CallByName(Obj, objProperty, VbLet)
Dim index As Long
For index = 1 To framesPerSec
' * Here is the example of what I'm trying to accomplish
obj.objProperty = obj.objProperty + increment
' API sleep function (Milliseconds)
Sleep sec
Next index
End Function
I've tried looking at the VBA callbyname
method, but can't seem to get it to work. Perhaps I didn't use it correctly when trying it?
I'm happy to take any feedback or help in any direction in how to pass control properties to a function?
Let's look at what's being invoked here:
Obj.objProperty = Obj.objProperty + Increment
Ignoring the fact that you're illegally treating a String
local/parameter as a member of Obj
, you have Obj.objProperty
on both sides of the assignment, so syntactically, we have:
[object].[property-let] = [object].[property-get] + [parameter]
In other words, there's no way that can work with a single CallByName
invocation: you need to first compute the RHS of the assignment, and in order to do that you need to invoke the Property Get
member to read the current value, then add the increment
, and then invoke the Property Let
member to write the new value.
Dim currentValue As Double
currentValue = CallByName(obj, objProperty, vbGet)
So that's the read part. Then you can do the write part:
CallByName obj, objProperty, vbLet, currentValue + increment
Not sure what the Prop
local Variant
variable would be used for.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加