I´m creating a Userform that among other things displays the name of employee when the ID field is completed.
In TextBox1 the user enters the ID and in TextBox4 they will see their names.
The problem comes because I´m using Application.Vlookup to complete TextBox4.
I´m not sure where is the error here.
Private Sub TextBox1_Change()
Dim ws As Worksheet, rngvlook As Range
Dim val As String, result As Variant
Set rngvlook = Hoja3.Range("A:B")
val = TextBox1.Value
result = Application.VLookup(val, rngvlook, 2, False)
If IsError(result) Then
TextBox4.Value = ""
Else
TextBox4.Value = result
End If
End Sub
Bellow the error
Error '-2147352571 (80020005) on execution time
Value property can´t be set. Type mismatch
The error is in a way an "architecture" error. Take a look at this VLookups with the following input in Range("A:B"):
The following code returns probabaly something unexpected:
Sub SomeTest()
'Runs ok:
Debug.Print Application.VLookup(1, Range("A:B"), 2, False)
Debug.Print Application.VLookup("id1", Range("A:B"), 2, False)
Dim val1 As Variant: val1 = 1
Debug.Print Application.VLookup(val1, Range("A:B"), 2, False)
'Error 2042:
Debug.Print Application.VLookup("1", Range("A:B"), 2, False)
Dim val2 As String: val2 = 1
Debug.Print Application.VLookup(val2, Range("A:B"), 2, False)
End Sub
Thus, in your code, you go into the second part of the example, thus going to Error 2042. Try to "move" your code to the working sample, thus declare the val as a Variant.
Related
I am starting my first user form in Excel.
I have a ComboBox, which uses a dropdown list to select a value. Once this value is selected it uses VLOOKUP to display the rest of the data in textboxes.
Upon using my reset button on the form, or trying to take out the data in these textboxes, it gives the VLOOKUP runtime error because the data is no longer there.
What do I have to do to stop this from happening?
Private Sub ComboBox1_Change()
Dim MyTableArray As Range, MyEmpID As String
Set MyTableArray = Sheets("CompressorData").Range("A:D")
Me.txtName.Value = WorksheetFunction.VLookup(Me.ComboBox1, MyTableArray, 2, 0)
Me.TextBox3.Value = WorksheetFunction.VLookup(Me.ComboBox1, MyTableArray, 1, 0)
Me.TextBox1.Value = WorksheetFunction.VLookup(Me.ComboBox1, MyTableArray, 4, 0)
End Sub
If it's the error you're trying to avoid (and just that), then include on 'On Error' statement like so:
Sub DropDown1_Change()
Dim MyTableArray As Range, MyEmpID As String
Set MyTableArray = Range("A:D")
On Error GoTo err_trap
DropDown1.txtName.Value = WorksheetFunction.VLookup(DropDown1.ComboBox1, MyTableArray, 2, 0)
DropDown1.TextBox3.Value = WorksheetFunction.VLookup(DropDown1.ComboBox1, MyTableArray, 1, 0)
DropDown1.TextBox1.Value = WorksheetFunction.VLookup(DropDown1.ComboBox1, MyTableArray, 4, 0)
err_trap:
MsgBox ("Caught the error - delete msgbox in VB code and replace with 'Exit Sub' to avoid seeing this message box! hardy har captain")
Exit Sub
End Sub
I am trying to utilize Vlookup function, according to the Textbox1 value user put in in Userform Guntest, automatically looking for corresponding features of the gun.
However the program currently doesn't run as it reminds me
'Runtime error '1004', method 'Range of object' _Global' failed.
The error appears on Retrieve1=…
I will be appreciated if you could help me to check where the problem is as I have really limited knowledge and experience on using VBA.
Thanks in advance.
It looks like some objects is undefined but I can't figure out where.
The module 1 code is:
Public Guncode As String
Option Explicit
Sub Test()
Call Vlookup
End Sub
Sub Vlookup()
Dim Retrieve1 As String
Dim Retrieve2 As String
Dim FinalRow As Long
Dim FinalColumn As Long
Dim WholeRange As String
If GunTest.TextBox1 = "" Then
Exit Sub
If GunTest.TextBox1 <> "" Then
MsgBox Guncode
End If
End If
With Sheets(1)
FinalRow = Range("A65536").End(xlUp).Row
FinalColumn = Range("IV1").End(xlToLeft).Column
WholeRange = "A2:" & CStr(FinalColumn) & CStr(FinalRow)
Retrieve1 = Application.WorksheetFunction.Vlookup(Trim(Guncode), Range(WholeRange), 1, False) 'Locate specific tool according to QR code number
Retrieve2 = Application.WorksheetFunction.Vlookup(Trim(Guncode), Range(WholeRange), 5, False) 'Locate specific gun type according to QR code number
If Guncode = "" Then
MsgBox "This gun doesn't exist in database!"
Else
MsgBox "The tool number is:" & Retrieve1 & vbCrLf & "The gun type is:" & Retrieve2
End If
End With
End Sub
The userform code is:
Option Explicit
Private Sub Label1_Click()
End Sub
Private Sub CommandButton1_Click()
If TextBox1 = "" Then Exit Sub 'Set condition 1 of exiting the program
Guncode = GunTest.TextBox1
With Me
Call Module1.Test
End With
End Sub
Private Sub PartID_Click()
End Sub
Private Sub TextBox1_Change()
End Sub
Private Sub UserForm_Click()
End Sub
It should run properly but it doesn't. Any help would be appreciated, thanks!
First off, you were passing in a number as the column letter value. CSTR() doesnt magically transform it into the letter equivalent but I like your enthusiasm.
Second, your method will bomb if the value isnt found - so you'll need to write your own error handling for it.
Sub Vlookup()
Dim Retrieve1 As String
Dim Retrieve2 As String
Dim FinalRow As Long
Dim FinalColumn As Long
Dim WholeRange As String
Dim vArr
Dim col_Letter As String
If GunTest.TextBox1 = "" Then
Exit Sub
If GunTest.TextBox1 <> "" Then
MsgBox Guncode
End If
End If
With ThisWorkbook.Sheets("Sheet1")
FinalRow = .Range("A65536").End(xlUp).Row
FinalColumn = .Range("IV1").End(xlToLeft).Column
vArr = Split(Cells(1, FinalColumn).Address(True, False), "$")
col_Letter = vArr(0)
WholeRange = "A2:" & col_Letter & CStr(FinalRow) '<---- you were passing a number in as the column value
Retrieve1 = Application.WorksheetFunction.Vlookup(Trim(Guncode), .Range(WholeRange), 1, False) 'Locate specific tool according to QR code number
Retrieve2 = Application.WorksheetFunction.Vlookup(Trim(Guncode), .Range(WholeRange), 5, False) 'Locate specific gun type according to QR code number
If Guncode = "" Then
MsgBox "This gun doesn't exist in database!"
Else
MsgBox "The tool number is:" & Retrieve1 & vbCrLf & "The gun type is:" & Retrieve2
End If
End With
End Sub
1. I am not sure what is the reason using Address(True, False) for row number.
This comes from a combination of these two functions. The true/false setting is telling the funciton to use/not use absolute references in the address.
Split ( expression [,delimiter] [,limit] [,compare] )
https://www.techonthenet.com/excel/formulas/split.php
expression.Address (RowAbsolute, ColumnAbsolute, ReferenceStyle, External, RelativeTo)
https://learn.microsoft.com/en-us/office/vba/api/excel.range.address
Shouldn't Cell (1, FinalColumn) stands for the column number?
No, the cells fucntiosn basically returns an intersection/address of rows & column.
Try this for example: debug.Print; thisworkbook.Sheets("Sheet1").Cells(2,2)
You mentioned CSTR doesn't magically transform to letter equivalent so what would it transform to? Could you further elaborate?
This is a data type conversion function. CSTR(666) essentially does this: this 666 becomes this "666"
2. vArr(0). I am confused with what does the parameter 0 stands for in the bracket. Actually this is a general question I always have regarding to parameter specification.
This is an array position refence. The split function returns an array of strings. Since we're using to capture the column label value, we only need to reference the first position.
(3) I tried copy your code and run it but still reminds me error on the same row.
Works fine for me unless there is no returning value, which returns an error which is what I meant by "bomb."
I want to make VBA, which find out throught "LOOP" the correct row (i + 1) and after that via using "Hlookup function" find out the correct value from "This row". Finding value is for expample "99". I have problem with "hlookup function.
Sub CreatePivotTable1()
Dim maxfromROW As Integer
Set wb = ThisWorkbook
i = 1
Do Until Cells(1 + i, 1).Value = "This row"
i = i + 1
Loop
maxfromROW = Application.WorksheetFunction.HLookup(99, Range("A4:O20"), i + 1, False)
wb.Worksheets("Hárok1").Range("B20").Value = maxOVB2
End Sub
There are plenty of reasons why your code should not be working:
You are not having Option Explicit and you are not declaring variables
You go into endless while loop.
You do not have a value for the HLookup and you are using .WorksheetFunction, which returns automatically run-time error, if no value is found.
Instead of Application.WorksheetFunciton(), try Application.HLookup, which would not throw a run-time error but a simple error, if no value is found:
Option Explicit
Sub TestMe()
Dim someResult As Variant
someResult = Application.HLookup(99, Worksheets(1).Range("A4:O20"), 3, False)
If Not IsError(someResult) Then
Debug.Print someResult
Else
Debug.Print someResult; " is an error!"
End If
End Sub
Sub UpdateFormula()
Dim CurrStr As String
Dim EndRow As Long
On Error GoTo 0
EndRow = Range("A" & Rows.Count).End(xlUp).Row
BaseStr = UCase(Range("A2").Value)
Application.ScreenUpdating = False
For iter = 4332 To EndRow
CurrStr = UCase(Range("A" & iter).Value)
result = Application.WorksheetFunction.VLookup(CurrStr, Sheets("CustAR").Range("A2:A2499"), 1, False)
'=IF(ISERROR(VLOOKUP(A2, CustAR!$A$2:$A2499, 1, FALSE)),"NotFound",VLOOKUP(A2, CustAR!$A$2:$A2499, 1, FALSE))
Next iter
Application.ScreenUpdating = True
End Sub
What's wrong in above code? I get error on the line where result is set.
Error is:
Runtime error 1004 : Application or object defined error
What I am trying to do is to look for value in CustAR sheet. The Excel file has two worksheets including CustAR sheet.
The line next to "result" is the formula that works in Excel.
I believe you have either not dimensioned what result is.
OR
You have but as some sort of object, thus the line needs to be:
Set result = Application...
VLOOKUP is searching for a string but it seems you have not provided one to be found - all numbers perhaps?
I am surprised you get that error message with your code, as I would have thought it would return:
. However, an equivalent for your worksheet formula might look as follows:
result = Application.VLookup(CurrStr, Sheets("CustAR").Range("A2:A2499"), 1, False)
If IsError(result) Then result = "NotFound"
Using VLookup as a property of the Application object, rather than as a property of the Worksheetfunction object, results in result containing an error code (in this case error 2042) instead of causing a VBA run-time error. An alternative would be to test for the VBA runtime error in your original code.
I modified the code as below and it worked.
I removed WorkSheetFunction and also handled the result value.
Sub UpdateFormula()
Dim CurrStr As String
Dim EndRow As Long
On Error GoTo 0
EndRow = Range("A" & Rows.Count).End(xlUp).Row
BaseStr = UCase(Range("A2").Value)
Application.ScreenUpdating = False
For iter = 4332 To EndRow
On Error Resume Next
result = Application.VLookup(CLng(CurrStr), shAR.Range("A2:A2499"), 1, False) '.WorksheetFunction
If result = "Error 2042" Then
result = "NotFound"
Else
result = result
End If
Cells(iter, 2).Value = result
On Error GoTo 0
'=IF(ISERROR(VLOOKUP(A2, CustAR!$A$2:$A2499, 1, FALSE)),"NotFound",VLOOKUP(A2, CustAR!$A$2:$A2499, 1, FALSE))
Next iter
Application.ScreenUpdating = True
End Sub
I'm having a problem populating a userform. I found some code online that does exactly what I want and the 'example' file works perfectly. When I modify it to my needs, it gives me an error message on the following line:
frmModifyData.Skill.Value = Application.VLookup(cmbItemName.Value, Sheets("Enrolled").Range(vrange), 1, False)
Here's the entire code I'm working with:
Dim NotNow As Boolean
Private Sub cmdCancel_Click()
Unload Me
End Sub
Private Sub cmdOkay_Click()
NotNow = True
N = Application.Match(Me.cmbItemName.Value, Range("AB:AB"), 0)
Cells(N, 1).Value = Me.frmEnterData.Skill.Text
Cells(N, 2).Value = Me.frmEnterData.txtCLASS.Text
Cells(N, 3).Value = Me.frmEnterData.LastName.Text
NotNow = False
End Sub
Private Sub cmbItemName_Change()
If NotNow Then Exit Sub
vrange = "FirstField"
'LINE WITH THE PROBLEM
frmModifyData.Skill.Value = Application.VLookup(cmbItemName.Value, Sheets("Enrolled").Range(vrange), 1, False)
'END OF LINE WITH THE PROBLEM (though it could affect the two lines of code below...)
frmModifyData.txtCLASS.Value = Application.VLookup(cmbItemName.Value, Sheets("Enrolled").Range(vrange), 2, False)
frmModifyData.LastName.Value = Application.VLookup(cmbItemName.Value, Sheets("Enrolled").Range(vrange), 3, False)
End Sub
Private Sub UserForm_Initialize()
frmModifyData.cmbItemName.RowSource = "FirstField"
End Sub
'FirstField' is a named range that is defined this way
=OFFSET(Enrolled!$AB$3,0,0,COUNTA(Enrolled!$AB:$AB)-1,3)
Column AB holds the "Full Name" of the user. This is what I'm using to find an individual. Once I pick a name using a drop-down box on the userform, it gives me the message Could not set the Value property. Invalid property value.
How do I fix this so it works?
Try breaking your code down a little and make sure your vlookup is working...
Dim v
v = Application.VLookup(cmbItemName.Value, Sheets("Enrolled").Range(vrange), 1, False)
If Not IsError(v) Then
frmModifyData.Skill.Value = v
Else
Msgbox cmbItemName.Value & " was not found!"
End If