標題:
請問如何將"股票交易明細"彙整貼入"部位表"
[打印本頁]
作者:
jasonwu0114
時間:
2013-11-13 11:06
標題:
請問如何將"股票交易明細"彙整貼入"部位表"
請問如何將
”交易明細”表中的明細
依各股加總”數量”及總”金額”
再依個股買/賣
貼入“”部位表””中的買入/賣出股數,金額
如無庫存則依代碼順序新插入一列
貼入代號,名稱,買入/賣出股數,金額
卡住好幾天了
謝謝幫忙
作者:
jasonwu0114
時間:
2013-11-21 10:45
回復
1#
jasonwu0114
有高手可以指導一下嗎
謝謝
作者:
GBKEE
時間:
2013-11-21 15:25
回復
2#
jasonwu0114
試試看
Option Explicit
Sub Ex()
Dim D(1 To 2) As Object, X As Integer, Rng As Range, Ar As Variant, S As String
Set D(1) = CreateObject("SCRIPTING.DICTIONARY") '字典物件1
Set D(2) = CreateObject("SCRIPTING.DICTIONARY") '字典物件2
Set Rng = Sheets("交易明細").Range("B4")
Do While Rng <> "" '迴圈: 讀取 (買,賣) 資料
With Rng
If Rng = "買" Then X = 1 Else X = 2 '字典物件 買(1),賣 (2)
If Not D(X).Exists(.Offset(, 1).Value) Then 'Exists 字典物件(KEY)是否存在 : 不存在
D(X)(.Offset(, 1).Value) = Array(Val(.Offset(, 2)), Val(.Offset(, 2)) * .Offset(, 3).Value) '字典物件(KEY)=陣列
Else '字典物件: 存在
Ar = D(X)(.Offset(, 1).Value) '陣列=字典物件(KEY)
Ar(0) = Ar(0) + Val(.Offset(, 2)) '陣列(0)=Ar(0)+數字
Ar(1) = Ar(1) + Val(.Offset(, 2)) * .Offset(, 3) '陣列(1)=Ar(1)+數字
D(X)(.Offset(, 1).Value) = Ar '字典物件(KEY)=陣列
End If
End With
Set Rng = Rng.Offset(1) '下一個買,賣資料
Loop
Set Rng = Sheets("部位表").Range("A5")
Do While Rng <> "" '迴圈: 讀取庫存(股票代號)
With Rng
S = .Offset(, 1) & " " & Rng 'S : 字典物件的(KEY)
If D(1).Exists(S) Then 'Exists= True: 字典物件(S)->存在
.Range("E1") = D(1)(S)(0) 'D(1)(S)(0): 字典物件(S)內容為陣列->第(0)元素值
.Range("F1") = D(1)(S)(1)
D(1).Remove (S) 'Remove: 把成員,從字典物件(1)移除
End If
If D(2).Exists(S) Then
.Range("G1") = D(2)(S)(0)
.Range("I1") = D(2)(S)(1)
D(2).Remove (S)
End If
End With
Set Rng = Rng.Offset(1)
Loop
'*** 無庫存則依代碼順序新插入一列 *********
For Each Ar In D(1).KEYS 'Remove後剩餘的字典物件
Rng.Resize(, 12).Insert
'Rng.Insert 插入後,Rng會下移在插入的儲存格下方,繼續插入,Rng會下移在插入的儲存格下方
With Rng.Offset(-1)
.Range("A1") = Split(Ar, " ")(1)
.Range("B1") = Split(Ar, " ")(0)
.Range("E1") = D(1)(Ar)(0)
.Range("F1") = D(1)(Ar)(1)
If D(2).Exists(Ar) Then
.Range("G1") = D(2)(Ar)(0)
.Range("I1") = D(2)(Ar)(1)
D(2).Remove (Ar)
End If
End With
Next
For Each Ar In D(2).KEYS
Rng.Resize(, 12).Insert
With Rng.Offset(-1)
.Range("A1") = Split(Ar, " ")(1)
.Range("B1") = Split(Ar, " ")(0)
.Range("G1") = D(2)(Ar)(0)
.Range("I1") = D(2)(Ar)(1)
End With
Next
End Sub
複製代碼
作者:
jasonwu0114
時間:
2013-11-27 15:13
回復
3#
GBKEE
感恩!!!!真是受益良多!!!我想我把書翻爛了也寫不出來
另外再請問
程式中“””無庫存則依代碼順序新插入一列””
執行後好像只會在最後往下新增一列
1.是否可照所有庫存的股票代號順序插入一列或者最後再全部做排序
2.因為儲存格內有計算公式是否可複製整列再新插入
謝謝
作者:
GBKEE
時間:
2013-11-27 16:04
本帖最後由 GBKEE 於 2013-11-27 17:45 編輯
回復
4#
jasonwu0114
Option Explicit
Sub Ex()
Dim D(1 To 2) As Object, X As Integer, Rng As Range, Ar As Variant, S As String
Set D(1) = CreateObject("SCRIPTING.DICTIONARY") '字典物件1
Set D(2) = CreateObject("SCRIPTING.DICTIONARY") '字典物件2
Set Rng = Sheets("交易明細").Range("B4")
Do While Rng <> "" '迴圈: 讀取 (買,賣) 資料
With Rng
If Rng = "買" Then X = 1 Else X = 2 '字典物件 買(1),賣 (2)
If Not D(X).Exists(.Offset(, 1).Value) Then 'Exists 字典物件(KEY)是否存在 : 不存在
D(X)(.Offset(, 1).Value) = Array(Val(.Offset(, 2)), Val(.Offset(, 2)) * .Offset(, 3).Value) '字典物件(KEY)=陣列
Else '字典物件: 存在
Ar = D(X)(.Offset(, 1).Value) '陣列=字典物件(KEY)
Ar(0) = Ar(0) + Val(.Offset(, 2)) '陣列(0)=Ar(0)+數字
Ar(1) = Ar(1) + Val(.Offset(, 2)) * .Offset(, 3) '陣列(1)=Ar(1)+數字
D(X)(.Offset(, 1).Value) = Ar '字典物件(KEY)=陣列
End If
End With
Set Rng = Rng.Offset(1) '下一個買,賣資料
Loop
Set Rng = Sheets("部位表").Range("A5")
Do While Rng <> "" '迴圈: 讀取庫存(股票代號)
With Rng
S = .Offset(, 1) & " " & Rng 'S : 字典物件的(KEY)
If D(1).Exists(S) Then 'Exists= True: 字典物件(S)->存在
.Range("E1") = D(1)(S)(0) 'D(1)(S)(0): 字典物件(S)內容為陣列->第(0)元素值
.Range("F1") = D(1)(S)(1)
D(1).Remove (S) 'Remove: 把成員,從字典物件(1)移除
End If
If D(2).Exists(S) Then
.Range("G1") = D(2)(S)(0)
.Range("I1") = D(2)(S)(1)
D(2).Remove (S)
End If
End With
Set Rng = Rng.Offset(1)
Loop
Set Rng = Rng.Offset(-1).Resize(, 12)
'*** 無庫存則依代碼順序新插入一列 *********
For Each Ar In D(1).KEYS 'Remove後剩餘的字典物件
Rng.Copy '複製
Rng.Offset(1).Insert Shift:=xlDown '下一列插上複製的儲存格
With Rng.Offset(1)
.SpecialCells(xlCellTypeConstants, 3) = "" '清除 下一列插上複製的儲存格的[文字,數字]
.Range("A1") = Split(Ar, " ")(1)
.Range("B1") = Split(Ar, " ")(0)
.Range("E1") = D(1)(Ar)(0)
.Range("F1") = D(1)(Ar)(1)
If D(2).Exists(Ar) Then
.Range("G1") = D(2)(Ar)(0)
.Range("I1") = D(2)(Ar)(1)
D(2).Remove (Ar)
End If
End With
Set Rng = Rng.Offset(1)
Next
For Each Ar In D(2).KEYS
Rng.Copy
Rng.Offset(1).Insert Shift:=xlDown
With Rng.Offset(1)
.SpecialCells(xlCellTypeConstants, 3) = ""
.Range("A1") = Split(Ar, " ")(1)
.Range("B1") = Split(Ar, " ")(0)
.Range("G1") = D(2)(Ar)(0)
.Range("I1") = D(2)(Ar)(1)
End With
Set Rng = Rng.Offset(1)
Next
Set Rng = Rng.Offset(-1).CurrentRegion 'CurrentRegion:延伸的範圍
'********* 排序 :主鍵股票代號
Rng.Sort Key1:=Rng.Cells(1), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _
:=xlStroke, DataOption1:=xlSortNormal
End Sub
End Sub
複製代碼
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)