Application Match Error 13 I dont understand - excel

I'm trying to find a solution of
application.match error code 13
this is just a simple code in vba, is someone who can can help me fixing this code
Private Sub cmbName_Change()
If Me.cmbName.Value <> "" Then
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("DATABASE")
Dim i As Integer
i = Application.Match(VBA.CLng(Me.cmbName.Value), sh.Range("A:A"), 0)
Me.txtDatepicker.Value = sh.Range("A" & y).Value
Me.cmbAddress.Value = sh.Range("C" & i).Value
Me.txtContact.Value = sh.Range("D" & j).Value
Me.cmbEducation.Value = sh.Range("E" & i).Value
Me.cmbSpecify.Value = sh.Range("F" & i).Value
Me.txtAge.Value = sh.Range("G" & j).Value
If sh.Range("H" & i).Value = "Male" Then Me.optMale.Value = True
If sh.Range("H" & i).Value = "Female" Then Me.optFemale.Value = True
Me.cmbTraining.Value = sh.Range("I" & i).Value
Me.cmbEmployment.Value = sh.Range("J" & i).Value
Me.txtOthers.Value = sh.Range("K" & i).Value
Me.txtAction.Value = sh.Range("L" & i).Value
Me.txtLivelihood.Value = sh.Range("M" & i).Value
End If
End Sub

Error code 13 stands for Type Mismatch.
Possibility 1
You get this error because you are passing a string that contains characters that can't be interpreted as number to the Clng function. Hence, it can't be converted to a long variable.
To solve this you could add some error handling to convert to a long only when Clng doesn't return an error or you could get rid of the Clng function all together and make sure that Column A is formatted as text (since that's the range you are matching to).
Possibility 2
You get this error because there is no cell in column A that matches the value in the combobox.
To solve this, you would need to add some error handling and decide what you want to do when there is no match.

Related

excel VBA concatenate function

First, I'm new to programming VBA to excel so if this program makes no sense...here's to the learning curve.
I'm trying to concatenate a range from "BO1:BQ" & DLimportLastRow, where DLimportLastRow is the last row of a variable import that was just run before this code. I've done as much internet research as i can, but most of the concatenate codes i found make no sense to me so i can't modify them to meet my needs. The end goal is to concatenate BO1:BQ" & DLimportLastRow with each cell separated by a "," and fitting into one cell which is "AE" & LastRow, where LastRow is the next blank cell in "AE".
DLimportLastRow = WorkEnd.Cells(WorkEnd.Rows.Count, "BO").End(xlUp).Offset(1).Row
DLimportLastRow = "BQ" & DLimportLastRow
i = 1
Do Until IsEmpty(Range("BO" & i).Value) = True Or Range("C" & i).Value = "" Or Range("C" & i).Value = Null Or Range("C" & i).Value = 0
x = Workbooks(WName).Worksheets("ReportGroupingInfo").Range("BO" & i).Value
For Each cell In Range("BO1", DLimportLastRow)
y = x & cell.Value & ","
i = i + 1
Next
Loop
Range("AE" & LastRow).Value = y
Got it:
Dim y as string
i = 1
Do Until IsEmpty(Range("BO" & i).Value) = True Or Range("C" & i).Value = "" Or Range("C" & i).Value = Null Or Range("C" & i).Value = 0
For Each cell In Range("BO1", DLimportLastRow)
y = y & cell.Value & ","
i = i + 1
Next
Loop
Range("AE" & LastRow).Value = y

Trying to call a list of first and last names but returns a mismatch error

I created a form that recalls information from a sheet i.e. first and last names. In the name combobox, when typing the name out on the form, if you type a name that isn't located on the list, a mismatch occurs. How do I remedy this? If I remove the 0 value the function no longer matches correctly.
* Call previous fields *
Private Sub ComboBox4_Change()
If Me.ComboBox4.Text <> "" Then
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Database")
Dim i As String
i = Application.Match(Me.ComboBox4.Value, sh.Range("A:A"), 0)
Me.TextBox2.Value = sh.Range("B" & i).Value
If sh.Range("H" & i).Value = "Introduced" Then Me.OptionButton1.Value = True
If sh.Range("H" & i).Value = "Not Introduced" Then Me.OptionButton2.Value = True
Me.ComboBox1.Value = sh.Range("C" & i).Value
Me.ComboBox2.Value = sh.Range("D" & i).Value
Me.ComboBox3.Value = sh.Range("M" & i).Value
Me.TextBox11.Value = sh.Range("J" & i).Value
Me.TextBox10.Value = sh.Range("K" & i).Value
Me.TextBox9.Value = sh.Range("L" & i).Value
Me.TextBox12.Value = sh.Range("I" & i).Value
Me.TextBox5.Value = sh.Range("G" & i).Value
Me.TextBox7.Value = sh.Range("N" & i).Value
End If
End Sub
Sometimes using functions native to VBA makes things a little easier. You can use Range.Find instead of the worksheet function Match for your code.
Dim sh As Worksheet: Set sh = ThisWorkbook.Sheets("Database")
Dim xName As Range
Set xName = sh.Range("A:A").Find Me.ComboBox4.Value
If xName Is Nothing Then
'End sub if not found
MsgBox "Name Not Found - Existing Sub"
Else
'To access the row use xName.Row
Me.TextBox2.Value = sh.Range("B" & xName.Row).Value
'.... rest of code
End If

How to fix "Run time error '-2147417848(80010108) Method 'Range' of Object _ Worksheet' Failed" caused by code within a worksheet

