|
回复 9楼 yuch8663 的帖子
Sub SumTest()
Dim account_item_name(4), p(4), i, j, n, S
account_item_name(1) = "現金及約當現金"
account_item_name(2) = "流動資產"
account_item_name(3) = "利息收入"
account_item_name(4) = "投資收入/股利收入"
For j = 1 To 4
For i = 1 To [A65536].End(xlUp).Row / 2
If Cells(i, 1) = account_item_name(j) And Cells(i + 1, 1) = "年" Then p(j) = i 'account_item_name(j)四個Title的起始列
Next i
Next j
Range("A" & p(1) + 1 & ":" & Chr(64 + [A2].End(xlToRight).Column) & p(1) + 1).Copy '複製第2列年度
ActiveSheet.Paste Destination:=Cells(p(1) + 1, [A2].End(xlToRight).Column + 4) '貼到年度後的第四欄
Range("A" & p(1) + 2 & ":A" & p(2) - 3).Copy '複製第1欄股票名稱
ActiveSheet.Paste Destination:=Cells(p(1) + 2, [A2].End(xlToRight).Column + 4) '貼到年度後的第四欄
For j = p(1) + 2 To p(2) - 4 '從"現金及約當現金"下列到"流動資產"前4列
For i = 2 To 10 'B欄到J欄
With Range("A" & p(1) & ":A" & p(4) - 1) '查詢範圍"現金及約當現金"-"投資收入/股利收入"前一列
Set c = .Find(Cells(j, 1), LookIn:=xlValues, Lookat:=xlWhole) '依股票名稱查詢
If Not c Is Nothing Then
firstAdd = c.Address
Do
n = n + 1 '查找account_item_name(1)-account_item_name(3)三個部分裡的股票名稱
If n < 3 Then S = S + Cells(c.Row, i) 'b3+b15
If n = 3 Then
Cells(j, i + 13) = S - Cells(c.Row, i) 'b3+b15-b27
n = 0: S = 0
Exit Do
End If
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAdd
End If
End With
Next i
Next j
End Sub |
|