How to fix Runtime Error "91" in Excel - vba

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).

Related

VBA userform only works once

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.

How do i get to next form in visual basic

I am trying to get to the next form for my excel macro
right now I have a login form that has user name and password
that works when I press login it goes to next form.
but when I go to my next form and type the information in it it closes and doesn't pull up the next forum.
Can someone please explain to me what I am doing wrong? why wont my next form pop up?
Private Sub cmdAdd_Click()
'Dim iRow As Long
'Dim ws As Worksheet
Set ws = Worksheets("D544 Back Panel")
'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'check for a part number
If Trim(Me.txtPrd.Value) = "" Then
Me.txtPrd.SetFocus
MsgBox "Please enter a Production Number"
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.txtDate.Value
.Cells(iRow, 2).Value = Me.txtHrs.Value
.Cells(iRow, 3).Value = Me.txtPrd.Value
.Cells(iRow, 4).Value = Me.txtSrp.Value
.Cells(iRow, 5).Value = Me.txtOper.Value
' .Protect Password:="Password"
End With
'clear the data
Me.txtDate.Value = ""
Me.txtHrs.Value = ""
Me.txtPrd.Value = ""
Me.txtSrp.Value = ""
Me.txtOper.Value = ""
Me.txtPrd.SetFocus
Unload Me
End Sub
Private Sub cmdSubmit_Click()
'Dim iRow As Long
'Dim ws As Worksheet
Set ws = Worksheets("Scrap")
'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'check for a part number
If Trim(Me.txtPress.Value) = "" Then
Me.txtPress.SetFocus
MsgBox "Please enter press scrap"
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.txtDelam.Value
.Cells(iRow, 2).Value = Me.txtCuts.Value
.Cells(iRow, 3).Value = Me.txtBurns.Value
.Cells(iRow, 4).Value = Me.txtDents.Value
.Cells(iRow, 5).Value = Me.txtStaple.Value
.Cells(iRow, 6).Value = Me.txtGlue.Value
.Cells(iRow, 7).Value = Me.txtPress.Value
' .Protect Password:="Password"
End With
'clear the data
Me.txtDelam.Value = ""
Me.txtCuts.Value = ""
Me.txtBurns.Value = ""
Me.txtDents.Value = ""
Me.txtStaple.Value = ""
Me.txtGlue.Value = ""
Me.txtPress.Value = ""
Me.txtPress.SetFocus
Unload Me
End Sub
I used
Formname1.Hide
Formname2.Show
This seemed to solve my issue. Thanks for the help guys. Sorry I am very new to this site.

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"

Adding new autofill cell and returning cell value to userform VBA Excel

I have been searching for a solution for hours, Can anyone help me?
I have a cmd button to open a user form and create a new entry in a 'master enquiry log' worksheet. I would like the cmd button to autofill the next number to the master log in column 'A' and then return that number to the user form.
I think there may need to be a user form initialization sub added but im not sure what to enter.
Any help will be much appreciated
Private Sub cmdAdd_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Master Enq. Log")
'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'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, 2).Value = Me.txtCustName.Value
.Cells(iRow, 3).Value = Me.txtCustAddr.Value
.Cells(iRow, 4).Value = Date
.Cells(iRow, 5).Value = Me.cboProjEng.Value
.Cells(iRow, 6).Value = Me.cboDrawer.Value
.Cells(iRow, 11).Value = Me.cboSalesPers.Value
' .Protect Password:="password"
End With
'clear the data
Me.txtCustName.Value = ""
Me.txtCustAddr.Value = ""
Me.cboProjEng.Value = "Select"
Me.cboDrawer.Value = "Select"
Me.cboSalesPers.Value = "Select"
Me.txtCustName.SetFocus
End Sub

Copy cell values to different Workbook

