在vba excel中保存大量的String

时间:2018-05-19 07:33:36

标签: excel vba excel-vba

我必须创建一个应用程序来加载excel vba中的图像,加密该图像并将其保存在单元格中。我的问题是单元格中字符的限制(32.767个字符),我的加密字符串就像800k字符。

我创建一个函数来创建一个带有自定义字符串字符长度的字符串数组以保存在一行中,但是当我保存在单元格中时,我发现了这个错误:

enter image description here

Public Function SplitString(ByVal TheString As String, ByVal StringLen As Integer) As String()
Dim ArrCount As Integer  'as it is declared locally, it will automatically reset to 0 when this is called again
Dim I As Long  'we are going to use it.. so declare it (with local scope to avoid breaking other code)
Dim TempArray() As String
ReDim TempArray((Len(TheString) - 1) \ StringLen)

For I = 1 To Len(TheString) Step StringLen
    TempArray(ArrCount) = Mid$(TheString, I, StringLen)
    ArrCount = ArrCount + 1
Next

SplitString = TempArray   'actually return the value
End Function

Dim StringArray As Variant
        StringArray = SplitString(EncodeFile(.SelectedItems(1)), 30000)

        Dim ind As Integer
        ind = 2

        For index = 1 To UBound(StringArray)
            Sheet5.Cells(55, ind).value = StringArray(index)
            ind = ind + 1
        Next index

我通过在for循环中添加延迟解决了这个问题,但这不是最佳解决方案

        For index = 1 To UBound(StringArray)
            Sheet5.Cells(55, ind).value = StringArray(index)
            ind = ind + 1
            Application.Wait (Now + TimeValue("00:00:01"))
        Next index

现在的问题是:我可以更快或更好地解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

将数组转储到一行中没有循环的单元格中。

Sheet5.Cells(55, "A").resize(1, ubound(StringArray) + 1) = StringArray