DericVonBraatson
New Member
- Joined
- Jan 23, 2018
- Messages
- 5
I'm opening my UserForm1 by selecting a specific cell (any cell within a range of A2:A100). I say "specific cell" because UserForm1 is pulling info back into my TextBox1 and TextBox2.
However, if any cell within my range of A2:A100 is empty, I want UserForm1 to open with a grey instruction text.
My code works, but ONLY from inside of Visual Basics by hitting the run/play button. My VBA code appears to cancel itself out if I'm selecting from the Excel spreadsheet, range A2:A100 in order to open my UserForm1.
I believe my problem is that I don't have some kind of "IF EMPTY" Statement in my code that goes into SHEET1. I'm not an experienced enough user to know how to create that.
I would appreciate any code help on this madness. Thank you!!!
SHEET1
UserForm1
Here's my Excel file example:
https://1drv.ms/x/s!AvBxwrOYtc3OgUC9qgGx-FV6Zx_F
Photo of my Excel example:
https://1drv.ms/i/s!AvBxwrOYtc3OgUGjosZFLY2KAfPS
However, if any cell within my range of A2:A100 is empty, I want UserForm1 to open with a grey instruction text.
My code works, but ONLY from inside of Visual Basics by hitting the run/play button. My VBA code appears to cancel itself out if I'm selecting from the Excel spreadsheet, range A2:A100 in order to open my UserForm1.
I believe my problem is that I don't have some kind of "IF EMPTY" Statement in my code that goes into SHEET1. I'm not an experienced enough user to know how to create that.
I would appreciate any code help on this madness. Thank you!!!
SHEET1
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' This retrieves info back into UserForm if values are present within a specific cell
If Target.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("A2:A100")) Is Nothing Then
UserForm1.TextBox1 = Sheets("Sheet1").Cells(Target.Row, "A")
UserForm1.TextBox2 = Sheets("Sheet1").Cells(Target.Row, "B")
UserForm1.Show
End If
End Sub
UserForm1
Code:
Private Sub UserForm_Initialize()
' This sets up the grey instruction text for two TextBoxes
TextBox1.ForeColor = &HC0C0C0
TextBox1.Text = "Input 1st Text Here"
TextBox2.ForeColor = &HC0C0C0
TextBox2.Text = "Input 2nd Text Here"
Me.CommandButton1.SetFocus
End Sub
Private Sub TextBox1_Enter()
' If new text is entered, the Textbox values turns black and will send to Excel Spreadsheet
With TextBox1
If .Text = "Input 1st Text Here" Then
.ForeColor = &H80000008 '<~~ Black Color
.Text = ""
End If
End With
End Sub
Private Sub TextBox1_AfterUpdate()
' If new text is not inputted (or even erased), the Textbox returns to grey instructions and will NOT send to Excel Spreadsheet
With TextBox1
If .Text = "" Then
.ForeColor = &HC0C0C0
.Text = "Input 1st Text Here"
End If
End With
End Sub
Private Sub TextBox2_Enter()
' If new text is entered, the Textbox values turns black and will send to Excel Spreadsheet
With TextBox2
If .Text = "Input 2nd Text Here" Then
.ForeColor = &H80000008
.Text = ""
End If
End With
End Sub
Private Sub TextBox2_AfterUpdate()
' If new text is not inputted (or even erased), the Textbox returns to grey instructions and will NOT send to Excel Spreadsheet
With TextBox2
If .Text = "" Then
.ForeColor = &HC0C0C0
.Text = "Input 2nd Text Here"
End If
End With
End Sub
Private Sub CommandButton1_Click()
'CommandButton1 Sends Info to Excel Spreadsheet
ActiveCell.Offset(0, 0).Value = Me.TextBox1.Value
ActiveCell.Offset(0, 1).Value = Me.TextBox2.Value
Unload Me
End Sub
Here's my Excel file example:
https://1drv.ms/x/s!AvBxwrOYtc3OgUC9qgGx-FV6Zx_F
Photo of my Excel example:
https://1drv.ms/i/s!AvBxwrOYtc3OgUGjosZFLY2KAfPS