Vlookup with VBA - vba

I am trying to do an automated vlookupfunction but I am getting a run time error:
1004 "Unable to get the Vlookup property of the worksheet functionclass"
Would need some help on where it could have gone wrong and how it could be adjusted!
This is the code:
Sub FINDSAL()
'On Error GoTo MyErrorHandler:
Dim Seat_No As String
Seat_No = InputBox("Enter the Seat Number:")
If Len(Seat_No) > 0 Then
nameit = Application.WorksheetFunction.Vlookup(Seat_No, Sheets("L12 - Data Sheet").Range("B4:E250"), 2, False)
MsgBox "The name is : $ " & nameit
Else
MsgBox ("You entered an invalid value")
End If
Exit Sub
MyErrorHandler:
If Err.Number = 1004 Then
MsgBox "Employee Not Present in the table."
End If
End Sub

Sub FINDSAL()
On Error GoTo MyErrorHandler:
Dim Seat_No As Integer
Seat_No = InputBox("Enter the Seat Number:")
If Len(Seat_No) > 0 Then
nameit = Application.WorksheetFunction.Vlookup(Seat_No, Sheets("L12 - Data Sheet").Range("B4:E250"), 2, False)
MsgBox "The name is : $ " & nameit
Else
MsgBox ("You entered an invalid value")
End If
Exit Sub
MyErrorHandler:
If Err.Number = 1004 Then
MsgBox "Employee Not Present in the table."
End If
End Sub

Related

Message box for null fields

