+ Reply to Thread
Results 1 to 11 of 11

What's wrong with this code?

  1. #1
    Registered User
    Join Date
    09-19-2011
    Location
    oc,ca
    MS-Off Ver
    Excel 2003
    Posts
    44

    What's wrong with this code?

    I have a main form with a listbox named listbox1 and a function named FindTextInListbox.

    Please Login or Register  to view this content.
    When I call the function above with the code below, I get a type mismatch error code 13

    Please Login or Register  to view this content.
    I'm 100% sure that Listbox1 exists in my form and that is is referenced correctly. Since the function has only 2 args and I'm sure the string arg is correct, then the error must be related to the listbox arg. Some help? Thanks
    Last edited by Leith Ross; 09-28-2012 at 04:17 PM. Reason: Added Code Tags

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: What's wrong with this code?

    Hello david90 ,

    It is a little difficult to say based on the code. Is this a VBA UserForm or VB UserFom?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    09-19-2011
    Location
    oc,ca
    MS-Off Ver
    Excel 2003
    Posts
    44

    Re: What's wrong with this code?

    Its a vba userform

  4. #4
    Registered User
    Join Date
    09-19-2011
    Location
    oc,ca
    MS-Off Ver
    Excel 2003
    Posts
    44

    Re: What's wrong with this code?

    I got the code from a VB program and it works in VB. When I copied it to my vba app, it gave me the error.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: What's wrong with this code?

    Hello david90 ,

    That is because VB and VBA are very different languages. While VB provides you with the window handle (hWnd) to the ListBox, VBA does not. In VBA you must use more API calls to obtain the hWnd for the ListBox.

  6. #6
    Registered User
    Join Date
    09-19-2011
    Location
    oc,ca
    MS-Off Ver
    Excel 2003
    Posts
    44

    Re: What's wrong with this code?

    Quote Originally Posted by Leith Ross View Post
    Hello david90 ,

    That is because VB and VBA are very different languages. While VB provides you with the window handle (hWnd) to the ListBox, VBA does not. In VBA you must use more API calls to obtain the hWnd for the ListBox.
    I get the error before excel vba executes the codes in the FindTextInListBox function. This means that that the error has something to do with the args. The error has something to do with how I pass the listbox control to the function.

    Is how I pass the control listbox to a function correct?
    Last edited by david90; 09-28-2012 at 05:02 PM.

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: What's wrong with this code?

    Hello david90 ,

    Again it is difficult to answer without seeing the full code. You are using SendMessage which is an API call. To use API calls in VBA, you must first declare them like below. If you are not familiar with API calls and how to use them, I would advise against using them. These calls have limited error trapping and are low level system calls. Improper use of this code can damage your system.
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    09-19-2011
    Location
    oc,ca
    MS-Off Ver
    Excel 2003
    Posts
    44

    Re: What's wrong with this code?

    Yes you might be right about hwnd, but the error that I'm talking about is not related to hwnd.

    To isolate the cause of the error, I created the code below.

    Please Login or Register  to view this content.
    When i run the sub main above, I get an error 13 type mismatch. The error couldn't be because of the hwnd problem because there aren't any code relating to hwnd. it looks like how I pass the object listbox2 is incorrect but I have no idea why it's incorrect.
    Last edited by Leith Ross; 09-28-2012 at 06:02 PM.

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: What's wrong with this code?

    Hello david90,

    If SearchListBox is in the General Declarations section of the UserForm, you should not get this error. If this macro is in a standard VBA module, your reference to ListBox1 must be prefixed with the UserForm name. Also, the UserForm must be loaded into memory.
    Please Login or Register  to view this content.

  10. #10
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: What's wrong with this code?

    declare the parameter as msforms.listbox instead of just listbox. excel has a listbox object too ;-)
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  11. #11
    Registered User
    Join Date
    09-19-2011
    Location
    oc,ca
    MS-Off Ver
    Excel 2003
    Posts
    44

    Re: What's wrong with this code?

    Quote Originally Posted by JosephP View Post
    declare the parameter as msforms.listbox instead of just listbox. excel has a listbox object too ;-)
    thank you. that fixed the problem .

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1