excel - 根据年份和唯一代码对多个数组的值进行排序
问题描述
我有一个包含大量数据的 excel 文档。我使用了四列。
- 财政年度
- 无效对冲的收益/损失
- SIC 代码。
- 已排序的唯一 SIC 代码,因此不会出现多次。
我创建了 4 个数组并将值存储在其中。
我想做的,但现在有一些问题,是每年(从 2001 年到 2018 年)并计算每个 SIC 代码的平均收益/损失(2),每年。我写了以下代码:
Sub Assignment4()
Dim Wb As Workbook
Dim Ws As Worksheet
Set Wb = ThisWorkbook
Set Ws = ThisWorkbook.Worksheets("Sheet1")
Dim iLastRow As Integer, iLastRow2 As Integer
Dim readStart As Integer
Dim count As Integer
Dim ArrayFiscalYear() As String
Dim ArrayGL() As String
Dim ArraySIC() As Variant
Dim ArraySICSorted() As Integer
ReDim ArrayFiscalYear(10000)
ReDim ArrayGL(10000)
ReDim ArraySIC(10000)
ReDim ArraySICSorted(10000)
Dim year As Integer
Dim arr As New Collection, a
Dim j As Long
Dim x As Long
Dim avg As Double
Dim sum As Double
Dim counter As Integer
iLastRow = Ws.Cells(Rows.count, "J").End(xlUp).Row
count = 2
readStart = 2
Do While (count <= iLastRow)
ArraySIC(count) = Ws.Cells(count, 10)
count = count + 1
Loop
Ws.Range("J2:J" & iLastRow).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=Ws.Range("L1"), _
Unique:=True
Ws.Cells(1, 12) = "Sorted SIC"
iLastRow2 = Ws.Cells(Rows.count, "L").End(xlUp).Row
Debug.Print (iLastRow2)
count = 0
counter = 2
Do While (count <= iLastRow2)
ArraySICSorted(count) = Ws.Cells(counter, 12)
Debug.Print (ArraySICSorted(count))
count = count + 1
counter = counter + 1
Loop
Ws.Columns(12).EntireColumn.Delete
i = 2
year = 2001
count = 0
x = 1
Do While year <= 2018
For x = LBound(ArraySICSorted) To UBound(ArraySICSorted)
For i = 1 To iLastRow
If Ws.Cells(i, 3) = year Then
ArrayFiscalYear(year) = Ws.Cells(i, 3)
ArrayGL(year) = Ws.Cells(i, 8)
ArraySIC(year) = Ws.Cells(i, 10)
count = count + 1
sum = sum + ArrayGL(year)
End If
Next i
avg = sum / count
Debug.Print (count & " --- " & ArraySICSorted(x) & " --- " & year & " --- " & x)
sum = 0
count = 0
If x >= 62 Then
year = year + 1
Exit For
End If
Next x
Loop
End Sub
我附上了我的 excel 文档的屏幕截图
我希望有人能告诉我我做错了什么,以便我可以从中吸取教训。
解决方案
推荐阅读
- python - 我正在尝试在以下代码段中使用 argpase 模块
- mongodb - 当我们加载应用程序和 F4 Popup 并单击 Next 按钮时,我们会从 Mongodb Server 得到延迟响应
- google-chrome - Chrome 版本 90 - 输入类型时间
- java - 如何在 SpringBoot 中连接类和对象
- flutter - Flutter Hive - 管理已保存数据的最佳方式
- python - Python - 列表中的字符串到numpy数组
- node.js - 将参数传递给 npm 脚本
- r - ggplot中的x标签按升序排列
- hubspot - Hubspot 自定义模块 - 可以附加到 JS onClick 上的 hubspot 数组吗?
- php - 处理像 SQL Query 这样的 php 数组,这可能吗?