+ Reply to Thread
Results 1 to 6 of 6

UserForm VBA to ignore blank textboxes

  1. #1
    Forum Contributor
    Join Date
    04-04-2014
    Location
    Tetbury, England
    MS-Off Ver
    Excel 2010
    Posts
    254

    UserForm VBA to ignore blank textboxes

    Hi!

    I have a spreadsheet of which you can enter sales figures via a UserForm.

    In this form you can enter the sales figures each month for 5 years. It currently correctly 'pastes' whatever the user has typed into each month into the cells on the Master worksheet.

    However... what if the user wants to leave one month blank or just work on years 1 and 2 and ignore 3, 4 and 5?

    This also works fine, except the user may not want to input sales into years 3, 4 and 5 because the Master worksheet already has data in these cells and so what the UserForm VBA is doing when a TextBox is left blank is; overwriting whatever is in the cell on the Master worksheet with nothing... effectively removing the sales figure that was there in the first place.

    How do I make the UserForm ignore and NOT overwrite blank TextBoxes into the cells where they should be 'pasted'.

    Here is my code for when the user clicks 'Submit' (I'm currently trying to get year 1 working, then the remaining years will be easy):

    Private Sub cmdSY1_5S1_Click()

    Dim ws As Worksheet
    Set ws = Worksheets("MASTER")

    'Post user's input onto MASTER worksheet Row 17 Column T - AE:

    'Year 1:

    ws.Cells(17, 20).Value = txtY1M1.Value
    ws.Cells(17, 21).Value = txtY1M2.Value
    ws.Cells(17, 22).Value = txtY1M3.Value
    ws.Cells(17, 23).Value = txtY1M4.Value
    ws.Cells(17, 24).Value = txtY1M5.Value
    ws.Cells(17, 25).Value = txtY1M6.Value
    ws.Cells(17, 26).Value = txtY1M7.Value
    ws.Cells(17, 27).Value = txtY1M8.Value
    ws.Cells(17, 28).Value = txtY1M9.Value
    ws.Cells(17, 29).Value = txtY1M10.Value
    ws.Cells(17, 30).Value = txtY1M11.Value
    ws.Cells(17, 31).Value = txtY1M12.Value

    'Closes the form after user has entered data:

    frmY1_5VATSalesType1.Hide

    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: UserForm VBA to ignore blank textboxes

    Please Login or Register  to view this content.
    Although the reference Cells(17,20) is referencing a single cell .... you probably need to change that
    Elegant Simplicity............. Not Always

  3. #3
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: UserForm VBA to ignore blank textboxes

    Try this:

    Please Login or Register  to view this content.
    Last edited by stnkynts; 04-04-2014 at 10:17 AM.

  4. #4
    Forum Contributor
    Join Date
    04-04-2014
    Location
    Tetbury, England
    MS-Off Ver
    Excel 2010
    Posts
    254

    Re: UserForm VBA to ignore blank textboxes

    Thank you stnkynt! Works absolutely flawlessly!! So simple!

    AndyLitch - yours seemed to overwrite the sales that were already on the worksheet?

    I'm not sure if using the iYear would work as on the Master worksheet the 5 years are spread across columns (T to CA) - how would I make the Year 2 input start on column AF (being Year 2 Month 1) and so on for the other years? Year 2 Month 1 = AF = column 32.

    Thanks in advance!

  5. #5
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: UserForm VBA to ignore blank textboxes

    Well you could do it with a counter if the correlation between the columns is the same (i.e., you are always increasing by 15 columns) but it is kind of hard to explain how to do that. It might be best for you to just have 5 different loops. If you submit your workbook with detailed instructions there might be some trick ways to do what you need to do.

  6. #6
    Forum Contributor
    Join Date
    04-04-2014
    Location
    Tetbury, England
    MS-Off Ver
    Excel 2010
    Posts
    254

    Re: UserForm VBA to ignore blank textboxes

    It's a rather complex workbook in general, I think I'll just add code for each year - thank you very much for 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. 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
  2. Userform ComboBox ignore Blank Cells in list
    By CRIMEDOG in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-24-2011, 09:33 AM
  3. Replies: 1
    Last Post: 11-29-2010, 06:56 AM
  4. Userform TextBoxes
    By tqm1 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-29-2007, 09:04 AM
  5. [SOLVED] UserForm TextBoxes
    By Rob in forum Excel General
    Replies: 2
    Last Post: 08-05-2005, 10:07 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