0

Could you please help me on below error in vba. Formula works but there are error. My scenario is need to extract in between number (between two "-")

Ite1-223466678-ghtrdhjuyr321
Ite-654354477-hjuyt-
Dftehh-767678765-4yutiuy

Extract only this 9 digit in between "-" and need to apply till last row of my excel through VBA

Formula:

= Value(mid(A1,search("-",A1)+1, search ("-",A1, search ("-", A1)+1-search("-", A1)-1))

VBA:

Sub Data2

Dim lastrow as long 

Lastrow=Range("A"&rows.count).end(xlup).row
Range("F2:F" & lastrow).formula = " = 
Value(mid(A1,search("-",A1)+1, search 
("-",A1, search ("-", A1)+1- 
 search("-.   ", A1)-1)"

End sub

ERROR

  • #VALUE ERROR need to fix am not sure how to do if search fails in formula

  • RUNTIME ERROR 13 data mismatch

James Z
  • 12,209
  • 10
  • 24
  • 44
Rahul2020
  • 33
  • 6

1 Answers1

1

Give this a try:

Sub BetweenDashs()
    Dim lastrow As Long, i As Long
    lastrow = Range("A" & Rows.Count).End(xlUp).Row
    
    For i = 2 To lastrow
        arr = Split(Range("A" & i).Value, "-")
        If UBound(arr) > 0 Then
            Range("F" & i).Value = CLng(arr(1))
        End If
    Next i
End Sub

EDIT#1:

If your data is in column C rather than column A, then use:

Sub BetweenDashs()
    Dim lastrow As Long, i As Long
    lastrow = Range("C" & Rows.Count).End(xlUp).Row
    
    For i = 2 To lastrow
        arr = Split(Range("C" & i).Value, "-")
        If UBound(arr) > 0 Then
            Range("F" & i).Value = CLng(arr(1))
        End If
    Next i
End Sub
Gary's Student
  • 95,722
  • 10
  • 59
  • 99