+ Reply to Thread
Results 1 to 3 of 3

Storing userform textbox inputs as currency in an array

  1. #1
    Registered User
    Join Date
    06-10-2014
    Posts
    15

    Storing userform textbox inputs as currency in an array

    I have a userform where the user enters prices for up to 12 items in textbox controls, and these prices are stored in an array. The textboxes are titled tbPrice1, tbPrice2, etc. I'm having an issue with the line of code I am using to store the values in an array. Every time I attempt to store them, I get runtime error 13 type mismatch, but I don't understand why. What am I doing wrong?

    dim PartPrices(1 to 12) as Currency

    For x = 1 to 12
    PartPrices(x) = IIf(Trim(Me.Controls("tbPrice" & x).Value) & vbNullString = vbNullString, CCur(0), CCur(Trim(Me.Controls("tbPrice" & x).Value)))
    Next

    A little more explanation here. Not all 12 parts will have prices, so I use the IIf statement to store the value 0 whenever the user has left a price field blank. I use trim in case they leave spaces in the price textbox or something. When the field is not blank, I take the value entered in the textbox, convert it to currency and attempt to store it in the relevant element of the array.

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

    Re: Storing userform textbox inputs as currency in an array

    In an Iif statement both the true and false expressions are evaluated.

    So if a textbox is empty this will still get calculated and will cause an error.
    Please Login or Register  to view this content.
    Try using If End If.
    Please Login or Register  to view this content.
    [/code]
    If posting code please use code tags, see here.

  3. #3
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Storing userform textbox inputs as currency in an array

    hi CaptainK, if you declare your array as

    Please Login or Register  to view this content.
    you will get it fiiled with zeroes and the final code might look like:

    Please Login or Register  to view this content.

+ 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. Format Textbox on UserForm as Currency
    By paulsouthampton in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-06-2015, 11:37 AM
  2. [SOLVED] Help with storing variable data from several inputs
    By grolschey in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-18-2013, 11:43 AM
  3. Replies: 3
    Last Post: 06-23-2013, 12:13 AM
  4. How do you format a TextBox on a UserForm as Currency
    By rkjudy in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-09-2011, 11:08 PM
  5. Replies: 5
    Last Post: 11-18-2010, 05:37 PM

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