VB Script to check if textbox is blank

Posted on 2010-09-09
Last Modified: 2012-06-27

I need an example of a vb script for excel 2003 that checks a textbox to see if it is blank if it is it then prompts you to enter a value in the text box but if there is a value there then do nothing

Any ideas

Question by:DarrenJackson
LVL 11

Expert Comment

ID: 33635842

Can you be more specific about the process, what event will "trigger" the check?


Author Comment

ID: 33635853
Sorry when the excel sheet is opened
LVL 11

Assisted Solution

Runrigger earned 167 total points
ID: 33635883
Open the VB Project explorer, double click the "ThisWorkbook" object to open it's VB editer, then paste the following;

Private Sub Workbook_Open()

    If Range("A1").Value = "" Then
        Range("A1").Value = InputBox("This workbook is missing a mandatory value in Cell A1, enter the value here!", "WARNING")
    End If

End Sub
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 13

Accepted Solution

MWGainesJR earned 167 total points
ID: 33636274
sub worksheet_activate()
dim str as string
if txtmytextbox.text = "" then
      str = inputbox("Enter a value.")
      txtmytextbox.text = str
end if
msgbox "Your value is: " & txtmytextbox.text
end sub
LVL 59

Assisted Solution

by:Chris Bottomley
Chris Bottomley earned 166 total points
ID: 33644340
If you want a single shot per activation then you cannot go wrong with the post from MWGainesJR.

The following will continue to pester until they input something, (OTHER THAN SPACES or blank).

Referenced to TextBox1 needs to reflect your textbox of course.


To Create a macro:

Alt + F11 to open the macro editor

For worksheet event handlers:
     In the project tree select as appropriate:
        EXCEL      : the worksheet code page
     In the workpane select as appropriate:
        EXCEL      : WorkSheet
     In the workpane select the required 'event', (i.e. 'Change')

Check Security as appropriate:

In the application select Tools | Macro | Security
2003 and Earlier : Select Medium
2007 : Outlook - Warnings for all Macros
     : Others - Enable a trusted location and inhibit macros otherwise so do both!
      : Disable Macros
            Office Button, (top left of the screen)
            Trust Centre
            Trust Centre Settings
            MAcro Settings
            Disable All MAcros with warnings
      : Enable Trusted Locations
            Office Button, (top left of the screen)
            Trust Centre
            Trust Centre Settings
            Trusted Locations
            Add a preferred location
            Re-open to ensure it is recognised if the above has been changed
Select OK

Private Sub Worksheet_Activate()
    Do While Trim(TextBox1.Text) = ""
        TextBox1.Text = InputBox("Enter String for the box", "TextBox1 is blank - needs initialisng")
End Sub

Open in new window


Author Comment

ID: 33644355

Thanks for the code I have just tried Chris's and they all work as desired

Thanks I will split points


Author Closing Comment

ID: 33644362
Great Thanks

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question