Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.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
VBA:无法将字典项放入范围_Vba_Excel - Fatal编程技术网

VBA:无法将字典项放入范围

VBA:无法将字典项放入范围,vba,excel,Vba,Excel,我正在使用字典存储数据,然后将它们转储到另一张表中。基本上我有这种格式的数据: abc 12367 abe 23456 abe 34567 dfy 78890 我需要这样输出: abc 12367 abe 23456, 34567 dfy 78890 以下是用于存储和输出数据的代码: Function ReadDict(ByVal wb_name As String, ByVal ws_name As String, row_begin A

我正在使用字典存储数据,然后将它们转储到另一张表中。基本上我有这种格式的数据:

abc    12367

abe    23456

abe    34567

dfy    78890
我需要这样输出:

abc    12367

abe    23456, 34567

dfy    78890
以下是用于存储和输出数据的代码:

Function ReadDict(ByVal wb_name As String, ByVal ws_name As String, row_begin As Integer, row_end As Integer, col As Integer) As Dictionary
On Error Resume Next

Dim wbSource As Workbook
Dim wsSource As Worksheet
Dim dictStorage As New Dictionary

Set wbSource = Workbooks(wb_name)
If wbSource Is Nothing Then
    Set wbSource = Workbooks.Open(wb_name)
End If

Dim iRowCounter As Integer
Dim vKey, vItem As Variant

For iRowCounter = row_begin To row_end
    vKey = wbSource.Sheets(ws_name).Cells(iRowCounter, col).Value
    vItem = wbSource.Sheets(ws_name).Cells(iRowCounter, col + 1).Value

    If dictStorage.Exists(vKey) = False Then
        dictStorage.Add vKey, vItem
    Else
        dictStorage.item(vKey) = dictStorage.item(vKey) & ", " & vItem
    End If
Next iRowCounter

Set ReadDict = dictStorage
End Function
我很确定这是可行的,因为我可以调试.Print

写入功能:

Function WriteDict(ByVal wb_name As String, ByVal ws_name As String, row_begin As Integer, col As Integer, dict As Dictionary)
On Error Resume Next

If dict.Count <= 0 Then MsgBox ("Dictionary contains no item!")

Dim wbSource As Workbook
Dim wsSource As Worksheet

Set wbSource = Workbooks(wb_name)
If wbSource Is Nothing Then
    Set wbSource = Workbooks.Open(wb_name)
End If

With Worksheets(ws_name)
    Dim ky As Variant
    With dict
        Range(Cells(2, 1), Cells(2 + .Count, 1)).Value = Application.Transpose(.Keys)
        Range(Cells(2, 2), Cells(2 + .Count, 2)).Value = Application.Transpose(.Items)
    End With
End With

Set dict = Nothing
End Function

如果ReadTJDict()不读取第1950行和第2000行之间的一些行,我很确定它可以正常工作,但没有什么特别之处。

好的,各位,我已经确定了问题所在。我有一个键有一个很长的项(超过255个字符)。我很肯定:

  • Excel单元格可以容纳超过255个字符
  • Excel字典可以保存长度超过255个字符的项(因为我可以调试。打印此项)

  • 然而,由于某种原因,由于这个项目,它破坏了代码。我添加了一行来限制长度,现在一切都好了。

    (简而言之,我指的是工作表中项目应该放在哪里的列,即“项目”列。)您是说当前(不正确)的输出是“项目”列为空,还是用键值填充?我假设代码没有抛出任何错误,因为您没有提到任何错误。@Mistella Hi我找到了原因,似乎字典的一项长度超过255个字符,导致了问题。它只显示键,不显示项目。请怀疑您的问题在于使用了
    ApplicationTranpose
    :如果您跳过该步骤并使用循环创建转置数组,您可以使用任意长度@Tim Williams Hi循环工作非常缓慢,甚至禁用屏幕更新。您是创建一个数组,然后在一次操作中将其分配给范围,还是逐个单元格填充范围单元格?基于数组的方法不应比原始方法慢。@TimWilliams实际上我正在创建一个字典。我稍后会尝试数组,顺便说一句,谢谢你的帖子,我不知道转置有255个字符的限制。
    Sub CombineTJ()
    Application.ScreenUpdating = False
    
    Dim sSourceWB, sSourceWS, sTargetWS As String
    Dim dictTJ As New Dictionary
    sSourceWS = ActiveSheet.Name
    sTargetWS = "Target"
    sSourceWB = ActiveWorkbook.Name
    
    Sheets.Add
    ActiveSheet.Name = sTargetWS
    
    Set dictTJ = ReadTJDict(sSourceWB, sSourceWS, 2, 8442, 1)
    
    Call WriteTJDict(sSourceWB, sTargetWS, 2, 1, dictTJ)
    
    Application.ScreenUpdating = True
    End Sub