Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Excel 2010 VBA动态信息到静态变量_Arrays_Variables_Excel_Dynamic_Vba - Fatal编程技术网

Arrays Excel 2010 VBA动态信息到静态变量

Arrays Excel 2010 VBA动态信息到静态变量,arrays,variables,excel,dynamic,vba,Arrays,Variables,Excel,Dynamic,Vba,我有很多单元格的信息格式如下 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' 我想使用VBA将引用作为分隔符拆分信息 我把那间牢房当作一根绳子 我编写了一个循环,每次遍历字符串一个字符,并对引号进行计数这里正好是四个,但这可能会改变它将计数存储在一个变量中:numQuotes 我编写了第二个循环来遍历字符串并注意引号的数字位置。我初始化了一个名为positions的动态数组,并给它一个n

我有很多单元格的信息格式如下

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

我想使用VBA将引用作为分隔符拆分信息

我把那间牢房当作一根绳子

我编写了一个循环,每次遍历字符串一个字符,并对引号进行计数这里正好是四个,但这可能会改变它将计数存储在一个变量中:numQuotes

我编写了第二个循环来遍历字符串并注意引号的数字位置。我初始化了一个名为positions的动态数组,并给它一个numQuotes大小。然后,每次它检测到一个引号时,它都会将该引号位置号输入到数组中

代码如下所示:

   ' loop2
    ' start with a dynamic array
    Dim positions() As Integer, size As Integer, k As Integer
    size = quoteCount
    ReDim positions(size)

    Dim quoteCount2 As Integer
    quoteCount2 = 0

    For k = 1 To stringLength

            Dim CharInspector2 As String
            CharInspector2 = Mid(cellContents, k, 1)
            If CharInspector2 = "'" Then
            quoteCount2 = quoteCount2 + 1
            positions(quoteCount2) = k
            End If
    Next k
一切都好!但是现在我需要将报价位置输出到静态变量中,以便在代码的下一部分中使用

有办法吗

谢谢

Aharon这里有一种方法:

Option Explicit

Sub test()
Dim foundPosition As Integer
Dim startPosition As Integer
Dim testString As String

testString = "XXXXXXXXXXXXXXXXXXX_'XXXXXXXXXXXXXXXXXXX'XXXXXXXXXXXXXXXXXXX'X'"

startPosition = 0

Do
    startPosition = startPosition + 1
    foundPosition = InStr(startPosition, testString, "'")
    If foundPosition <> 0 Then
        ' Do whatever you need to do with that position, here I'm just spitting out the position to the immediate window
        Debug.Print (foundPosition)
    End If
    startPosition = foundPosition
Loop Until startPosition = 0
End Sub

你为什么不在字符串上使用Split呢?