I'm writing a code that calculate number automatically every time you edit a sheet. But somehow the code I wrote is not functioning properly that it gives a run-time error. I checked the cells and range but they are all valid and correct. All of the inputs and variables involved are simple integers (no more than 3 digits).
I just got a work assignment to automate some excel sheets at work and I just learned vba from ground up recently.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim A As Integer
Dim i As Byte
i = 5
For i = 5 To 12
If Worksheets("Sheet1").Range("D" & i).Value = "" Or Worksheets("Sheet1").Range("D" & i).Value = 0 Then
A = Worksheets("Sheet1").Range("E" & i).Value - Worksheets("Sheet1").Range("C" & i).Value
Worksheets("Sheet1").Range("F" & i).Value = A
Else
Worksheets("Sheet1").Range("F" & i).Value = Worksheets("Sheet1").Range("D" & i).Value * Worksheets("Sheet1").Range("B" & i).Value _
+ Worksheets("Sheet1").Range("E" & i).Value - Worksheets("Sheet1").Range("C" & i).Value
End If
Next i
End Sub
It gives a run-time error
Give this a shot and let me know what error you get:
Private Sub Worksheet_Change(ByVal Target As Range)
'Only run if something changes in column D or E
If Target.Column = 4 Or Target.Column = 5 Then
'Turn off any events so that we don't encounter recursion
Application.EnableEvents = False
'This will help readability a bit
Dim sht As Worksheet
Set sht = ThisWorkbook.Worksheets("Sheet1")
Dim A As Integer
Dim i As Long
'This needs to be removed - it's irrelevant as i is used as an iterable on the next line
'i = 5
For i = 5 To 12
If sht.Range("D" & i).Value = "" Or sht.Range("D" & i).Value = 0 Then
'What's the point of using a variable here?
A = sht.Range("E" & i).Value - sht.Range("C" & i).Value
sht.Range("F" & i).Value = A
Else
'Order of operations - is that important here?
'Are we certain these fields are numeric?
sht.Range("F" & i).Value = sht.Range("D" & i).Value * sht.Range("B" & i).Value _
+ sht.Range("E" & i).Value - sht.Range("C" & i).Value
End If
Next i
'Turn it back on once we're done
Application.EnableEvents = True
End If
End Sub

Types Mismatch error on range value, can't figure out why

So Every time I run my code I get the Types Mismatch error and I can't find what to change into what in order to run it properly. Even though this might still not entirely Solve the greater problem which is how to formulate my question into the vba code, but first things first. How to get this line of code without errors:
Range("I" & r & ":" & "I" & B).Value = Range("I" & r & ":" & "I" & B).Value + 1
Where r = the current row the code is checking ( 5 to 44)
and B is the last row the code can check ( 44)
All I want this line to do is to add one to the already existing value of the cell (which is 0 if nothing is done in that row or a formula if conditions are met, this formula will make a value from 1 to 40)
You cannot do this the way you are doing -> you cannot add a number to a range of cells at once. You must add the number to one cell at a time.
Try this:
Dim i As Integer
For i = r To B
Range("I" & i).Value = Range("I" & i).Value + 1
Next
To handle formulas:
Dim i As Integer
For i = r To B
Dim numberToAdd As Integer
numberToAdd = 1
If Range("I" & i).HasFormula Then
Range("I" & i).Value = Range("I" & i).Formula & "+" & Trim(Str(numberToAdd))
Else
Range("I" & i).Value = Range("I" & i).Value + 1
End If
Next

Error Overflow in VBA

I'm a newbie to VBA. Recently, I have typed some codes and following is the sample of my codes:
Dim n As Long
n = Range("A1", Range("A1").End(xlDown)).Rows.Count
For i = 3 To n
Range("P" & i).Value = WorksheetFunction.IfError(Range("N" & i).Value / Range("O" & i).Value, 0))
Next
And it turns out to have the error of Overflow. I have searched on the Internet and figure out it my sample code should be converted to Long type data. However, when I change into:
Range("P" & i).Value = CLng(WorksheetFunction.IfError(CLng(Range("N" & i).Value) / CLng(Range("O" & i).Value), 0))
the problem also remains.
Thank you for any help !
The division in your code (Range("N" & i).Value / Range("O" & i).Value) is happening before it is passed as a parameter to the IfError function. Therefore, if the division fails, your code crashes and the IfError never gets a chance to do anything.
An alternate way of doing this would be:
Dim n As Long
n = Range("A1", Range("A1").End(xlDown)).Rows.Count
For i = 3 To n
'Set the value in column P to a default value
Range("P" & i).Value = 0
'Switch on error handling
On Error Resume Next
'Attempt the calculation - if it fails, the value in column P will not change
Range("P" & i).Value = Range("N" & i).Value / Range("O" & i).Value
'Switch error handling off again
On Error GoTo 0
Next
You can check whether the cell value is zero or null. If not you can perform your caluculation.
Sub Demo()
Dim n As Long
n = Range("A1", Range("A1").End(xlDown)).Rows.Count
For i = 3 To n
If NotNullOrZero(Range("O" & i).Value) Then
Range("P" & i).Value = WorksheetFunction.IfError(Range("N" & i).Value / Range("O" & i).Value, 0)
Else
Range("P" & i).Value = ""
End If
Next
End Sub
Public Function NotNullOrZero(aValue As Variant) As Boolean
' Returns true if the value is not null and greater than zero
If Not IsNull(aValue) Then
If (aValue > 0) Then
NotNullOrZero = True
End If
End If
NotNullOrZero = False
End Function
Got NotNullOrZero function from here answered by #BrianKE.

Resources