- 帖子
- 4901
- 主題
- 44
- 精華
- 24
- 積分
- 4916
- 點名
- 155
- 作業系統
- Windows 7
- 軟體版本
- Office 20xx
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台北
- 註冊時間
- 2010-4-30
- 最後登錄
- 2025-6-27
               
|
7#
發表於 2011-4-4 16:50
| 只看該作者
回復 6# jesscc - Sub SourceData_S()
- Dim Ay()
- With Worksheets("資料來源")
- Set Rng = .Range("A3:B3")
- fs = False
- If .Range("B3").Value = "" Then
- MsgBox "無法取得股票名稱,請確定股票名稱已填入B3儲存格", 32, "資料錯誤!"
- Exit Sub
- End If
- For Each sh In Sheets '檢查工作表名稱是否存在
- If sh.Name = .[B3].Text Then fs = True: Exit For
- Next
- If fs = False Then Sheets.Add.Name = .[B3].Text '如果工作表不存在就新增工作表
- ar = Array("A", "C", "I", "P") '需要提取的欄位
- If fs = False Then '如果是新增工作表,就存入標題
- ReDim Preserve Ay(s) '將標題列存入陣列的第一筆並擴大陣列
- Ay(s) = Array(.Cells(4, ar(0)).Value, .Cells(4, ar(1)).Value, .Cells(4, ar(2)).Value, .Cells(4, ar(3)).Value, "成交量占股本比例")
- s = s + 1
- End If
- For i = 5 To .Cells(.Rows.Count, 1).End(xlUp).Row '進入資料迴圈
- If Weekday(.Cells(i, ar(0)), vbMonday) < 5 Then '判斷日期為星期幾,星期5以前執行
- ReDim Preserve Ay(s) '將資料存入陣列
- Ay(s) = Array(.Cells(i, ar(0)).Text, .Cells(i, ar(1)).Value, .Cells(i, ar(2)).Value, .Cells(i, ar(3)).Value, "=RC[-2]*RC[-1]/R1C4")
- s = s + 1
- Else '星期五執行
- ReDim Preserve Ay(s) '將資料存入陣列
- Ay(s) = Array(.Cells(i, ar(0)).Text, .Cells(i, ar(1)).Value, .Cells(i, ar(2)).Value, .Cells(i, ar(3)).Value, "=RC[-2]*RC[-1]/R1C4")
- s = s + 1
- ReDim Preserve Ay(s) '儲存一個空白列到陣列
- Ay(s) = Array("", "", "", "", "")
- s = s + 1
- End If
- Next
- With Sheets(Sheets("資料來源").[B3].Text)
- Rng.Copy .[a1] '股票名稱
- .[C1] = "股本(張)": .[D1].FormulaLocal = "=YES|DQ!'" & .[a1] & ".Capital'*1000"
- With .Range(.[A3], .Cells(.Rows.Count, 6))
- '.ClearContents '清除原來資料
- .Columns(1).NumberFormat = "yyyy/mm/dd" '設定A欄為日期格式
- End With
- .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).Resize(s, 5) = Application.Transpose(Application.Transpose(Ay)) '將陣列值寫入工作表
- .Columns("A:E").AutoFit 'A:E欄自動欄寬
- End With
- End With
- End Sub
複製代碼 |
|