- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
你好,
我有一个表格,其中包含每个部门每周的销售额,格式如下:
Week1 Week2 Week3 ...
Dept1 10 20 10
Dept1 20 10 30
Dept1 30 30 20
Dept2 20 20 30
Dept2 20 20 10
Dept3 50 40 60
...
我需要做的是创建一个较小的报表来汇总每个部门的销售额。按照以下模板:
Week1 Week2 Week3
Dept1 60 60 60
Dept2 40 40 40
Dept3 50 40 60
Total 150 140 160
每个部门的行数各不相同。然后应该将此报告打印在电子表格上。
据我了解,这可以使用字典或集合来完成。到目前为止,我已经设法计算出每周的总和,但是,我不明白如何将这些结果转移到工作表中。我试过将总和转移到数组,但没有成功。
这是我目前的代码。它会正确计算每周的总和,然后清空集合并在下一周再次计算。所以,我的主要问题是如何将这些结果写入工作表。
Dim collection As collection
Dim dataitems As Itemlist 'defined in classmodule
Dim key As String
Dim item As Double
Dim row As Long, column As Long
Dim lstrow As Long, lstcolumn As Long
Set collection = New collection
columnindex = 3 'that is the column where name of departments appear
lstrow = Sheet1.Cells(Sheet1.Rows.Count, column).End(xlUp).row
lstcolumn = Sheet1.Cells(1, Sheet1.Columns.Count).End(xlToLeft).column
For column = 5 To lstcolumn 'column 5 is where the weekly data start
For row = 2 To lstrow 'first 1 contains titles
key = CStr(Sheet1.Cells(row, "C").Value2)
item = CDbl(Sheet1.Cells(row, column).Value2)
Set dataitems = Nothing: On Error Resume Next
Set dataitems = collection(key): On Error GoTo 0
If dataitems Is Nothing Then
Set dataitems = New Itemlist
dataitems.key = key
collection.Add dataitems, key
End If
With dataitems
.Sum = .Sum + item
.Itemlist.Add item
End With
Next
Set collection = New collection
Next
感谢任何帮助。谢谢。
最佳答案
您可能有一个有效的代码,但我想向您展示一种不同的方法来实现您的目标。
此方法包含 3 个部分。
1-Control your unique keys(dept names) in a dictionary as keys.
2-Your weekly sums to be stored in an array, as values of your dictionary.
3-Sum your unique dept names using
Application.SumIf
in a single line.
你的字典的最终结果是这样的(我用你的模板来演示和比较):
dict = {key1:value1,key2:value2,key3:value3)
For example:
dict = {"Dept1":(60,60,60),"Dept2":(40,40,40),"Dept3":(50,40,60)}
如您所见,值是数组,其中包含部门名称的每周总和。
但是,这些数组并不是为每个部门名称声明的。它们实际上是另一个数组内部的数组,就像这样:
arr1 = (arr1_1(),arr1_2(),arr1_3())
For example:
arr1 = ((60,60,60),(40,40,40),(50,40,60))
现在如果要获取dept3的每周总计,基本上就是
arr1(2) which is (50,40,60)
如果要获取dept3第二周的总数,就是
arr1(2)(1) which is 40
我希望你明白了。在我们开始之前还有一件事,你在你的代码中评论了:
'that is the column where name of departments appear
'column 5 is where the weekly data start
'first 1 contains titles
所以我也这样做了,这是代码:
Sub ArrayMyDictionary()
Dim dict As Object, lastrow As Long, lastcol As Long, i As Long, j As Long, c As Long
Dim arr1() As Variant, arr2() As Variant
Set dict = CreateObject("Scripting.Dictionary")
With Worksheets("Sheet1")
lastrow = .Cells(.Rows.Count, 3).End(xlUp).Row
lastcol = .Cells(1, .Columns.Count).End(xlToLeft).Column
ReDim arr1(c) 'array1 initial size 0, later on size is number dept
ReDim arr2(lastcol - 5) 'array2 size is number of weeks
For i = 2 To lastrow
If Not dict.Exists(.Cells(i, 3).Value) Then 'check if Dept not exists in dict
ReDim Preserve arr1(c)
arr1(c) = arr2() ' create empty array2 (size is number of weeks) as an element of current array1
For j = 5 To lastcol
arr1(c)(j - 5) = Application.SumIf(.Range(.Cells(2, 3), .Cells(lastrow, 3)), .Cells(i, 3).Value, .Range(.Cells(2, j), .Cells(lastrow, j)))
Next
dict(.Cells(i, 3).Value) = arr1(c) ' create key (Dept name) and value (an array that holds relevant weekly sums)
c = c + 1
End If
Next
End With
'this part will print out your results to Sheet2
With Worksheets("Sheet2")
Dim key As Variant
For Each key In dict.Keys
.Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0) = key 'last empty row - print key
For j = 0 To lastcol - 5
.Cells(.Rows.Count, 1).End(xlUp).Offset(0, j + 1) = dict(key)(j) 'same row proceed to cell on right - print each element in array inside value
Next j
Next key
.Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0) = "Total" 'last row - calculate totals
For j = 0 To lastcol - 5
.Cells(.Rows.Count, 1).End(xlUp).Offset(0, j + 1) = Application.WorksheetFunction.Sum(.Columns(j + 2)) 'same row proceed to cell on right - sum of columns
Next j
End With
End Sub
关于arrays - 从数据表/字典到工作表的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43986792/
我最近一直在学习 Clojure。 Clojure 世界中是否有类似 Scala 的工作表这样的东西,我可以在其中放入任何代码并在保存后立即对其进行评估?或者也许 Clojure 有类似的解决方案?
有人可以帮我吗?我想知道如何过滤工作表中的多个选项卡(C1-C19)。这是我所做的: 我创建了一张表格,将所有回复存储在我的谷歌表单(事件注册表单)中。每个参与者将收到一个坦克编号,每个坦克编号根据其
这就是我将打开的面板显示为 float 窗口的方式。 有人可以帮我将面板作为工作表运行吗?窗口对象是mWindow。我使用的许多标准代码都已被折旧。 NSOpenPanel *openPanel =
当您仅键入 worksheets() 时,默认范围 ActiveWorkbook 或 ThisWorkbook 是什么?对于那些不了解这些区别的人来说,它们非常重要,尤其是在 Excel 2013 中
我有一个带有一些图表的 HTML 页面。我想要做的是编写一个加载 javascript 函数,它将从 excel 表中读取值,将它们存储在变量中并在 html 页面上使用它们。我的问题是是否有任何 j
我需要将参数 callFrom 传递给 SwiftUI 中的工作表。 奇怪的是,该参数在第一次调用时没有使用,但对以下调用有效。 import SwiftUI struct ContentView:
我试着 var tempSheet = wrksheets[sheetName] as Worksheet; 在哪里 wrksheets是类型表 sheetName 是“带空格的工作表名称” 如果
该函数用作“ Assets 类别分配”引擎(在参数范围内具有约束)并在数组的每一行上模拟投资组合模型。我尝试使用四种方法将数组发布到工作表上,但每种方法都失败了。 对于 Assets A、B、C、D
目前,我的 excel 文件有两张表,一张名为“English”,一张名为“French”。 我以编程方式打开我的工作簿并编辑我的英文表,没有任何问题。当我打开第二张工作表时,出现以下错误: The
我添加了一个 VBA 表单 userform和一个模块 Module1在 Excel 中打开 Microsoft VBA 编辑器 (Alt+F11)。 现在,每当我打开任何其他 Excel 时,按 A
在单个 Excel 工作簿中,我想选择各种工作表来运行 VBA 子例程。我找到了显示如何遍历选定工作表的代码,它使用“MsgBox sh.Name”;但是,当我将代码放入其中时,它只会影响选择的最后一
我想知道是否有一个函数可以在 Excel 中加载特定于 Python 的工作表,例如,如果我有 34 张工作表只加载前 25 张工作表。通过以下行,我加载了所有工作表。 xlsx=pd.ExcelFi
我有一个名为“A”、“B”、“C”等的工作表的 xlsx。我需要形成一个名称为“A”、“B”、“C”的表作为第一列,以及来自的一些数据每个工作表中与第二列相同的单元格。例如,这可能看起来像: S
我有一张用密码保护的工作表。当我使用 VBA 更改该表上的任何内容时,我会像这样取消保护: Private Sub Worksheet_Change(ByVal target As Range)
我想将 Excel 文档插入 Excel 工作表。我可以通过以下步骤手动执行此操作; 插入/文本/对象/从文件创建(勾选显示为图标)/浏览。 然后我选择文件并插入文档。 我想通过宏来做到这一点。 (录
是否可以创建 批处理文件那将执行以下操作? 重命名 Excel 文件中的单个工作表(不是 Excel 工作簿/文件本身) 将简单格式应用于 Excel 文件 - 例如,将字体和字体大小应用于整个工作簿
Private Sub CommandButton1_Click() Dim ws As Worksheet With Application.FileDialog(msoFileDialog
我想知道是否可以在不复制该工作表的情况下引用另一本工作簿中的 Excel 工作表? 情况:我有一些非常大的工作表,其中充满了各种数据,但我不想在我的工作簿中保留它们的副本,因为虽然每个工作簿都使用相同
我有这个 Python 字典,我想将这个数据写入 Excel 文件。 注意:有很多类别,每个类别有很多汽车(为简单起见,我使用了 2 个类别) data = {"Category": {"Diesel
我有一个 excel 工作簿,在工作簿中我有 2 张名为 Front Page 和 Drafting 的工作表。起草工作表引用了首页工作表中的一些值。这只是一个基本的引用 我有像这样的公式:='Fro
我是一名优秀的程序员,十分优秀!