Solved

VB Script to check if textbox is blank

Posted on 2010-09-09
7
551 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
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…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

708 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

12 Experts available now in Live!

Get 1:1 Help Now