I am trying to create a database for part locations. Three of the fields (JobNumber, PartNumber and Location) are required fields and I have an If statement written to check for Nulls and error handling to give a message box. After the message box is closed, it will let me return to the form but it will not allow me to edit the field again AND it still adds the incomplete data to the table. Any advice?
Private Sub cmdAddNew_Click()
If IsNull(Me.JobNumber) Then
GoTo cmdAddNew_Click_Err
ElseIf IsNull(Me.PartNumber) Then
GoTo cmdAddNew_Click_Err
ElseIf IsNull(Me.Location) Then
GoTo cmdAddNew_Click_Err
End If
On Error GoTo cmdAddNew_Click_Err
On Error Resume Next
DoCmd.GoToRecord , "", acNewRec
If (MacroError <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
End If
cmdAddNew_Click_Exit:
Exit Sub
cmdAddNew_Click_Err:
MsgBox "Job Number, Part Number and Location are required."
End Sub
Adding Cancel = True in your second if statement should fix this.
Private Sub cmdAddNew_Click()
If IsNull(Me.JobNumber) Then
GoTo cmdAddNew_Click_Err
ElseIf IsNull(Me.PartNumber) Then
GoTo cmdAddNew_Click_Err
ElseIf IsNull(Me.Location) Then
GoTo cmdAddNew_Click_Err
End If
On Error GoTo cmdAddNew_Click_Err
On Error Resume Next
DoCmd.GoToRecord , "", acNewRec
If (MacroError <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
Cancel = True
End If
cmdAddNew_Click_Exit:
Exit Sub
cmdAddNew_Click_Err:
MsgBox "Job Number, Part Number and Location are required."
End Sub
Alternatively, you could define functions to check for a null value and return a boolean result:
Function isvalid(Field as string) As Boolean
If IsNull(field)
isvalid = False
Else
isvalid = True
End If
End Function
Sub Check_valid()
Call isvalid(field 1)
Call isvalid(f2)
Call isvalid(f3)
If isvalid(field 1) = false Or isvalid(f2) = false Or isvalid(f3) = false Then
msgbox "Job Number, Part Number and Location are required."
Exit Sub
End If
End Sub
Hope this helps!

find page and assign a value to a cell

I have a excel file and there are 20 sheets
I need to search and find several sheet on my excel that want and add email to M1 Cell
Could you please help me.
Application.ScreenUpdating = False
On Error Resume Next
Application.DisplayAlerts = False
döngü:
For i = 1 To Worksheets.Count
If Worksheets(i).Name = "ABC" Then
Sheets("ABC").Select
Range("M1").Select
ActiveCell.FormulaR1C1 = "abc#hotmail.com"
If Worksheets(i).Name = "ABC2" Then
Sheets("ABC2").Select
Range("M1").Select
ActiveCell.FormulaR1C1 = "abc2#hotmail.com"
GoTo döngü:
pass:
Next i
Application.ScreenUpdating = True
MsgBox "mail assign done"
End Sub
You can simply use below sub.
Sub WriteEmail()
On Error GoTo HarunErrHandler
Sheets("ABC").Range("M1") = "abc#hotmail.com"
Sheets("ABC2").Range("M1") = "abc2#hotmail.com"
Exit Sub
HarunErrHandler:
MsgBox "No such sheet found.", vbInformation, "Info"
'MsgBox("Error Number: " & Err.Number & vbCrLf & Err.Description, vbCritical, "Error")
End Sub
Many thanks Harun i made a little change with your comments.
Following code worked well.
Thank you so much
Sub WriteEmail()
Sheets("ABC").Range("M1") = "abc#hotmail.com"
On Error Resume Next
Sheets("ABC2").Range("M1") = "abc2#hotmail.com"
On Error Resume Next
Sheets("ABC3").Range("M1") = "abc3#hotmail.com"
On Error Resume Next
MsgBox ("process done")
End Sub

How to match/search the value of Sheet1.Range("A1") in Sheet2.Range("A1:A10") in excel VBA

Let's say i have this value "ABC123" in Sheet1.Range("A1")
I want to search for/ match this value in Sheet2.Range("A1:A10") // or the column
If the value is found
//msgbox "Found"
else
//msgbox "Not found"
end if
try this:
Sub foo()
Dim t As Long
On Error Resume Next
t = Application.WorksheetFunction.Match(Worksheets("Sheet1").Range("A1"), Worksheets("Sheet2").Range("A:A"), 0)
On Error GoTo 0
If t > 0 Then
MsgBox "Found"
Else
MsgBox "Not found"
End If
End Sub
Try using the Match function below (you will get the row number found):
Option Explicit
Sub MatchTest()
Dim MatchRes As Variant
MatchRes = Application.Match(Worksheets("Sheet1").Range("A1").Value, Worksheets("Sheet2").Range("A1:A10"), 0)
If IsError(MatchRes) Then
MsgBox "Not found"
Else
MsgBox "Found at row " & MatchRes
End If
End Sub
Consider:
Sub dural()
Dim s As String, r1 As Range, r2 As Range, r3 As Range
Set r1 = Sheet1.Range("A1")
Set r2 = Sheet2.Range("A1:A10")
s = r1.Value
Set r3 = r2.Find(what:=s, after:=r2(1))
If r3 Is Nothing Then
MsgBox "not found"
Else
MsgBox "Found"
End If
End Sub
just playing around a little bit with Scott's solution:
Sub foo2()
Dim t As Long
On Error GoTo NotFound
t = Application.WorksheetFunction.Match(Worksheets("Sheet1").Range("A1"), Worksheets("Sheet2").Range("A:A"), 0)
MsgBox "Found
Exit Sub
NotFound:
MsgBox "Not found"
End Sub

Error 424 Object needed - Cant seem to find the error

i am farly new to VBa and am trying to learn by building or replicating existing vba sheets.
In this one, i am getting an error in the following code:
Private Sub lstLookup_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
'declare the variables
Dim cPayroll As String
Dim I As Integer
Dim findvalue
'error block
On Error GoTo errHandler:
'get the select value from the listbox
For I = 0 To lstLookup.ListCount - 1
If lstLookup.Selected(I) = True Then
cPayroll = lstLookup.List(I, 1)
End If
Next I
'find the payroll number
Set findvalue = Sheet2.Range("F:F").Find(What:=cPayroll, LookIn:=xlValues).Offset(0, -3)
'add the database values to the userform
cNum = 21
For X = 1 To cNum
Me.Controls("Reg" & X).Value = findvalue
Set findvalue = findvalue.Offset(0, 1)
Next
'disable adding
Me.cmdAdd.Enabled = False
Me.cmdEdit.Enabled = True
'error block
On Error GoTo 0
Exit Sub
errHandler::
MsgBox "An Error has Occurred " & vbCrLf & "The error number is: " _
& Err.Number & vbCrLf & Err.Description & vbCrLf & _
"Please notify the administrator"
End Sub
It is giving me the error :" 424 Object required"
i cant seem to find the error
Can someone help me?
Thanks in advance.
Change
Me.cmdAdd.Enabled = False
Me.cmdEdit.Enabled = True
to
Me.Controls("cmdAdd").Enabled = False
Me.Controls("cmdEdit").Enabled = True

Listbox - Unable to recognize blank value

I have two listboxes in my script which are populated with Month values and Year values. If the listbox is populated with values, it writes them to a worksheet, whereas if they are blank, it should throw an error.
I created a test scenario where I have two blank listboxes, the code below should throw a message box advising the user to select a value before executing the script. However, VBA has a mind of its own and thinks that the list boxes are populated with values, therefore I get:
Runtime Error 1004 - Unable to get the List property of the DropDown class
Is there something wrong with the code below that I am not seeing?
Sub TestDropDown()
Dim MonthBox As DropDown, YearBox As DropDown
Dim WB As Workbook
Dim WS4 as Worksheet
Set WB = ThisWorkbook
Set WS4 = WB.Worksheets("Config")
Set MonthBox = ThisWorkbook.Worksheets("Control Sheet").DropDowns("Drop Down 8")
Set YearBox = ThisWorkbook.Worksheets("Control Sheet").DropDowns("Drop Down 9")
'Check Monthbox for values
If IsNull(MonthBox.Value) Then
MsgBox ("Select a Month before running the script")
Failvalue = 1
GoTo EndSub:
Else
WS4.Cells(2, 9).Value = MonthBox.List(MonthBox.Value)
End If
'Check Yearbox for values
If IsNull(YearBox.Value) Then
MsgBox "Please select a Year before running the script", vbExclamation
Failvalue = 1
GoTo EndSub:
Else
WS4.Cells(2, 10).Value = YearBox.List(YearBox.Value)
End If
EndSub:
If Failvalue = 0 Then
MsgBox ("Process complete")
Else
MsgBox "Process failed to complete", vbCritical
End If
WS4.Visible = xlSheetHidden
End Sub
You should check the .ListIndex instead of the .Value. If ListIndex returns zero, it means none is selected.
Try this:
Sub TestDropDown()
Dim MonthBox As DropDown, YearBox As DropDown, Failvalue As Integer
Dim WB As Workbook
Dim WS4 As Worksheet
Set WB = ThisWorkbook
Set WS4 = WB.Worksheets("Config")
Set MonthBox = ThisWorkbook.Worksheets("Control Sheet").DropDowns("Drop Down 8")
Set YearBox = ThisWorkbook.Worksheets("Control Sheet").DropDowns("Drop Down 9")
'Check Monthbox for values
'If IsNull(MonthBox.Value) Then
If MonthBox.ListIndex = 0 Then
MsgBox ("Select a Month before running the script")
Failvalue = 1
GoTo EndSub:
Else
WS4.Cells(2, 9).Value = MonthBox.List(MonthBox.ListIndex)
End If
'Check Yearbox for values
'If IsNull(YearBox.Value) Then
If YearBox.ListIndex = 0 Then
MsgBox "Please select a Year before running the script", vbExclamation
Failvalue = 1
GoTo EndSub:
Else
WS4.Cells(2, 10).Value = YearBox.List(YearBox.ListIndex)
End If
EndSub:
If Failvalue = 0 Then
MsgBox ("Process complete")
Else
MsgBox "Process failed to complete", vbCritical
End If
WS4.Visible = xlSheetHidden
End Sub
It looks like the following line of code wasn't sufficent as the listbox value wasn't equating to null.
If IsNull(YearBox.Value) then
If a ran a msgbox on the listbox value, it was returning 0 on a blank listbox so I changed the code to the following
If YearBox.Value = 0 then
This works fine!