Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.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
使用Excel VBA将范围保存到字典_Vba_Excel_Dictionary - Fatal编程技术网

使用Excel VBA将范围保存到字典

使用Excel VBA将范围保存到字典,vba,excel,dictionary,Vba,Excel,Dictionary,我有一个excel表格,其中包含以下行和列。我想将其保存到字典中,这样我就可以使用列a中的年份作为键来引用每一行,然后在列a中添加具有相同年份的行值 A ....|.. B..| ..C..|..D. 2014 | UNION| 5677 | 4556 2014 | UNION| 5677 | 455

我有一个excel表格,其中包含以下行和列。我想将其保存到字典中,这样我就可以使用列a中的年份作为键来引用每一行,然后在列a中添加具有相同年份的行值

A  ....|.. B..| ..C..|..D.                                                                            
 2014  | UNION| 5677 | 4556                  
 2014  | UNION| 5677 | 4556                
 2015  | BEST | 5677 | 4556              
 2015  | BEST | 5677 | 4556
请问我如何才能做到这一点,因为我卡在这个代码。谢谢

A  ....|.. B..| ..C..|..D.                                                                            
 2014  | UNION| 5677 | 4556                  
 2014  | UNION| 5677 | 4556                
 2015  | BEST | 5677 | 4556              
 2015  | BEST | 5677 | 4556
这是我的密码

A  ....|.. B..| ..C..|..D.                                                                            
 2014  | UNION| 5677 | 4556                  
 2014  | UNION| 5677 | 4556                
 2015  | BEST | 5677 | 4556              
 2015  | BEST | 5677 | 4556
Sub AnyThing()
Dim lastrow_DE As Integer

lastrow_DE = DEsheet.Cells(DEsheet.Rows.Count, "E").End(xlUp).Row

DEsheet.Range("A1:L" & lastrow_DE).Select

Selection.AutoFilter field:=2, Criteria1:=Array("UNION", "BEST"), Operator:=xlFilterValues

Selection.AutoFilter field:=5, Criteria1:=Array("2014", "2015"), Operator:=xlFilterValues


Dim rng As Range

Set rng = DEsheet.Range("A2:L" & lastrow_DE).SpecialCells(xlCellTypeVisible)


Dim p As Variant

Dim dict As Scripting.Dictionary

Set dict = New Scripting.Dictionary


For Each p In rng

dict.Add key = p.Items(1).Value, items =p.Items(2).Value, p.Items(3).Value, p.Items(4).Value

Next

Else

End If
End Sub

您可以运行下面的代码。你可以用一本词典。我已经选择创建一个键,它是渴望和你的第二个过滤值的连接,然后在写回工作表时将其拆分

A  ....|.. B..| ..C..|..D.                                                                            
 2014  | UNION| 5677 | 4556                  
 2014  | UNION| 5677 | 4556                
 2015  | BEST | 5677 | 4556              
 2015  | BEST | 5677 | 4556
注意,我已将您的第二个标准字段更改为一个,因为您在A列中显示年份

A  ....|.. B..| ..C..|..D.                                                                            
 2014  | UNION| 5677 | 4556                  
 2014  | UNION| 5677 | 4556                
 2015  | BEST | 5677 | 4556              
 2015  | BEST | 5677 | 4556
代码:

A  ....|.. B..| ..C..|..D.                                                                            
 2014  | UNION| 5677 | 4556                  
 2014  | UNION| 5677 | 4556                
 2015  | BEST | 5677 | 4556              
 2015  | BEST | 5677 | 4556
Option Explicit

Public Sub AnyThing()
    Dim lastrow_DE As Long
    Dim DEsheet As Worksheet
    Set DEsheet = ActiveSheet

    lastrow_DE = DEsheet.Cells(DEsheet.Rows.Count, "E").End(xlUp).Row

    With DEsheet.Range("A1:L" & lastrow_DE)
        .AutoFilter field:=2, Criteria1:=Array("UNION", "BEST"), Operator:=xlFilterValues
        .AutoFilter field:=1, Criteria1:=Array("2014", "2015"), Operator:=xlFilterValues
    End With

    Dim rng As Range, p As Variant, dict As Scripting.Dictionary
    '<== You should add a test here that filter columns contain filter values i.e. there will be visible cells after applying filter
    Set rng = DEsheet.Range("A2:L" & lastrow_DE).SpecialCells(xlCellTypeVisible)
    Set dict = New Scripting.Dictionary

    For Each p In rng.Columns(1).Cells
        If Not dict.Exists(p.Value & "," & p.Offset(, 1)) Then
            dict.Add p.Value & "," & p.Offset(, 1), Application.WorksheetFunction.Sum(p.Offset(, 2).Resize(1, 10))
        Else
            dict(p.Value & "," & p.Offset(, 1)) = dict(p.Value & "," & p.Offset(, 1)) + Application.WorksheetFunction.Sum(p.Offset(, 2).Resize(1, 10))
        End If
    Next p

    Dim key As Variant

    For Each key In dict.Keys
        Debug.Print key & " : " & dict(key)
    Next key

    Sheets.Add

    Dim counter As Long

    With ActiveSheet
        For Each key In dict.Keys
            counter = counter + 1
            .Cells(counter, "A").Resize(1, 2) = Split(key, ",")
            .Cells(counter, "C") = dict(key)

        Next key
    End With

