【问题标题】:VBA: Cannot put Dictionary items into range [duplicate]VBA:无法将字典项目放入范围[重复]
【发布时间】:2018-04-19 21:07:24
【问题描述】:

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

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

我很确定这是可行的,因为我可以 Debug.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

函数只写键,不写项目。为什么?我很高兴这些项目在那里,因为我可以简单地 Debug.Print 它们。最令人沮丧的是,如果某些行没有被读取,write 函数可以正常工作,但我发誓这些行没有什么特别的......

我列出了主要的子:

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

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

【问题讨论】:

  • (简而言之,我将在工作表中将项目所在的列称为 items-column。) 你是说当前(不正确)输出是 items-column 是空白的,还是用键值填充?我假设代码没有抛出任何错误,因为你没有提到任何错误。
  • @Mistella 嗨,我找到了原因,似乎字典的一项长度超过 255 个字符并导致问题。它只显示键,而不是项目。

标签: vba excel


【解决方案1】:

好的,伙计们,我已经确定了问题所在。我有一把钥匙很长(超过 255 个字符)。我很确定:

  1. Excel 单元格可以容纳超过 255 个字符
  2. Excel 字典可以保存超过 255 个字符的项目(因为我可以 debug.print 这个项目)

但是,由于某种原因,由于这个项目,它破坏了代码。我添加了一条线来限制长度,现在一切都很好。

【讨论】:

  • 怀疑你的问题是使用ApplicationTranpose:如果你跳过它并使用循环来创建你的转置数组,你可以使用你喜欢的任何长度。 stackoverflow.com/questions/35395789/…
  • @Tim Williams 嗨,循环工作但是非常缓慢,甚至禁用了屏幕更新
  • 您是在创建一个数组,然后在一次操作中将其分配给范围,还是逐个单元格地填充范围?基于数组的方法不应该比你原来的方法慢。
  • @TimWilliams 实际上我正在创建字典。稍后我会尝试数组,顺便说一句,谢谢你的帖子,我不知道转置有 255 个字符的限制。
猜你喜欢
  • 1970-01-01
  • 2019-04-05
  • 2019-03-26
  • 2020-03-02
  • 2018-01-15
  • 2012-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多