Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB Script to check if textbox is blank

Posted on 2010-09-09
7
Medium Priority
?
568 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 668 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
Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 
LVL 13

Accepted Solution

by:
MWGainesJR earned 668 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 664 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

721 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