Solved

VB Script to check if textbox is blank

Posted on 2010-09-09
7
552 Views
Last Modified: 2012-06-27
Guys

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

Regards
0
Comment
Question by:DarrenJackson
7 Comments
 
LVL 11

Expert Comment

by:Runrigger
ID: 33635842
Darren,

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

Thanks
Dave
0
 

Author Comment

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

Assisted Solution

by:Runrigger
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 13

Accepted Solution

by:
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
0
 
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.

Chris

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)
            Options
            Trust Centre
            Trust Centre Settings
            MAcro Settings
            Disable All MAcros with warnings
      : Enable Trusted Locations
            Office Button, (top left of the screen)
            Options
            Trust Centre
            Trust Centre Settings
            Trusted Locations
            Add a preferred location
            ENSURE YOUR FILE IS IN A TRUSTED 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")

    Loop

End Sub

Open in new window

0
 

Author Comment

by:DarrenJackson
ID: 33644355
Guys

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

Thanks I will split points

Thankyou
0
 

Author Closing Comment

by:DarrenJackson
ID: 33644362
Great Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

863 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now