Split function for long text fails with VALUE! error

喜你入骨 提交于 2019-12-07 10:03:12

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/43239079/split-function-for-long-text-fails-with-value-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!