User Form - Text Boxes and Labels

MrByte

Board Regular
Joined
Feb 9, 2007
Messages
167
Office Version
  1. 365
Just a few minutes ago I had my user form at least displaying vlaues in the labels. After I added the Dim xxx, xxx As Integer lines I do not get anything in the labels now.

Here's what I am trying to accomplish. In the 5 text boxes the user inputs a value, not all text boxes will be filled if they are not needed. Then if the check box is checked off (40% will be deducted from the value in the text box) and the result is placed in the corresponding label.

I'm sure there is an easier way to do this rather than the way I have it now. And any suggestions would be greatly appreciated.

Here is my code as it is now.
Code:
Private Sub chkCutHalf_Click()
    Dim Disc As Integer
    Dim txtCutA1, txtCutA2, txtCutA3, txtCutA4, txtCutA5 As Integer
    Dim lblCutA1, lblCutA2, lblCutA3, lblCutA4, lblCutA5 As Integer
    Disc = 0.6
    
    'Calculate each field and place result in 'B' field
    If txtCutA1 <> 0 Then
        lblCutA1 = txtCutA1 * Disc
        Else
        lblCutA1 = txtCutA1
    End If
    
    If txtCutA2 <> 0 Then
        lblCutA2 = txtCutA2 * Disc
        Else
        lblCutA2 = txtCutA2
    End If
        
    If txtCutA3 <> 0 Then
        lblCutA3 = txtCutA3 * Disc
        Else
        lblCutA3 = txtCutA3
    End If
                
    If txtCutA4 <> 0 Then
        lblCutA4 = txtCutA4 * Disc
        Else
        lblCutA4 = txtCutA4
    End If
                
    If txtCutA5 <> 0 Then
        lblCutA5 = txtCutA5 * Disc
        Else
        lblCutA5 = txtCutA5
    End If
    
End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
I don't know if a label can display an integer value.

Try this for every value that is going into the caption

str(txtCutA3 * Disc )
 
Upvote 0
Thanks but that didn't work. I "remed" out the Dim lines and I get my numbers back in the labels but not discounted.

Below is a portion of the revised code:

Code:
Private Sub chkCutHalf_Click()
    Dim Disc As Integer
    'Dim txtCutA1, txtCutA2, txtCutA3, txtCutA4, txtCutA5 As Integer
    'Dim lblCutA1, lblCutA2, lblCutA3, lblCutA4, lblCutA5 As Integer
    Disc = 0.6
    
    'Calculate each field and place result in 'B' field
    If txtCutA1 <> 0 Then
        lblCutA1 = Str(txtCutA1 * Disc)
        Else
        lblCutA1 = txtCutA1
    End If
 
Upvote 0
MrByte

What are those variables meant to represent?

What are the names of your labels/texboxes?

If they are txtCutA1, txtCutA2... and lblCutA1, lblCutA2... then you shouldn't even need the variables.

Also if Disc is 0.6 it ain't an integer.:)

And the only variable declared an integer here is the last one.
Code:
Dim txtCutA1, txtCutA2, txtCutA3, txtCutA4, txtCutA5 As Integer

Try something like this.
Code:
Private Sub chkCutHalf_Click()
Dim Disc As Single
Dim I As Long
   
    Disc = 0.6
   
    For I = 1 To 5
        If Val(Me.Controls("txtCutA" & I)) <> 0 Then
            Me.Controls("lblCutA" & I) = Me.Controls("txtCutA" & I) * Disc
        Else
            Me.Controls("lblCutA" & I) = Me.Controls("txtCutA" & I)
        End If
    Next I
   
End Sub
 
Upvote 0
Thanks Norie - I'm an old school Basic programmer (do you remember line numbers?) and have been out of the loop for a long time. I just now trying to pick up on VB.

As you can tell by my code I am still learning.

What are those variables meant to represent?

What are the names of your labels/texboxes?
The variables are from user input, it represents a dollar value, therefore the textboxes. The labels are the result (of course you can see that) of the dollar value minus 40%.

I'm going to try your code now. Thanks for your help and I'll let you know how it goes.
 
Upvote 0
Try something like this.
Code:
Private Sub chkCutHalf_Click()
Dim Disc As Single
Dim I As Long
   
    Disc = 0.6
   
    For I = 1 To 5
        If Val(Me.Controls("txtCutA" & I)) <> 0 Then
            Me.Controls("lblCutA" & I) = Me.Controls("txtCutA" & I) * Disc
        Else
            Me.Controls("lblCutA" & I) = Me.Controls("txtCutA" & I)
        End If
    Next I
   
End Sub

Norie forgive my ignorance but what is Me.Controls?
 
Upvote 0
Well Me refers to the userform I assume you are using.

And Controls is a collection of all the controls on the userform.
 
Upvote 0
Norie thank you for being such a BIG help with this. Now I have a couple of more problems with this code than expected. And since you have been shuch a help with this I thought I'd direct these next two problems to you as well, BUT anyone reading this can chime in and give me a hand too if you would like.

#1) The code here is giving me an error. Which is not "dumping" into the worksheet.
Code:
    Range("BCutTable") = txtBCTable.Text
    For I = 1 To 5
        Range("BCutA" & I) = Me.Controls("lblCutA" & I).Value
    Next I

#2 I have looked HIGH and low for the solution to this one that Norie helped out with originally. In the same code, mentioned in an earlier post, I want to have it rounded up to the nearest .10 (10 cents). I know in a worksheet I would use the fomula =ROUNDUP((R1C1*0.6)*10,0)/10
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,196
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top