End Sub
选项显式
公共子系统()
昏暗的最后一排
将图纸作为工作表调暗
设置DEsheet=ActiveSheet
lastrow_DE=DEsheet.Cells(DEsheet.Rows.Count,“E”).End(xlUp).Row
带图纸范围(“A1:L”和lastrow\u DE)
.AutoFilter字段:=2,准则1:=数组(“联合”、“最佳”),运算符:=xlFilterValues
.AutoFilter字段:=1,准则1:=Array(“2014”、“2015”),运算符:=xlFilterValues
以
Dim rng作为范围,p作为变量,dict作为脚本。字典

“您可以运行下面的代码。你可以用一本词典。我选择创建一个关键,它是渴望的具体化和你的第二个过滤值,然后在写回工作表后将其拆分

A  ....|.. B..| ..C..|..D.                                                                            
 2014  | UNION| 5677 | 4556                  
 2014  | UNION| 5677 | 4556                
 2015  | BEST | 5677 | 4556              
 2015  | BEST | 5677 | 4556
注意,我已将您的第二个标准字段更改为一个,因为您在A列中显示年份

A  ....|.. B..| ..C..|..D.                                                                            
 2014  | UNION| 5677 | 4556                  
 2014  | UNION| 5677 | 4556                
 2015  | BEST | 5677 | 4556              
 2015  | BEST | 5677 | 4556
代码:

A  ....|.. B..| ..C..|..D.                                                                            
 2014  | UNION| 5677 | 4556                  
 2014  | UNION| 5677 | 4556                
 2015  | BEST | 5677 | 4556              
 2015  | BEST | 5677 | 4556
Option Explicit

Public Sub AnyThing()
    Dim lastrow_DE As Long
    Dim DEsheet As Worksheet
    Set DEsheet = ActiveSheet

    lastrow_DE = DEsheet.Cells(DEsheet.Rows.Count, "E").End(xlUp).Row

    With DEsheet.Range("A1:L" & lastrow_DE)
        .AutoFilter field:=2, Criteria1:=Array("UNION", "BEST"), Operator:=xlFilterValues
        .AutoFilter field:=1, Criteria1:=Array("2014", "2015"), Operator:=xlFilterValues
    End With

    Dim rng As Range, p As Variant, dict As Scripting.Dictionary
    '<== You should add a test here that filter columns contain filter values i.e. there will be visible cells after applying filter
    Set rng = DEsheet.Range("A2:L" & lastrow_DE).SpecialCells(xlCellTypeVisible)
    Set dict = New Scripting.Dictionary

    For Each p In rng.Columns(1).Cells
        If Not dict.Exists(p.Value & "," & p.Offset(, 1)) Then
            dict.Add p.Value & "," & p.Offset(, 1), Application.WorksheetFunction.Sum(p.Offset(, 2).Resize(1, 10))
        Else
            dict(p.Value & "," & p.Offset(, 1)) = dict(p.Value & "," & p.Offset(, 1)) + Application.WorksheetFunction.Sum(p.Offset(, 2).Resize(1, 10))
        End If
    Next p

    Dim key As Variant

    For Each key In dict.Keys
        Debug.Print key & " : " & dict(key)
    Next key

    Sheets.Add

    Dim counter As Long

    With ActiveSheet
        For Each key In dict.Keys
            counter = counter + 1
            .Cells(counter, "A").Resize(1, 2) = Split(key, ",")
            .Cells(counter, "C") = dict(key)

        Next key
    End With

