+ Reply to Thread
Results 1 to 6 of 6

VBA Data Type Error

  1. #1
    Registered User
    Join Date
    03-07-2014
    Location
    Atlanta, Georgia, USA
    MS-Off Ver
    Excel 2013
    Posts
    3

    Question VBA Data Type Error

    I'm trying to write my first custom function, and I've hit roadblock after roadblock. I found answers for all except this last error message. After this, my function should be finished.

    I've found the line that prompts the error, and I've got no clue as to why it won't work. Here's the last snippet of my code. (the 2 dimension table array and entry variable were declared higher up in the code and they've been working fine)

    Please Login or Register  to view this content.
    Running my code with this as shown returns the #VALUE! error (A value used in the formula is of the wrong data type). Now if I remove the "counter = counter + 1" line, then the code runs properly. I've tried making the counter variable every possible data type in some last-ditch effort, but with no luck. Why would this not work? Any insights would be greatly appreciated.
    Last edited by mlehman6; 01-15-2018 at 05:22 PM.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: VBA Data Type Error

    Can you post the entire function and/or upload a sample workbook?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    03-07-2014
    Location
    Atlanta, Georgia, USA
    MS-Off Ver
    Excel 2013
    Posts
    3

    Re: VBA Data Type Error

    Quote Originally Posted by Norie View Post
    Can you post the entire function and/or upload a sample workbook?
    I think I attached the workbook, if not here's the code in full:

    Please Login or Register  to view this content.
    Attached Files Attached Files

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,343

    Re: VBA Data Type Error

    the #VALUE! error (A value used in the formula is of the wrong data type).
    I'm not sure I would assign this kind of specific meaning to a spreadsheet #VALUE error. In my experience with UDF's, the #VALUE error really means "I came across some kind of untrapped runtime error that I could not resolve so I will put #VALUE here instead". This could be a data type mismatch error, or it could be a syntax error, or it could be an untrapped divide by 0 error or whatever.

    You indicate that removing the counter=counter+1 statement "resolves" the error. Since all counter is doing is specifying what element from table(i,j) to return, I would guess that counter becomes a value that is incompatible with the table() array (probably why Norie wants to see how table() is defined and filled). I think the next thing I would do would be to add a stop statement (probably right before counter is initialized), then step through the loop -- paying careful attention to what counter, table, arr, entry, and any other variables are doing using the locals and watch windows.
    Please Login or Register  to view this content.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: VBA Data Type Error

    If I uncomment this then the function will return #VALUE!.
    Please Login or Register  to view this content.
    This is because of a subscript out of range error here,
    Please Login or Register  to view this content.
    which is caused by counter02 exceeding the bounds of the array table.

    Not sure how to fix the function because, to be honest, I'm not sure what it's meant to be doing.
    Last edited by Norie; 01-15-2018 at 05:22 PM.

  6. #6
    Registered User
    Join Date
    03-07-2014
    Location
    Atlanta, Georgia, USA
    MS-Off Ver
    Excel 2013
    Posts
    3

    Re: VBA Data Type Error

    Thank you for your help! I forgot that defining an array from zero gives it one more value than the number(arr(4) has 5 entries). After adjusting that (and the else for other reasons) my function works properly. Thank you for all your help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Data mismatch type error
    By kelseygueldalewis in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-06-2017, 01:16 PM
  2. [SOLVED] Data Type Mismatch Error
    By kelseygueldalewis in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-06-2017, 12:53 PM
  3. Invalid Data type error
    By kiran527 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-12-2012, 11:00 PM
  4. wrong data type #VALUE! error
    By osteolass in forum Excel General
    Replies: 3
    Last Post: 06-20-2011, 07:02 PM
  5. Data type error when using VLOOKUP
    By favoritepsalm1 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-13-2007, 10:02 AM
  6. [SOLVED] Tricky data type error
    By RB Smissaert in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-07-2006, 08:00 PM
  7. Strange data type error
    By johnsday in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-20-2006, 12:10 PM

Tags for this Thread

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