Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 572
  • Last Modified:

VB Script to check if textbox is blank

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
DarrenJackson
Asked:
DarrenJackson
3 Solutions
 
RunriggerCommented:
Darren,

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

Thanks
Dave
0
 
DarrenJacksonAuthor Commented:
Sorry when the excel sheet is opened
0
 
RunriggerCommented:
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
Technology Partners: 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!

 
MWGainesJRCommented:
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
 
Chris BottomleyCommented:
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
 
DarrenJacksonAuthor Commented:
Guys

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

Thanks I will split points

Thankyou
0
 
DarrenJacksonAuthor Commented:
Great Thanks
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now