user3172566
user3172566

Reputation: 53

simple messagebox not getting displayed

i am trying to display a message box if the string is empty and this is the code i have tried.i am not getting any errors but still message box is not getting displayed.Any help is appreciated.

Dim tmp1 As String, tmp2 As String, tmp3 As String
Dim TextBox1 As String
Dim TextBox2 As String
Dim TextBox3 As String

tmp1 = Sheets("Sheet1").TextBox1.Value
If tmp1 = " " Then
MessageBox.Show ("file1 not selected")
End If

tmp2 = Sheets("Sheet1").TextBox2.Value
If tmp2 = " " Then 
MessageBox.Show ("file2 not selected")
End If

tmp3 = Sheets("Sheet1").TextBox3.Value
If tmp3 = " " Then 
MessageBox.Show ("file3 not selected")

Upvotes: 1

Views: 53

Answers (2)

RL8
RL8

Reputation: 16

Based on your question, I recommend you try the following steps:

  1. Name three cells in sheet 1 as TextBox1, 2, and 3 - use name manager in Excel (no VBA needed)

  2. Type the below code in VBE:

code:

If Range("TextBox1").value = "" Then
     Msgbox "pls fill field 1"
End if

I hope it makes sense and solves your problem.

Upvotes: 0

Siddharth Rout
Siddharth Rout

Reputation: 149295

Two things

A. If tmp2 = " " Then

If you are checking for blanks then use this

If Len(Trim(tmp2)) = 0 Then

B. MessageBox.Show is VB.Net. For VBA use

MsgBox "file1 not selected"

Similarly for others...

Upvotes: 2

Related Questions