Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3681230
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:40:02+00:00 2026-05-19T03:40:02+00:00

I am trying to create a function for excel-VBA. This function is used to

  • 0

I am trying to create a function for excel-VBA. This function is used to calculate the index of a certain string in a combobox or listbox. I would like the function to have the following signature:

Public Function MyIndexOf(list As ???, str As String) As Integer

My question is if there is a common superclass to Combobox and Listbox so i can replace ??? with some class.

Maybe there is another way of doing IndexOf. Suggestions are welcome but i want it to be done in a common separate function.

Anyone have a suggestion on how to solve this?

Thanks in advance
/Patrik

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T03:40:03+00:00Added an answer on May 19, 2026 at 3:40 am

    There is no inheritance in VBA (or VB 6), so unfortunately there’s no such thing as a superclass.

    You do, however, have the benefit of very loose typing. You can declare the variable as a Variant, which is a universal data type that can hold any type of value, including a control. Generally, you should try and avoid using the Variant type if at all possible, but in this case, you have little choice.

    So you could declare your function like this:

    Public Function MyIndexOf(list As Variant, str As String) As Integer
        ' Do work here...
    End Function
    

    Of course, remembering that a Variant data type can really hold any type of value, and that the type is not known until run-time, a good defensive programmer would check to make sure that the specified type is not an Integer or a String or even a control that doesn’t expose the same public members as a ComboBox or ListBox control (thus breaking the code you wrote expecting one that does).

    You can determine the actual type of a Variant at runtime using the TypeName function. So you could simply modify the above function to include a guard clause to protect against anything other than a ComboBox or ListBox control being passed in:

    Public Function MyIndexOf(list As Variant, str As String) As Integer
        If (Not TypeName(list) = "ComboBox") And (Not TypeName(list) = "ListBox") Then
            MsgBox "Wrong type of list variable was specified."
        Else
            ' Do work here...
        End If
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to create a simple log function in my Excel VBA project. I
I'm trying to create a function that would append two vectors together, but I
I'm trying to create a function that would dynamically allocate an array, sets the
I'm trying to create an export Excel/CSV function that will iterate through a custom
I’m trying to create a COM User Defined Function automation add in for Excel
I'm trying to create a string and then execute it. By this I mean,
I'm trying to create function that will do this same thing for 3 different
Im trying to create a function to check for a substring within a string
Im trying to create a function in my VBA where if the record they
im trying to create this function in javascript but its not working, im a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.