【发布时间】:2018-05-19 07:33:35
【问题描述】:
我必须制作一个应用程序来在 excel vba 中加载图像,加密该图像并将其保存在单元格中。我的问题是单元格中的字符限制(32.767 个字符),而我的加密字符串就像 800k 个字符。
我创建了一个函数来创建一个具有自定义字符串字符长度的字符串数组以保存在一行中,但是当我保存在单元格中时,我收到了这个错误:
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
现在的问题是:我可以让它更快或更有效地解决这个问题吗?
【问题讨论】:
-
低悬果警报:为什么不使用 Long 而不是 Integer 并摆脱 ind 并在 Cells() 中的位置使用 index + 1?添加 DoEvents 而不是 Wait 有帮助吗?关闭屏幕更新,直到操作完成。并且需要函数签名末尾的 () 吗?
-
问题不在于 SplitString 函数,该函数可以工作,但是当我添加从该函数返回的数组值时,它会让我内存不足。 @QHarr,该索引是我的单元格的计数,因为我将每个元素添加到同一行的不同单元格中。编辑:DoEvent 给出同样的错误
-
是的......但它与 Index 做同样的事情,所以我认为你添加了不必要的代码行。
-
是的,我像你说的那样修改了那个指令,但是如果我删除等待指令,仍然会出现同样的错误