是否可以在VBA中划分数组

时间:2013-08-21 16:11:41

标签: arrays vba search binary split

是否可以分割数组?

示例:

array(2) As String
array(1) = "test1"
array(2) = "test2"

~ Now Split

array1 (contains test1) & array 2 (contains test2)

我想实现Binarysearch

3 个答案:

答案 0 :(得分:0)

你可以像这样分开

Sub split_array()

Dim array1(1 To 2) As String
Dim array2(1 To 2) As String
Dim array3(1 To 2) As String

array1(1) = "Test1"
array1(2) = "Test2"

array2(1) = array1(1)
array3(1) = array1(2)

End Sub

但我怀疑这不是最好的方法。我认为使用3(可能是长整数)变量来表示数组中的位置会更好。 1表示第1个元素,1表示最后一个元素,1表示中间元素。

Dim lLowerSearchElement As Long
Dim lUpperSearchElement As Long
Dim lMiddleSearchElement As Long
Dim array1(1 to 999) as string

lLowerSearchElement = 1
lUpperSearchElement = 999
lMiddleSearchElement = (lUpperSearchElement + lLowerSearchElement) / 2

然后,您可以检查元素是否等于,大于或小于中间元素,然后相应地继续。

另外请记住,在尝试使用二进制搜索之前,您需要对数据进行排序,如果您了解递归调用,则会很有用。

您还需要严格测试您的实施,因为一个小错误可能导致搜索无法正常工作。

编辑2013年8月22日

我用于二进制搜索的实现如下:

Function bCheckSamplePoint(ByRef lSamplePointArray() As String, ByRef bfound As Boolean, _
    ByVal lSamplePoint As String) As Boolean
    'byref used for the array as could be slow to keep copying the array, bFound is used by calling procedure

    Dim lLowerSearchElement As Long
    Dim lUpperSearchElement As Long
    Dim lMiddleSearchElement As Long

    bfound = False 'False until found

    'Set initial limits of the search
    lLowerSearchElement = 0
    lUpperSearchElement = UBound(lSamplePointArray())

    Do While lLowerSearchElement <= lUpperSearchElement And bfound = False
        lMiddleSearchElement = (lUpperSearchElement + lLowerSearchElement) / 2
        If StrComp(lSamplePointArray(lMiddleSearchElement), lSamplePoint, vbTextCompare) = -1 Then
'            'Must be greater than middle element
            lLowerSearchElement = lMiddleSearchElement + 1
        ElseIf (lSamplePointArray(lMiddleSearchElement) = lSamplePoint) Then
            bfound = True
        Else
            'must be lower than middle element
            lUpperSearchElement = lMiddleSearchElement - 1
        End If 'lSamplePointArray(lmiddlesearchlelemnt) < lSamplePoint

    Loop 'While lLowerSearchElement <= lUpperSearchElement

ErrorExit:
    bCheckSamplePoint = bReturn
    Exit Function

正如你所看到的,这个二进制搜索只是检查是否在字符串数组中找到了一个字符串,但它可以被修改用于其他目的。

答案 1 :(得分:0)

您不需要拆分功能来执行二进制搜索

我的VBA版二进制搜索可以在
找到 http://fastexcel.wordpress.com/2011/08/02/developing-faster-lookups-part-3-a-binary-search-udf/

答案 2 :(得分:0)

将数组拆分为大块

Public Function splitArray(ByVal initial_array As Variant, Optional chunk_size As Long = 1) As Variant()
  Dim split_array() As Variant
  Dim chunk() As Variant
  Dim chunk_index As Integer: chunk_index = 0
  Dim array_index As Integer: array_index = 1

  If UBound(initial_array) > chunk_size Then
    For i = 0 To UBound(initial_array)
      If (i + 1) / (chunk_size * array_index) = 1 Or i = UBound(initial_array) Then
        ReDim Preserve chunk(chunk_index)
        chunk(chunk_index) = initial_array(i)
        ReDim Preserve split_array(array_index - 1)
        split_array(array_index - 1) = chunk
        chunk_index = 0
        array_index = array_index + 1
      Else
        ReDim Preserve chunk(chunk_index)
        chunk(chunk_index) = initial_array(i)
        chunk_index = chunk_index + 1
      End If
    Next i
    splitArray = split_array
  Else
    ReDim Preserve split_array(0)
    split_array(0) = initial_array
    splitArray = split_array
  End If

End Function