Combine String VBA issue

keiserj

Board Regular
Joined
May 31, 2013
Messages
89
Below is the code for a userform that im working on. But have str1 & str2 combining but if the user does not select str1 which is a list box it comes up a error. any way around this?

Code:
Private Sub CommandButton1_Click()
Dim RowCount As Long
Dim ctl As Control
Dim str1 As String
Dim str2 As String




If Me.textpartnum.Value = "" Then
MsgBox "Please enter a  PART #.", vbExclamation, "Missing Parts Form"
Me.textpartnum.SetFocus
Exit Sub
End If
If Me.Textmachinenum.Value = "" Then
MsgBox "Please enter a Machine #.", vbExclamation, "Missing Parts Form"
Me.Textmachinenum.SetFocus
Exit Sub
End If
If Me.ListBox1.Value = "" Then
MsgBox "Please select MFG or Purch.", vbExclamation, "Missing Parts Form"
Me.ListBox1.SetFocus
Exit Sub
End If


str1 = Me.ListBox1.Value
str2 = Format(Now, "mmddyyhhnnss")


RowCount = Worksheets("Missing Part Log").Range("A3").CurrentRegion.Rows.Count
With Worksheets("Missing Part Log").Range("A3")
.Offset(RowCount, 0).Value = str1 & str2
.Offset(RowCount, 1).Value = Me.textpartnum.Value
.Offset(RowCount, 2).Value = Me.textQTYNEED.Value
.Offset(RowCount, 3).Value = Me.ListBox1.Value
.Offset(RowCount, 4).Value = Me.ComboBox1.Value
.Offset(RowCount, 5).Value = Me.textmfgdate.Value
.Offset(RowCount, 6).Value = Me.Textrecdate.Value
.Offset(RowCount, 7).Value = Me.Textmachinenum.Value
.Offset(RowCount, 8).Value = Me.Textsonum.Value
.Offset(RowCount, 9).Value = Me.Textcopnum.Value
.Offset(RowCount, 10).Value = Me.Textbuilddate.Value


Call resetForm
End With


End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
This part here should prevent that problem
Code:
If Me.ListBox1.Value = "" Then
MsgBox "Please select MFG or Purch.", vbExclamation, "Missing Parts Form"
Me.ListBox1.SetFocus
Exit Sub
End If
Is your listbox single or multi select?
 
Upvote 0
Try replacing
Code:
Me.ListBox1.Value
with
Code:
ListBox1.list(ListBox1.ListIndex)
 
Upvote 0
I would assume this is what your replacing?

Code:
If ListBox1.List(ListBox1.ListIndex) = "" Then
MsgBox "Please select MFG or Purch.", vbExclamation, "Missing Parts Form"
Me.ListBox1.SetFocus
Exit Sub
End If
 
Upvote 0
You'll need to do it everywhere you have ListBox1.Value
 
Upvote 0
Change the if statement to
Code:
If Me.ListBox1.ListIndex = -1 Then
MsgBox "Please select MFG or Purch.", vbExclamation, "Missing Parts Form"
Me.ListBox1.SetFocus
Exit Sub
End If
but leave the others as they are
 
Upvote 0
That did the trick!! Thank you so much for the help!!

If Me.ListBox1.ListIndex = -1 Then
MsgBox "Please select MFG or Purch.", vbExclamation, "Missing Parts Form"
Me.ListBox1.SetFocus
Exit Sub
End If
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top