- 帖子
- 4901
- 主題
- 44
- 精華
- 24
- 積分
- 4916
- 點名
- 142
- 作業系統
- Windows 7
- 軟體版本
- Office 20xx
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台北
- 註冊時間
- 2010-4-30
- 最後登錄
- 2026-7-16
                
|
回復 3# 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") '需要提取的欄位
- 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
- 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)
- 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)
- s = s + 1
- ReDim Preserve Ay(s) '儲存一個空白列到陣列
- Ay(s) = Array("", "", "", "")
- s = s + 1
- End If
- Next
- With Sheets(Sheets("資料來源").[B3].Text)
- Rng.Copy .[A1] '股票名稱
- With .Range(.[A3], .Cells(.Rows.Count, 6))
- .ClearContents '清除原來資料
- .Columns(1).NumberFormat = "yyyy/mm/dd" '設定A欄為日期格式
- End With
- .[A2].Resize(s, 4) = Application.Transpose(Application.Transpose(Ay)) '將陣列值寫入工作表
- .Columns("A").AutoFit 'A欄自動欄寬
- End With
- End With
- End Sub
複製代碼 |
|