Runtime error 5 while using Mid() - vba

I am working with string in vba. I would like to do something like this:
Dim matchprodat, matchtxt As MatchCollection
rega2lhex.Pattern = "[\\][\w]*"
regprodat.Pattern = "(Datensatz:|Projekt:)[\s\w*,*]*[\\\w]*"
\\ I am reading each line of a text file
Set matchprodat = regprodat.Execute(line)
If matchprodat.Count <> 0 Then
Set matcha2lhex = rega2lhex.Execute(matchprodat.Item(0))
DCMname = DCMfilename + "<>" + Mid(matcha2lhex.Item(0), 2)
End if
but I am getting the error: Run-time error '5': "Invalid procedure call or argument"
the matcha2lhex.Item(0) is not empty and has the length of 8. What cause this error in Mid()?
If I am watching the value:

Related

VBA - Accessing Text Value Error

I have written the following code:
Function Find_Index(Gender As String)
Dim Index As Long
Do While Gender = Worksheets("Source").Cells(Index, "A").Text
Index = Index + 1
Loop
Find_Index = Index
End Function
I'm getting the following error:
"Application-defined or object-defined error"
Due to the following line in the code:
Do While Gender = Worksheets("Source").Cells(Index, "A").Text
Any ideas why ?
You define Index but never set it to a value before you use it.
Add Index = 1 before the Do While line

VBA Error - Application-defined pr Object-defined Error When using If

I wrote this following code in VBA:
Dim Counter As Long
Dim Counter_Two As Long
Dim Array_Column_Letter(3) As String
Array_Column_Letter(0) = "A"
Array_Column_Letter(1) = "B"
Array_Column_Letter(2) = "C"
Array_Column_Letter(0) = "D"
For Counter = 0 To LastRow
For Counter_Two = 0 To 3
If Worksheets("Sheet1").Cells(Counter, Array_Column_Letter(Counter_Two)).Value = IsEmpty(True) Then
MsgBox ("Hi")
End If
Next Counter_Two
Next Counter
For some reason I'm getting the following error:
Application-defined pr Object-defined Error
VBA says there is something wrong with if statement causing this error. I can't seem to figure out why. Any ideas?
Ranges in Excel start at "A1" not "A0"
This line
If Worksheets("Sheet1").Cells(Counter, Array_Column_Letter(Counter_Two)).Value = IsEmpty(True) Then
Should be changed to
If Worksheets("Sheet1").Cells(Counter + 1 , Array_Column_Letter(Counter_Two)).Value = IsEmpty(True) Then
Also to use isempty correctly you should use it as below
If IsEmpty(Worksheets("Sheet1").Cells(Counter + 1 , Array_Column_Letter(Counter_Two)).Value) Then
You get that error because you are trying to find row 0. Change the starting value in your first For statement

How can I convert an Userform Textbox value which is a String into a Long in Excel Vba?

Here I am trying to read a Textbox.Value which is a String into a Variable (i.e. text_bval) and converting that String into a Long using CLng function for my calculations. The CLng function is showing run time error like type mismatch.
See my code below:
Dim text_bval As String
Dim text_long As Long
Dim per As Integer
text_bval = text_box.value
user_entry = CLng(text_bval)
According to the MS Documentation, "If expression lies outside the acceptable range for the Long subtype, an error occurs."
In this case, your expression is text_bval, a string. If your string contains characters that cannot be converted to a number, an error will occur.
For example:
text_bval = "2" then CLng(text_bval) = 2
text_bval = "2.3" then CLng(text_bval) = 2
text_bval = "hello" then CLng(text_bval) = ERROR
In order to prevent an error from occuring, you should do something like the following:
user_entry = 0
On Error Resume Next
user_entry = CLng(text_bval)
On Error GoTo 0

Getting Error No.: 424 object required upon using String.Length in VBA

I am want to get length of a string. But I am getting error Error No.: 424 object required .
Dim Start as String
Dim LengthofStart as Integer
If Start <> "" Then
MsgBox ("Going to calculate length now::")
lengthofstart = Start.Length
MsgBox ("The lenght of start is " & LengthofStart)
End If
The error is appearing when I get the length of string "Start".
Try Len(Start) instead
lengthofstart = Len(Start)

How to fix "Compile Error: Object Required" error?

This is my very first time with VBA. For the following code fragment:
Dim i As Integer
Set i = 0
For Each v In dictDT.Keys
Cells(10, 5 + i) = dictDT.Item(v)
i = i + 1
Next
I keep getting this error:
Compile Error: Object Required
What am I doing wrong?
Change
Set i = 0
to
i = 0
Only objects require the Set keyword. Other variable types do not.