+ Reply to Thread
Results 1 to 7 of 7

Userform problem with Textboxes and IF Statement

Hybrid View

  1. #1
    Registered User
    Join Date
    08-23-2017
    Location
    South Afrca
    MS-Off Ver
    2016
    Posts
    3

    Userform problem with Textboxes and IF Statement

    Good day Guys

    This is my first post as a member (and a guy that just discovered forms) so my humble apologies if I am doing it wrong.

    So here goes:

    Ive got a Userform (Tax_Year_Details) that asks the User to select TAX YEAR DATE from a combobox(works perfect) and then to type opening odometer from a textbox1 and closing odometer from textbox2 and then finally total Business Kilometer in textbox4. All of these three textboxes are defined as

    Private Sub TextBox1_Change() 
    Range("b8").Value = TextBox1
    End sub 
    
    Private Sub TextBox2_Change()
    Range("b9").Value = TextBox2
    End Sub
    
    Private Sub TextBox4_Change()
    Range("b14").Value = TextBox4
    End Sub
    Once proceeding to click on the OK button it need to do a few checks and if there are errors a msgbox should give a describtion.

    .... example Textbox2 can not be less in value than textbox1. E.G Opening reading is 5000km (textbox1) and Closing reading 4000km(textbox2) = msgbox " Closing is less than opening - Re-enter"
    and
    ....example textbox3 can not be more than the difference of textbox2-textbox1. E.G. Traveled 2000km this year but opening reading was on 1000km and closing reading was on 1400km, = msgbox " Kilometers traveled is greater than kilometers from Opening and Closing specified"

    Private Sub Tax_year_OK_button_Click()
    ' First test that closing isnt less than opening
    If Range("B9") < Range("B8") Then
    MsgBox "CLOSING KILOMETERS IS LESS THAN OPENING KILOMTERS - Change Closing Kilometers"
    
    TAX_Year_date.Show
     
    'Second test if traveled in year for business is greater than Cell B9 - and cell B8
    ElseIf Range("B14") < Range("B13") Then
    MsgBox "TOTAL KILOMETERS TRAVELED IS LESS THAN BUSINESS KILOMTERS - Change Closing Kilometers"
    
    TAX_Year_date.Show
    
    'values are correctly entered proceed to next step
    Range("b9").Value = TextBox2
    End If
    
    Unload Me
    
    End Sub

    Some times the values work sometimes it doesnt - Not sure why not. See attached pictures







    Working Forum.png

    Not Working Forum MSGBox.png

    Not Working Forum MSGBox 2.png
    Attached Files Attached Files
    Last edited by Mariolockem; 08-24-2017 at 03:36 PM.

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,913

    Re: Userform problem with Textboxes and IF Statement

    Partly deleted while OP complied to Forum Rules.

    Why not perform all tests (when OK-button pressed) before writing to Worksheet ?

    Post example file with userform so we can test and adapt code.
    Last edited by bakerman2; 08-24-2017 at 04:43 PM.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Registered User
    Join Date
    08-23-2017
    Location
    South Afrca
    MS-Off Ver
    2016
    Posts
    3

    Re: Userform problem with Textboxes and IF Statement

    Quote Originally Posted by bakerman2 View Post

    Post example file with userform so we can test and adapt code.
    File attached - as I said I am still need to coding and any basic advice would be greatly appreciated.

  4. #4
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Userform problem with Textboxes and IF Statement

    I find that it is a lot easier to use one Macro to control all my textboxes.

    
    Public Changeflag As Boolean
    
    Private Sub TextBox1_Change()
    Checksum
    End Sub
    
    Private Sub TextBox2_Change()
    Checksum
    End Sub
    
    Private Sub TextBox4_Enter()
    TextBox1.SetFocus
    End Sub
    
    Private Sub Checksum()
    If Changeflag = True Or TextBox1 = "" Or TextBox2 = "" Then Exit Sub
    Changeflag = True
    If Not IsNumeric(TextBox1.Value) And TextBox1.Value <> "" Then TextBox1.Value = ""
    If Not IsNumeric(TextBox2.Value) And TextBox2.Value <> "" Then TextBox2.Value = ""
    If TextBox2 <> "" And TextBox1 > TextBox2 Then
    MsgBox "CLOSING KILOMETERS IS LESS THAN OPENING KILOMTERS - Change Closing Kilometers"
    Else
    TextBox4.Value = TextBox2.Value - TextBox1.Value
    End If
    Changeflag = False
    End Sub
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  5. #5
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,913

    Re: Userform problem with Textboxes and IF Statement

    This should do it.
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    08-23-2017
    Location
    South Afrca
    MS-Off Ver
    2016
    Posts
    3

    Re: Userform problem with Textboxes and IF Statement

    WHOOP WHOOOP

    Love it bakerman2

    Thanks a mil

  7. #7
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,913

    Re: Userform problem with Textboxes and IF Statement

    You're welcome.

+ 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] Populating userform textboxes basis of results from 2 userform comboboxes
    By rlh in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-11-2017, 10:26 AM
  2. [SOLVED] Problem running Excel loop with Userform textboxes and checkboxes
    By dentler in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-20-2017, 01:59 PM
  3. Help Populating Textboxes in Userform, With Multipage Userform.
    By mdmorgan24 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-07-2012, 05:29 PM
  4. Write If/And statement for userform textboxes that are only visible
    By clemsoncooz in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-15-2012, 11:10 AM
  5. Userform TextBoxes
    By tqm1 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-29-2007, 09:04 AM
  6. [SOLVED] UserForm TextBoxes
    By Rob in forum Excel General
    Replies: 2
    Last Post: 08-05-2005, 10:07 PM
  7. [SOLVED] Userform Textboxes and Looping
    By Robbyn in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-17-2005, 12:06 AM

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