Firstly, let me apologise if this question has already been answered somewhere else. I had a good look but couldn't find anything that would help me.
Secondly, I'm sure there is a far more simple way to do this but I'm very new the VBA and I'm just trying to teach myself as I go along.
Ok, so I have a sheet at the end of my workbook that compiles information from the previous sheet and I want to copy those values that are all in row 2 to another workbook that we have a network drive.
I've managed to get this to work on the same sheet but not to another workbook (without using a userform).
It comes back with the error 'Invalid Qualifier' for the line
Cells(emptyRow, 1.Value - DateRaised.Value
Here is my code below,
Sub CommandButton1_Click()
Dim emptyRow As Long
Dim DateRaised As Long
Dim CustomerName As String
Dim SiteAddress As String
Dim CallReason As String
Dim CustomerOrderNo As Long
Dim InvoiceNo As Long
Dim CovernoteNo As Long
Dim Findings As String
Dim ProductType As String
Dim Supplier As String
Dim Attempts As Long
Dim Condition As String
Dim DateClosed As Long
Dim CreditGiven As String
Dim CreditValue As Long
Dim IssueDays As Long
Dim Comments As String
DateRaised = Cells(2, "A").Value
CustomerName = Cells(2, "B").Value
SiteAddress = Cells(2, "C").Value
CallReason = Cells(2, "D").Value
CustomerOrderNo = Cells(2, "F").Value
InvoiceNo = Cells(2, "G").Value
CovernoteNo = Cells(2, "H").Value
Findings = Cells(2, "I").Value
ProductType = Cells(2, "J").Value
Supplier = Cells(2, "K").Value
Attempts = Cells(2, "L").Value
Condition = Cells(2, "M").Value
DateClosed = Cells(2, "N").Value
CreditGiven = Cells(2, "O").Value
CreditValue = Cells(2, "P").Value
IssueDays = Cells(2, "Q").Value
Comments = Cells(2, "R").Value
Dim WrkBk As Workbook
Dim WrkSht As Worksheet
Set WrkBk = Workbooks.Open("R:\6024 Onsite\COVER NOTE WORKFLOW\Database\Covernote Databse.xlsx")
Set WrkSht = WrkBk.Sheets("Covernote Database")
WrkSht.Activate
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
Cells(emptyRow, 1).Value = DateRaised.Value
Cells(emptyRow, 2).Value = CustomerName.Value
Cells(emptyRow, 3).Value = SiteAddress.Value
Cells(emptyRow, 4).Value = CallReason.Value
Cells(emptyRow, 5).Value = CustomerOrderNo.Value
Cells(emptyRow, 6).Value = InvoiceNo.Value
Cells(emptyRow, 7).Value = CovernoteNo.Value
Cells(emptyRow, 8).Value = Findings.Value
Cells(emptyRow, 9).Value = ProductType.Value
Cells(emptyRow, 10).Value = Supplier.Value
Cells(emptyRow, 11).Value = Attemps.Value
Cells(emptyRow, 12).Value = Condition.Value
Cells(emptyRow, 13).Value = DateClosed.Value
Cells(emptyRow, 14).Value = CreditGiven.Value
Cells(emptyRow, 15).Value = CreditValue.Value
Cells(emptyRow, 16).Value = IssueDays.Value
Cells(emptyRow, 17).Value = Comments.Value
WrkBk.Close (SaveChanges = False)
End Sub
If anyone can point me in the right direction I'd be a very happy man.
it's because you're attempting to treat value types (like String and Long) variables as if they were reference type (objects) ones calling their Value property:
Cells(emptyRow, 1).Value = DateRaised.Value
while you can't (unless you use User Defined Types): value type variables can be only accessed as they are:
Cells(emptyRow, 1).Value = DateRaised
but you can simply code like follows:
Option Explicit
Sub CommandButton1_Click()
Dim emptyRow As Long
Dim curSht As Worksheet
Set curSht = ActiveSheet
With Workbooks.Open("R:\6024 Onsite\COVER NOTE WORKFLOW\Database\Covernote Databse.xlsx").Sheets("Covernote Database")
emptyRow = WorksheetFunction.CountA(.Range("A:A")) + 1
.Cells(emptyRow, 1).Resize(, 17).value = curSht.Cells(2, 1).Resize(, 17).value '<-- paste values from originally opened sheet range A2:Q2
End With
ActiveWorkbook.Close SaveChanges:=False
End Sub