VBA userform only works once - vba

I am Creating a Userform in Excel 2013 to make entering data into an inventory file easier at my workplace. The userform
(Userform and Code Picture) will run and I can enter multiple new items (Ignore the Update existing entry button I haven't started on that yet) into the inventory file, but once I close the form opening it again causes the error
"Run time error '-2147417848 (80010108)' Method 'Value" of 'Range' failed"
to appear and the Debug seems to highlight a different object each time I try it.
Private Sub New_Data_Click()
Dim lRow As Long
Dim lPart As Long
Dim ws As Worksheet
Set ws = Worksheets("Worksheet")
lRow = ws.Cells.Find(What:"*", SearchOrder:=xlRows,
SearchDirection:=xlPrevious, LookIn=xlValues).Row + 1
With ws
.Cells(lRow, 1).Value = Me.EnterChemical.Value
.Cells(lRow, 2).Value = Me.EnterNotes.Value
.Cells(lRow, 3).Value = Me.EnterLot.Value
.Cells(lRow, 4).Value = Me.EnterItem.Value
.Cells(lRow, 5).Value = Me.SpinButton1.Value
.Cells(lRow, 6).Value = Me.EnterManf.Value
.Cells(lRow, 7).Value = Me.EnterLoc.Value
.Cells(lRow, 8).Value = Me.EnterTare.Value
.Cells(lRow, 9).Value = Me.EnterGross.Value
.Cells(lRow, 11).Value = Me.EnterUnit.Value
.Cells(lRow, 12).Value = Me.EnterExp.Value
.Cells(lRow, 16).Value = Date
If Me.Expire.Value = True Then
.Cells(lRow, 13).Value = "Expire"
ElseIf Me.Retest.Value = True Then
.Cells(lRow, 13).Value = "Retest"
EndIf
If Me.API.Value = True Then
.Cells(lRow, 1).Value = "Yes"
EndIf
EndWith
EndSub
Private Sub Closefrm.Click()
Unload Me
End Sub
Private Sub SpinButton1_Change()
Reference_no.Caption = SpinButton1.Value
So this all works one time but I dont know why it fails the second. Do I need to have a different closing function on the button or I am completely missing something.

Related

Unable to eliminate Runtime error 1004

I am trying to copy several data from a table on a different sheet, to a new table on another sheet. My structure is like:
These are the steps that me and Mr. #QHarr have tried:
Checked the objects and values exist
Tried running the codes line by line
Activate sheets and re-arranging the codes
None worked so far:
Here is my current codes:
Private Sub cmdedit_Click()
If MsgBox("Transfer selected asset to " & Me.ComboBox1.Text & "?", vbYesNo, "CONFIRMATION") = vbYes Then
Dim ws As Worksheet
Dim ws1 As Worksheet
Dim wsendRow As Range
Dim wsendRow1 As Range
Dim lo As ListObject
Dim lr As ListRow
Set ws = Sheets("FIELD OFFICE DATABASE")
Set ws1 = Sheets("Transferred Items")
Set lo = ws1.ListObjects("table3")
Set lr = lo.ListRows.Add
Set wsendRow = ws.Range("B" & Rows.Count).End(xlUp)
'Set wsendRow1 = ws1.Range("A" & Rows.Count).End(xlUp)
ws.Activate
Range("B2").Select
Do Until ActiveCell.Address = wsendRow.Address
If ActiveCell.Value = Me.cmbemn.Text Then
'ws1.Unprotect "321321"
'ws1.Activate
lr.Range(1, 1).Value = Me.cmbemn.Text 'error appears on this line. if I place a comment here, the error will just move on the next line.
lr.Range(1, 2).Value = Me.TextBox1.Text
lr.Range(1, 3).Value = Me.txttype.Text
lr.Range(1, 4).Value = Me.txtmodel.Text
lr.Range(1, 5).Value = ActiveCell.Offset(0, 4).Value
lr.Range(1, 6).Value = ActiveCell.Offset(0, 5).Value
lr.Range(1, 7).Value = Me.txtpurdate.Text
lr.Range(1, 8).Value = Me.txtprice.Text
lr.Range(1, 9).Value = Me.txtcon.Text
lr.Range(1, 10).Value = ActiveCell.Offset(0, 9).Value
lr.Range(1, 11).Value = ActiveCell.Offset(0, 11).Value
lr.Range(1, 12).Value = Me.ComboBox1.Text
lr.Range(1, 13).Value = ActiveCell.Offset(0, 13).Value
lr.Range(1, 14).Value = Date
lr.Range(1, 15).Value = ws.Range("A13").Value
lr.Range(1, 16).Value = Me.TextBox2.Text
Exit Do
Exit Sub
Else
ActiveCell.Offset(1, 0).Select
End If
Loop
End If
End Sub
Codes never worked. Also to note, this question is in reference to my other question, (which took a lot of comments from me and Mr. QHarr) until he suggested that I should ask another question instead.
I hope someone can help me figure this out.
Thank you so much in advance

Run-time error 1004 with vba adding hyperlink

Can someone please help me in my time of need. I have created a userform which is entering in a hyperlink dependent on dropdowns from a listbox.
Despite the hyperlink actually going in when the submit button is pressed, I am still receiving the error message of
Run-Time error 1004. Application-defined or object defined error.
When I debug ws.cells(iRow, 4) is the line highlighted
Private Sub Comm1_Click()
Dim iRow As Long
Dim ws As Worksheet
Dim ws2 As Worksheet
Dim rng As Range
Set ws = Worksheets("QttOutlay")
Set ws2 = Worksheets("LookupVals")
iRow = ws.Cells.Find(what:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
Set rng = ws.Cells(iRow)
ws.Cells(iRow, 2).Value = RmRef.Value
ws.Cells(iRow, 3).Value = RetMod.Value
ws.Cells(iRow, 4).Value = rng.Parent.Hyperlinks.Add(Anchor:=rng, Address:=WorksheetFunction.VLookup(RetMod.Value, ws2.Range("A:B"), 2, False), TextToDisplay:="Info")
ws.Cells(iRow, 5).Value = OrdCod.Value
ws.Cells(iRow, 6).Value = hmm.Value
ws.Cells(iRow, 7).Value = lmm.Value
ws.Cells(iRow, 8).Value = rdtype.Value
ws.Cells(iRow, 9).Value = dtt.Value
ws.Cells(iRow, 10).Value = Wtt.Value
ws.Cells(iRow, 11).Value = Qt.Value
ws.Cells(iRow, 12).Value = LPc.Value
ws.Cells(iRow, 13).Value = Dt.Value
ws.Cells(iRow, 14).Value = (LPc.Value * Dt.Value) * Qt.Value
End Sub
The Hyperlinks.Add Method returns a hyperlink object which you are trying to assign to a cells value: ws.Cells(iRow, 4).Value = rng.Parent.Hyperlinks.Add(…). That won't work.
I guess that ws.Cells(iRow, 4) is meant to be the anchor of the hyperlink like: Anchor:=ws.Cells(iRow, 4)
So instead of
ws.Cells(iRow, 4).Value = rng.Parent.Hyperlinks.Add(Anchor:=rng, Address:=WorksheetFunction.VLookup(RetMod.Value, ws2.Range("A:B"), 2, False), TextToDisplay:="Info")
you should replace the whole line with something like this
ws.Hyperlinks.Add Anchor:=ws.Cells(iRow, 4), Address:=WorksheetFunction.VLookup(RetMod.Value, ws2.Range("A:B"), 2, False), TextToDisplay:="Info"

If combobox equal to empty, do not copy userform data to excel sheet

I have create 2 sub function like this:
Sub Product1()
Dim lRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Inventory")
lRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
With ws
If IsEmpty(UserForm5.ComboBox5.Value) Then
Exit Sub
Else
.Cells(lRow, 1).Value = UserForm5.TextBox1.Value
.Cells(lRow, 2).Value = UserForm5.ComboBox2.Value
.Cells(lRow, 3).Value = UserForm5.ComboBox3.Value
.Cells(lRow, 4).Value = UserForm5.ComboBox4.Value
.Cells(lRow, 5).Value = UserForm5.ComboBox1.Value
.Cells(lRow, 6).Value = UserForm5.ComboBox5.Value
.Cells(lRow, 7).Value = UserForm5.TextBox2.Value
.Cells(lRow, 8).Value = UserForm5.TextBox5.Value
.Cells(lRow, 9).Value = UserForm5.TextBox6.Value
.Cells(lRow, 10).Value = UserForm5.TextBox4.Value
.Cells(lRow, 11).Value = UserForm5.TextBox7.Value
End If
End With
End Sub
Sub Product2()
Dim lRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Inventory")
lRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
With ws
If IsEmpty(UserForm5.ComboBox6.Value) Then
Exit Sub
Else
.Cells(lRow, 1).Value = UserForm5.TextBox1.Value
.Cells(lRow, 2).Value = UserForm5.ComboBox2.Value
.Cells(lRow, 3).Value = UserForm5.ComboBox3.Value
.Cells(lRow, 4).Value = UserForm5.ComboBox4.Value
.Cells(lRow, 5).Value = UserForm5.ComboBox1.Value
.Cells(lRow, 6).Value = UserForm5.ComboBox6.Value
.Cells(lRow, 7).Value = UserForm5.TextBox9.Value
.Cells(lRow, 8).Value = UserForm5.TextBox11.Value
.Cells(lRow, 9).Value = UserForm5.TextBox12.Value
.Cells(lRow, 10).Value = UserForm5.TextBox10.Value
.Cells(lRow, 11).Value = UserForm5.TextBox8.Value
End If
End With
End Sub
I was wondering that, if my combobox6 is empty, it should not transfer the data for to exel sheet.
What I faced now is if the combobox6 is empty( did not select any value), it will still copy all the data to the excel sheet.
Is there any way to fix it?
Change
If IsEmpty(UserForm5.ComboBox5.Value) Then
to
If UserForm5.ComboBox5.Value = "" Then
And make the same sort of change to the Product2 sub.
If the combo box is "empty" then checking its value will give you an empty string.
Sub Product1()
Dim lRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Inventory")
lRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
With ws
If UserForm5.ComboBox5.Value <> "" Then
Exit Sub
Else
.Cells(lRow, 1).Value = UserForm5.TextBox1.Value
.Cells(lRow, 2).Value = UserForm5.ComboBox2.Value
.Cells(lRow, 3).Value = UserForm5.ComboBox3.Value
.Cells(lRow, 4).Value = UserForm5.ComboBox4.Value
.Cells(lRow, 5).Value = UserForm5.ComboBox1.Value
.Cells(lRow, 6).Value = UserForm5.ComboBox5.Value
.Cells(lRow, 7).Value = UserForm5.TextBox2.Value
.Cells(lRow, 8).Value = UserForm5.TextBox5.Value
.Cells(lRow, 9).Value = UserForm5.TextBox6.Value
.Cells(lRow, 10).Value = UserForm5.TextBox4.Value
.Cells(lRow, 11).Value = UserForm5.TextBox7.Value
End If
End With
End Sub
Sub Product2()
Dim lRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Inventory")
lRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
With ws
If UserForm5.ComboBox6.Value <> "" Then
Exit Sub
Else
.Cells(lRow, 1).Value = UserForm5.TextBox1.Value
.Cells(lRow, 2).Value = UserForm5.ComboBox2.Value
.Cells(lRow, 3).Value = UserForm5.ComboBox3.Value
.Cells(lRow, 4).Value = UserForm5.ComboBox4.Value
.Cells(lRow, 5).Value = UserForm5.ComboBox1.Value
.Cells(lRow, 6).Value = UserForm5.ComboBox6.Value
.Cells(lRow, 7).Value = UserForm5.TextBox9.Value
.Cells(lRow, 8).Value = UserForm5.TextBox11.Value
.Cells(lRow, 9).Value = UserForm5.TextBox12.Value
.Cells(lRow, 10).Value = UserForm5.TextBox10.Value
.Cells(lRow, 11).Value = UserForm5.TextBox8.Value
End If
End With
End Sub

Method or data not found in simple VBA program

I have been trying to get a simple task done in VBA; I am writing a GUI for an excel spreadsheet that is holding inventory. It enables users to input info into the GUI click ok and all of what they typed in is saved into the excel document.
However every time I run the below code I get a compiler error "Method or data not found".
Private Sub Label1_Click()
End Sub
Private Sub Label4_Click()
End Sub
Private Sub cmdAdd_Click()
'Copy input values to sheet.
Dim lRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Inventory")
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
With ws
.Cells(lRow, 1).Value = Me.AssetType.Value
.Cells(lRow, 2).Value = Me.AssetNumber.Value
.Cells(lRow, 4).Value = Me.Description.Value
.Cells(lRow, 5).Value = Me.SerialNbr.Value
.Cells(lRow, 6).Value = Me.CurrentUse.Value
.Cells(lRow, 7).Value = Me.DateRec.Value
.Cells(lRow, 8).Value = Me.FundingSource.Value
.Cells(lRow, 9).Value = Me.Manufacturer.Value
.Cells(lRow, 10).Value = Me.Model.Value
.Cells(lRow, 11).Value = Me.Contract.Value
.Cells(lRow, 12).Value = Me.Status.Value
.Cells(lRow, 13).Value = Me.Room.Value
.Cells(lRow, 14).Value = Me.OfficeLocation.Value
.Cells(lRow, 19).Value = Me.Custodian.Value
.Cells(lRow, 20).Value = Me.ExcessedDate.Value
.Cells(lRow, 21).Value = Me.ExcessAuthorization.Value
.Cells(lRow, 22).Value = Me.Comments.Value
.Cells(lRow, 23).Value = Me.OutDate.Value
End With
'Clear input controls.
Me.AssetType.Value = ""
Me.AssetNumber.Value = ""
Me.Description.Value = ""
Me.SerialNbr.Value = ""
Me.CurrentUse.Value = ""
Me.DateRec.Value = ""
Me.FundingSource.Value = ""
Me.Manufacturer.Value = ""
Me.Model.Value = ""
Me.Contract.Value = ""
Me.Status.Value = ""
Me.Room.Value = ""
Me.OfficeLocation.Value ""
Me.Custodian.Value ""
Me.ExcessedDate.Value ""
Me.ExcessAuthorization.Value ""
Me.Comments.Value ""
Me.OutDate.Value ""
End Sub
Private Sub cmdClose_Click()
'Close UserForm.
Unload Me
End Sub
Private Sub Cmdbutton_add_Click()
End Sub
Private Sub Model_Click()
End Sub
Private Sub UserForm_Click()
End Sub
Get the value of a text box by the .text property of the text box.
If your text box is named AssetType it would be
.Cells(lRow, 1).Value = AssetType.Text
You may need to use the form name.
.Cells(lRow, 1).Value = UserForm1.AssetType.Text

How to fix Runtime Error "91" in Excel

I have created a userform to enter data into worksheets for me. I am trying to code it where it will put the information into a selected worksheet. I have a combobox where I can select the sheet I want it to put the info in. I am trying to set the variable ws to the combobox value and I get a runtime error '91' and it says "object variable or with block not set". The code I am having problems with is below.
Private Sub bttn_Submit_Click()
Dim iRow As Long
Dim ws As Worksheet
'set the variable for the worksheet
ws = ComboBox1.Value
'find first empty row in database
iRow = Columns("A").Find("*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'check for a date
If Trim(Me.txt_Date.Value) = "" Then
Me.txt_Date.SetFocus
MsgBox "Please enter a Date"
Exit Sub
End If
'copy the data to the database
'use protect and unprotect lines,
' with your password
' if worksheet is protected
With ws
' .Unprotect Password:="password"
.Cells(iRow, 1).Value = Me.txt_Date.Value
.Cells(iRow, 2).Value = Me.txt_Invoice.Value
.Cells(iRow, 3).Value = Me.txt_Mileage.Value
.Cells(iRow, 5).Value = Me.CheckBox_Oil_Filter
.Cells(iRow, 7).Value = Me.CheckBox_Air_Filter
.Cells(iRow, 9).Value = Me.CheckBox_Primary_Fuel_Filter
.Cells(iRow, 11).Value = Me.CheckBox_Secondary_Fuel_Filter
.Cells(iRow, 13).Value = Me.CheckBox_Transmission_Filter
.Cells(iRow, 15).Value = Me.CheckBox_Transmission_Service
.Cells(iRow, 17).Value = Me.CheckBox_Wiper_Blades
.Cells(iRow, 19).Value = Me.CheckBox_Rotation_Rotated
.Cells(iRow, 21).Value = Me.CheckBox_New_Tires
.Cells(iRow, 23).Value = Me.CheckBox_Differential_Service
.Cells(iRow, 25).Value = Me.CheckBox_Battery_Replacement
.Cells(iRow, 27).Value = Me.CheckBox_Belts
.Cells(iRow, 29).Value = Me.CheckBox_Lubricate_Chassis
.Cells(iRow, 30).Value = Me.CheckBox_Brake_Fluid
.Cells(iRow, 31).Value = Me.CheckBox_Coolant_Check
.Cells(iRow, 32).Value = Me.CheckBox_Power_Steering_Check
.Cells(iRow, 33).Value = Me.CheckBox_A_Transmission_Check
.Cells(iRow, 34).Value = Me.CheckBox_Washer_Fluid_Check
' .Protect Password:="password"
End With
'clear the data
Me.txt_Date.Value = ""
Me.txt_Invoice.Value = ""
Me.txt_Mileage.Value = ""
Me.txt_Date.SetFocus
End Sub
I take your piece of code and explain you the inconsistency:
Dim ws As Worksheet
ws = ComboBox1.Value
The first line of code is declaring ws as a Worksheet object. Objects in VBA need the keyword Set to be set, and of course the data type needs to match the declaration.
On the other hand, in the second statement you're setting ws as ComboBox1.Value, which is a type String. So:
1) The reason why you get the error is that ws is a variable that, because of its declaration, expects to get a Worksheet object but you're instead trying to cast a String in it;
2) What you probably want to do is to have in ws the Worksheet object whose name is the ComboBox1.Value; this, in terms of code, should be written as follows:
Set ws = ThisWorkbook.Sheets(ComboBox1.Value)
You only need to replace the line ws = ComboBox1.Value with the line above.
Pay attention, in general: if you declare a variable of type X, it will always be able to cast only a type X object, unless you don't declare it at all (very bad practice, you would get errors after).