4

I am using a function that i saw here on Stackoverflow:

Function EXTRACTELEMENT(Txt, n, Separator) As String
 EXTRACTELEMENT = Split(Application.Trim(Txt), Separator)(n - 1)
End Function

It was spliting an array of data, like this:

sRN LMDscandata sRA LMDscandata 1 1 F97BBF 0 0 6D2A 6D2D 71F5A0FA 71F5FD85 0 0 7 0 0 1388 168 0 1 DIST1 3F800000 00000000 D9490 1388 5 6E2 6DC 6E3 6ED 6E1 0 0 0 0 0 0

But when i tried to increase the amount of data:

sRN LMDscandata sRA LMDscandata 1 1 F97BBF 0 0 FCDF FCE2 9DC90606 9DC9637B 0 0 7 0 0 1388 168 0 1 DIST1 3F800000 00000000 C3500 1388 3D 525 50B 518 508 51D 50A 51A 502 514 50F 502 51C 50E 51C 50E 4FF 509 505 50B 4F9 505 51B 513 516 501 50F 509 4FE 505 508 50C 507 50C 50E 51A 511 514 528 511 519 524 52E 526 522 524 535 534 52E 527 52F 52E 53D 52F 550 535 547 548 559 551 557 558 0 0 0 0 0 0

An error is occuring and the VBA returns an error window and no data is split. How can I fix it?

This is the full code, I am coding it to test a sensor output, where I receive some important data in hex & ascii and then transform to dec and make some graphs. This is the function that convert the values. If someone can also give some tips on the Sub, I would appreciate it.

    Sub ler()

Dim ncell
Dim vArr, col
Dim counter, elem_end As Integer
Dim rng1, rng2 As String

 Set ascii = ThisWorkbook.Worksheets("ASCII")
Set medidas = ThisWorkbook.Worksheets("Medidas")
'Valor da última linha preenchida da coluna A'
    ncell = ascii.Range("A65536").End(xlUp).Row
    'Número de elementos'
    elem_end = ascii.Range("B" & ncell).Value

    For counter = 1 To elem_end

    counterplus = counter + 2
    vArr = Split(Cells(1, counterplus).Address(True, False), "$")
Col_Letter = vArr(0)
 col = Col_Letter
Let rng1 = col & ncell
Let rng2 = "A" & ncell
   ascii.Range(rng1).NumberFormat = "@"
    ele = EXTRACTELEMENT(ascii.Range(rng2), counter, " ")
    ascii.Range(rng1).FormulaR1C1 = ele


    Next


    With ascii.Range(Cells(ncell, 1), Cells(ncell, counterplus))
 Set dist = .Find("DIST1", LookIn:=xlValues)
    If Not dist Is Nothing Then
        firstAddress = dist.Address
        Do
            dist1 = firstAddress
            Set dist = .FindNext(dist)
        Loop While Not dist Is Nothing And dist.Address <> firstAddress
    End If
End With


   data_col = ascii.Range(dist1).Column + 5
  data_num = ascii.Cells(ncell, data_col).Value
  dec_num = CLng("&H" & data_num)
  medidas.Range("A" & ncell).Value = dec_num

  For counter2 = 1 To data_num
  asc_value = ascii.Cells(ncell, data_col + counter2).Value
  Dec = CLng("&H" & asc_value)
  medidas.Cells(ncell, counter2 + 1).Value = Dec

  Next


End Sub

In Column B, there is a function that calculates the number of elements of the data in column A

2
  • What is in the error window? Apr 5, 2017 at 18:36
  • Error '13' and the EXTRACTELEMENT function is highlighted. Apr 5, 2017 at 18:41

1 Answer 1

4

You need to declare the variables, you are getting a Type mismatch, at least the first needs to be declared.

Function EXTRACTELEMENT(Txt As String, n As Long, Separator As String) As String
 EXTRACTELEMENT = Split(Application.Trim(Txt), Separator)(n - 1)
 End Function
5
  • But why it works on smaller quantity of datas? And when it gets long the program just breaks? Apr 5, 2017 at 18:47
  • 2
    I can only assume that when it gets long, it tries to bring the variable as a range. Because if I switch Application.Trim(Txt) to Application.Trim(Txt.Value) it also works. Apr 5, 2017 at 18:49
  • 2
    Generally speaking, any number of things could happen when you arent declaring the variables up front. When a type isn't declared a type has to be assumed. The variables all start off as 'Variant' inputs, but then get a type assigned as the program runs. It is always best practice to declare variable types to avoid potential issues during run-time. Apr 5, 2017 at 18:51
  • I have changed and the function is now working, thx I will just have to make some changes on the main code. Apr 5, 2017 at 18:53
  • @ScottCraner -that's what the VBE's Watch window reports.
    – user4039065
    Apr 5, 2017 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.