|
Microsoft Office Application Development
|
|
Finding cells matching a specific property using the CallByName functionIntroductionI thought it would be nice to have a generic VBA function to which we could pass a range object, a string indicating a property of the object and the property's value, which would then return all cells matching that criteria. CallByNameI decided it was time to explore the CallByName function, introduced with Office 2000. According to Excel XP VBA Help: CallByName FunctionExecutes a method of an object, or sets or returns a property of an object. Syntax CallByName(object, procname, calltype,[args()]) The CallByName function syntax has these named arguments:
Suppose we'd want to find out the colorindex of a cell's interior: Sub test() Since I would like to make this method a bit more general, I would like to just pass the cell object and the entire "procname" in a string: Sub test() Unfortunately, this does not work, "procname" only accepts a single entity (Property, Method or Object). So it is necessary to split up the "procname" string into it's individual elements. Something like this (note: Excel 97 doesn't have the Split function, nor the CallByName function): Dim lProps As Long So to get at the colorindex property of the Interior object of the Cell object, we need to loop through the variant vProps: For lCount = 0 To lProps - 1 We stop the loop at the one-but-last element of vProps, because all of the elements except the last one will be objects and the last one will be the property we're interested in. Then we get the property of the last object the loop has given us: CallByName(oTemp, vProps(lProps), VbGet) The complete function is shown below: Function FindCells(ByRef oRange As Range, ByVal sProperties As String, _ A small example of its use, selecting all cells with a white fill: Sub UseFindCellsExample() FeedbackSince you have managed to reach the end of this article, maybe you would care to write me a small message, expressing your opinion on this article? Click here to write an email message to me (Maybe I'll add a feedback page with the feedback I get). | ||||||||||||||||||||||||
Use the contact page to issue
questions or comments about this website. |