+ Reply to Thread
Results 1 to 7 of 7

Textbox value won't assign to variable

Hybrid View

  1. #1
    Registered User
    Join Date
    02-22-2013
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    4

    Question Textbox value won't assign to variable

    I was running tests on my software and found that some of the values it was producing weren't correct.

    I decided to step through the code and noticed that the variables I had assigned to textbox values when hovered over said empty, even though when hovering over the textbox assigned to it, the value inputted by the user showed.

    For Example,

    n = BiTimeSteps_TextBox.Value
    when hovered over said
    n = empty
    even though
    BiTimeSteps_TextBox.Value = 2
    when hovered over.

    So say I have a formula shortly after, that says
    d = n*2
    n when hovered over says empty and d is made 0 when it shouldn't be.

    Someone told me if I switch it around to
    BiTimeSteps_TextBox.Value = n
    it should be recognised but it is still not.


    What could possibly be causing this?

    Copy of my actual coding (I'm trying to price options using the binomial tree pricing method):

     BiCurrentStockPrice_TextBox.Value = S
        BiStrikePrice_TextBox.Value = X
        BiRisk_Free_Rate_TextBox.Value = r
        BiexpTime_TextBox.Value = T
        BiVolatility_TextBox.Value = sigma
        BiTimeSteps_TextBox.Value = n
    
    
    Dim i, j, k As Integer
    Dim p, V, u, d, dt As Double
    
    
    dt = T / n ' This finds the value of dt
    u = Exp(sigma * Sqr(dt)) 'formula for the up factor
    d = 1 - u 'formula for the down factor
    
    'V value of option
    
    'array having the values
    Dim bin() As Double 'is a binomial arrays, it stores the value of each node, there is a loop
    
    'work out the risk free probability
    p = (1 + r - d) / (u - d) 'probability of going up
    
    ReDim bin(n + 1) As Double 'it redims the array, and n+1 is used because it starts from zero
    '------------------------------------------------------------------------------------------------------------------------------
    ''European Call
    
    If BiCall_CheckBox = True Then
    
        For i = 0 To n 'payoffs = value of option at final time
        
        bin(i + 1) = Application.WorksheetFunction.Max(0, (u ^ (n - i)) * (d ^ i) * S - X) 'It takes the max payoff or 0
        Cells(i + 20, n + 2) = bin(i + 1) 'to view payoffs on the isolated column on the right
        
        Next i
    
    
    End If
    
    'european put
    If BiPut_CheckBox = True Then
    
    For i = 0 To n 'payoffs = value of option at final time
    
        bin(i + 1) = Application.WorksheetFunction.Max(0, X - (S * (u * (n - i)) * (d * i))) ' European Put- It takes the max payoff or 0
        
        Cells(i + 20, n + 2) = bin(i + 1) 'to view payoffs on the isolated column on the right
    
    Next i
    
    End If
    
    For k = 1 To n 'backward column loop
    
        For j = 1 To (n - k + 1) 'loop down the column loop
    
            bin(j) = (p * bin(j) + (1 - p) * bin(j + 1)) / (1 + r)
        
            Cells(j + 19, n - k + 2) = bin(j) '' print the values along the column, view of tree
        
        Next j
        
    Next k
    
    Worksheets("Binomial").Cells(17, 2) = bin(1) ' print of the value V
    BiOptionPrice_TextBox = bin(1)
    
    End Sub

  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: Textbox value won't assign to variable

    Where you hovering over before or after the code to assign the textbox value to the variable was executed?

    Could you attach a sample workbook?

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    02-22-2013
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Textbox value won't assign to variable

    Book3.xlsm

    I hovered over the variables once i'd ran the software to see if my programming was processing properly.

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

    Re: Textbox value won't assign to variable

    When code is finished all the variables are destroyed.

    What are you trying to do here?
        BiCurrentStockPrice_TextBox.Value = S
        BiStrikePrice_TextBox.Value = X
        BiRisk_Free_Rate_TextBox.Value = r
        BiexpTime_TextBox.Value = T
        BiVolatility_TextBox.Value = sigma
        BiTimeSteps_TextBox.Value = n
    That's putting the values of S, X, r, T, sigma and n into textboxes on the form.

    Some of those variables aren't even declared, and all of them are empty.

    If you are trying to put the values from the textboxes in the variables then it should be the other way round.
     S = BiCurrentStockPrice_TextBox.Value 
     X = BiStrikePrice_TextBox.Value 
     r = BiRisk_Free_Rate_TextBox.Value 
     T = BiexpTime_TextBox.Value 
     sigma = BiVolatility_TextBox.Value 
     n = BiTimeSteps_TextBox.Value

  5. #5
    Registered User
    Join Date
    02-22-2013
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Textbox value won't assign to variable

    I originally had it that way round and I tried to get a friend to help me and he advised me to flip and see if that helps.

    I've tried declaring all the variables originally I had it as

    dim S,X,r,T,sigma,n As Double

    Then i changed it to declaring them individually and then scrapped it because the variables were still empty. What you can see are the remaining ones I didn't delete due to my indecisiveness as to what to do next.

    And you have said exactly my point. All the variables are empty. They are assigned to textbox values, but when values are entered into the textboxes the variables still remain empty.

    What could I do to possibly solve this?

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

    Re: Textbox value won't assign to variable

    This code is assigning the values of the variables to the textboxes, not the other way round.
     BiCurrentStockPrice_TextBox.Value = S
        BiStrikePrice_TextBox.Value = X
        BiRisk_Free_Rate_TextBox.Value = r
        BiexpTime_TextBox.Value = T
        BiVolatility_TextBox.Value = sigma
        BiTimeSteps_TextBox.Value = n
    I changed the code to have the variable on the left and the textbox on the right, and declare all the variables, like this:
    
        Dim S As Double
        Dim X As Double
        Dim sigma As Double
        Dim r As Variant
        Dim T As Variant
        Dim n As Variant
        
        S = BiCurrentStockPrice_TextBox.Value
        X = BiStrikePrice_TextBox.Value
        r = BiRisk_Free_Rate_TextBox.Value
        T = BiexpTime_TextBox.Value
        sigma = BiVolatility_TextBox.Value
        n = BiTimeSteps_TextBox.Value
    I then opened the form, entered values in the textboxes, clicked the Calculation Option button and stepped through the code line by line with F8.

    When I reached the above section of code all the variables were assigned values from the various textboxes.

    Those variables were then used in the subsequent calculations.

  7. #7
    Registered User
    Join Date
    02-22-2013
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Textbox value won't assign to variable

    Thank you so much. I've been worrying about this for hours!!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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