VBA 中的 UDF 中的长字符串错误 #VALUE 用于 Excel 中的输出
Error #VALUE with long string in UDF in VBA for output in Excel
我使用下面的 UDF 连接引用以将结果包含在
SQL 查询,例如 ref in ('ref1', 'ref2', ...)
.
UDF 正常工作,但是当我需要放置一个巨大的引用列表时,
我在 Excel.
中得到 #VALUE
我已经看过 this answer,但我无法使我的 UDF 工作...
我试图将函数的类型从 String
更改为 Variant
(明确地),
但它并没有改变任何事情...
我还尝试了 ConcatSQL2 = A(0)
和 ConcatSQL2 = A
的输出,
Dim A(0 To 0) As String
对于声明,...再次不起作用...
我 运行 没主意了...
有关信息,结果字符串预计长度约为 220k...
为了帮助您生成大量文本,您可以使用 Lorem Ipsum generator here!
Public Function ConcatSQL2(Plage As Range, Optional Doublon As Boolean = True)
Dim A() As String, _
Cel As Range
ReDim A(0)
A(0) = "('"
For Each Cel In Plage.Cells
If (Cel.Value <> "" And (InStr(1, A(0), Cel.Value) = 0 Or Doublon)) Then
A(0) = A(0) & Cel.Value & "', '"
End If
Next
A(0) = Left(A(0), Len(A(0)) - 3) & ")"
ConcatSQL2 = A(0)
End Function
关于@Rory 的评论:
32767 is the maximum number of characters in a cell
我决定将输出写到一个文本文件中以便以后重复使用!
这是最终的解决方案
Public Function ConcatSQL2(Plage As Range, Optional Doublon As Boolean = True)
Dim A(0 To 0) As String, _
myFile As String, _
Cel As Range
'ReDim A(0)
A(0) = "('"
For Each Cel In Plage.Cells
If (Cel.Value <> "" And (InStr(1, A(0), Cel.Value) = 0 Or Doublon)) Then
A(0) = A(0) & Cel.Value & "', '"
End If
Next
A(0) = Left(A(0), Len(A(0)) - 3) & ")"
myFile = "Path\FileName.txt"
Open myFile For Output As #1
Write #1, A(0)
Close #1
ConcatSQL2 = A
End Function
我使用下面的 UDF 连接引用以将结果包含在
SQL 查询,例如 ref in ('ref1', 'ref2', ...)
.
UDF 正常工作,但是当我需要放置一个巨大的引用列表时,
我在 Excel.
#VALUE
我已经看过 this answer,但我无法使我的 UDF 工作...
我试图将函数的类型从 String
更改为 Variant
(明确地),
但它并没有改变任何事情...
我还尝试了 ConcatSQL2 = A(0)
和 ConcatSQL2 = A
的输出,
Dim A(0 To 0) As String
对于声明,...再次不起作用...
我 运行 没主意了...
有关信息,结果字符串预计长度约为 220k...
为了帮助您生成大量文本,您可以使用 Lorem Ipsum generator here!
Public Function ConcatSQL2(Plage As Range, Optional Doublon As Boolean = True)
Dim A() As String, _
Cel As Range
ReDim A(0)
A(0) = "('"
For Each Cel In Plage.Cells
If (Cel.Value <> "" And (InStr(1, A(0), Cel.Value) = 0 Or Doublon)) Then
A(0) = A(0) & Cel.Value & "', '"
End If
Next
A(0) = Left(A(0), Len(A(0)) - 3) & ")"
ConcatSQL2 = A(0)
End Function
关于@Rory 的评论:
32767 is the maximum number of characters in a cell
我决定将输出写到一个文本文件中以便以后重复使用!
这是最终的解决方案
Public Function ConcatSQL2(Plage As Range, Optional Doublon As Boolean = True)
Dim A(0 To 0) As String, _
myFile As String, _
Cel As Range
'ReDim A(0)
A(0) = "('"
For Each Cel In Plage.Cells
If (Cel.Value <> "" And (InStr(1, A(0), Cel.Value) = 0 Or Doublon)) Then
A(0) = A(0) & Cel.Value & "', '"
End If
Next
A(0) = Left(A(0), Len(A(0)) - 3) & ")"
myFile = "Path\FileName.txt"
Open myFile For Output As #1
Write #1, A(0)
Close #1
ConcatSQL2 = A
End Function