End Sub
选项显式
公共子系统()
昏暗的最后一排
将图纸作为工作表调暗
设置DEsheet=ActiveSheet
lastrow_DE=DEsheet.Cells(DEsheet.Rows.Count,“E”).End(xlUp).Row
带图纸范围(“A1:L”和lastrow\u DE)
.AutoFilter字段:=2,准则1:=数组(“联合”、“最佳”),运算符:=xlFilterValues
.AutoFilter字段:=1,准则1:=Array(“2014”、“2015”),运算符:=xlFilterValues
以
Dim rng作为范围,p作为变量,dict作为脚本。字典
“。。。。。
E列是年份和日期(用作dict键)…C列相当于best和union列。。。G、 H、I、J、L用类似的“年份和日期+类似的C列”对列求和。。。我之所以使用字典,是因为在QHarr添加更多数据时,我可以通过运行VBA将数据添加到一起

A  ....|.. B..| ..C..|..D.                                                                            
 2014  | UNION| 5677 | 4556                  
 2014  | UNION| 5677 | 4556                
 2015  | BEST | 5677 | 4556              
 2015  | BEST | 5677 | 4556
。。。。。
A  ....|.. B..| ..C..|..D.                                                                            
 2014  | UNION| 5677 | 4556                  
 2014  | UNION| 5677 | 4556                
 2015  | BEST | 5677 | 4556              
 2015  | BEST | 5677 | 4556
E列是年份和日期(用作dict键)…C列相当于best和union列。。。G、 H、I、J、L用类似的“年份和日期+类似的C列”对列求和。。。我之所以使用字典,是因为在QHarr添加更多数据时,我可以通过运行VBA将数据添加到一起

A  ....|.. B..| ..C..|..D.                                                                            
 2014  | UNION| 5677 | 4556                  
 2014  | UNION| 5677 | 4556                
 2015  | BEST | 5677 | 4556              
 2015  | BEST | 5677 | 4556


请您显示上述产品的预期产量,好吗?显示的数据似乎与代码不完全匹配,除非有两张表。我看不到带有“UNI”、“IND”的文本字段,您是否有理由希望使用vba而不是公式(如果可以使用公式解决方案)?我找不到
if
语句的起始位置。它们将没有任何字典条目,因为过滤器将不显示任何单元格,并且代码将出错。即使它编译了,它也会这样做,但它不会这样做。这一切都与过滤器有关。错误的筛选器=无值=未填充字典。请看下面的答案作为起点。你能展示一下上面的预期输出吗?显示的数据似乎与代码不完全匹配,除非有两张表。我看不到带有“UNI”、“IND”的文本字段,您是否有理由希望使用vba而不是公式(如果可以使用公式解决方案)?我找不到
if
语句的起始位置。它们将没有任何字典条目,因为过滤器将不显示任何单元格,并且代码将出错。即使它编译了,它也会这样做,但它不会这样做。这一切都与过滤器有关。错误的筛选器=无值=未填充字典。请参阅下面的答案以了解出发点。这篇文章中的列要少得多。只有4列吗?他们是A:D吗?另外,谢谢你整理评论+1感谢您为此付出的努力。@jitjagboro请参阅我的上述评论。问题是。你想让我分别对C列和D列求和,对吗?如果我们说你真的想要C,D,E,F,G列分开求和,那就更复杂了。请给我一个完整的例子,我会在下班后复习。请记住包含预期的结果。此列表中的列要少得多。只有4列吗?他们是A:D吗?另外,谢谢你整理评论+1感谢您为此付出的努力。@jitjagboro请参阅我的上述评论。问题是。你想让我分别对C列和D列求和,对吗?如果我们说你真的想要C,D,E,F,G列分开求和,那就更复杂了。请给我一个完整的例子,我会在下班后复习。请记住包含预期结果。数据已排序。因此,您将不会看到应用排序的区别。是的,当然,我会给你更多的解释:-)明天可以添加解释文档吗?用于排序代码。我不需要复制可见行,因为我之前做了相反的操作。我删除了隐藏的行。因此,表中只剩下感兴趣的行。我这样做是因为可见行可能不是连续的,我需要一个连续的范围来处理。代码创建该工作表,然后将其删除。它是所有实际工作完成的地方。它使用新的工作表和引用原始源工作表的sumifs来计算总数。sumif的条件来自新表的第1列和第4列,即按项目过滤的行,例如qharr和yearMonth(20170301),我明天将写解释。现在,这里有一个文件,您可以单独使用它来了解正在发生的事情,或者至少观看它。在屏幕和Excel工作表的左半部分排列Excel VBE(代码区)
A  ....|.. B..| ..C..|..D.                                                                            
 2014  | UNION| 5677 | 4556                  
 2014  | UNION| 5677 | 4556                
 2015  | BEST | 5677 | 4556              
 2015  | BEST | 5677 | 4556