- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
本帖最後由 GBKEE 於 2014-4-27 15:24 編輯
回復 34# smart3135 - Option Explicit
- Private Sub Test()
- Dim fs As Object, E As Range, C As Variant
- Set fs = CreateObject("Scripting.FileSystemObject")
- Set fs = fs.CreateTextFile("D:\財報資料\1101\Test.TXT", True)
- '創見一個檔案,如檔案存在可覆蓋掉
- '文字檔的寫入是
- For Each E In Sheets(1).UsedRange.Columns
- C = E.Value '-> 陣列(1 To 列數,1 To 欗數)的二維陣列
- C = Application.Transpose(E.Value) '因為第二維只有一欄,轉置一次可變為一維陣列
- '如E In UsedRange.rows
- 'C = E.Value '-> 陣列(1 To 列數,1 To 欗數)的二維陣列
- 'C = Application.Transpose(Application.Transpose(E.Value)) '第二維不只一欄,所以轉置二次才可變為一維陣列
- C = Join(C, vbTab)
- 'Join 函數 傳回一個字串,該字串是透過連結某個陣列中的多個子字串而建立的。
- 'Join 函數 使用的陣列必須是一維陣列
- fs.WriteLine C
- Next
- fs.Close
- End Sub
複製代碼
詳看註解可明瞭.-
- Option Explicit
- Sub 抓季月營收資料()
- Dim E As Integer, URL As String, xPath As String, xFile As String
- Dim i As Integer, ii As Integer, Rng As Range, S1 As String, S2 As String, t As Date
- Dim AR()
- t = Time
- URL = "URL;https://djinfo.cathaysec.com.tw/Z/ZC/ZCH/ZCH.DJHTM?A="
- xPath = "D:\財報資料"
- Application.DisplayStatusBar = True
- With ThisWorkbook
- '兩者取一即可 (選擇儲存格較方便)
- '***'不需要的股票如4203天仁(後抓取)是錯誤的股票號碼 置入-> Ar陣列
- ' AR = Array(1202, 1420, 1433, 1502, 1518, 1610, 1716, 2346, 2372, 2391, 2513, 2526, 2541, 2802, 2803, 2804, 2806, _
- 2813, 2814, 2815, 2817, 2818, 2819, 2821, 2826, 2830, 2839, 2840, 2843, 2844, 2848, 2907, 2909, 4101, 4112, _
- 4175, 4201, 4203, 4204, 4301, 4302, 4405, 4407, 4409, 4410, 4411, 4412, 4504, 4505, 4507, 4508, 4509, 4512, _
- 4514, 4516, 4517, 4519, 4520, 4521, 4524, 4525, 4531, 4603, 4604, 4605, 4606, 4607, 4608, 4701, 4704, 4705, _
- 4708, 4709, 4710, 4713, 4715, 4718, 4901, 4902, 5001, 5003, 5004, 5005, 5012, 5101, 5311, 5319, 5320, 5322, _
- 5323, 5327, 5330, 5331, 5334, 5335, 5337, 5341, 5342, 5354, 5357, 5358, 5359, 5360, 5361, 5362, 5363, 5366, _
- 5368, 5369, 5374, 5377, 5379, 5380, 5382, 5389, 5391, 5393, 5394, 5396, 5397, 5399, 5404, 5405, 5408, 5409, _
- 5411, 5412, 5415, 5416, 5417, 5418, 5419, 5420, 5421, 5422, 5423, 5424, 5427, 5428, 5430, 5431, 5433, 5435, _
- 5440, 5444, 5445, 5446, 5447, 5449, 5453, 5456, 5458, 5459, 5461, 5462, 5463, 5470, 5472, 5476, 5477, 5482, _
- 5485, 5486, 5495, 5496, 5499, 5509, 5517, 5527, 5606, 5705, 5804, 5805, 5806, 5807, 5809, 5812, 5814, 5815, _
- 5854, 6003, 6006, 6019, 6102, 6106, 6401, 6501, 8001, 8003, 8903, 8904, 8912, 8914, 8915, 8918, 8920, 8922, 8939, 9105, 9909)
-
- '***A欄中不需要的股票 如4203天仁(後抓取)號碼的儲存格 置入-> Ar陣列
- Set Rng = .Sheets(2).Range("A:A").SpecialCells(xlCellTypeConstants)
- If Rng Is Nothing Then
- AR = Array()
- ElseIf Rng.Count = 1 Then
- AR = Array(Rng.Value)
- Else
- AR = Application.Transpose(Rng.Value)
-
- '先前出錯是這裡寫錯 AR = Application.Transpose(Application.Transpose(Rng.Value))
- 'Rng.Value=>單欄二維陣列
- End If
- '***************************************************
- .Sheets(2).UsedRange.Offset(, 1).Clear
- Application.ScreenUpdating = False
- Application.StatusBar = " "
- With .Sheets(1) '活頁簿的第 1 張工作表
- If .QueryTables.Count = 0 Then
- With .QueryTables.Add(Connection:=URL, Destination:=.Range("$A$1"))
- .Refresh BackgroundQuery:=False
- End With
- End If
- .Rows(1).Delete
- .Columns(1).Delete
- For E = 1101 To 5000
- If UBound(Filter(AR, E)) > -1 Then GoTo xLnext
- 'Filter(AR, E)函數 等同使用Find方法 如 Set Rng = .Find(S2, lookat:=xlPart) '搜尋:股票名稱
- With .QueryTables(1)
- .Connection = URL & E
- .PreserveFormatting = True
- .BackgroundQuery = True
- .RefreshStyle = xlInsertDeleteCells
- .SaveData = True
- .AdjustColumnWidth = True
- .RefreshPeriod = 0
- .WebSelectionType = xlSpecifiedTables
- .WebFormatting = xlWebFormattingNone
- .WebTables = "3"
- .WebPreFormattedTextToColumns = True
- .WebConsecutiveDelimitersAsOne = True
- .Refresh BackgroundQuery:=False
- If InStr(.ResultRange(2, 1), "查無") Then '查無這檔股票
- Mark_Code E '查無的股票 放置A攔
- GoTo xLnext
- End If
- S1 = .ResultRange(1) '匯入資料的第1個儲存格
- S2 = Mid(S1, 1, InStr(S1, "(") - 1) '股票名稱
- End With
-
- With ThisWorkbook.Sheets(2).Range("B:B")
- Set Rng = .Find(S2, lookat:=xlPart) '搜尋:股票名稱
- If Rng Is Nothing Then
- i = i + 1
- .Range("A" & i) = S1 '股票名稱代碼
- Else
- Mark_Code E '重複的股票 放置A攔
- S2 = Mid(Trim(Rng), InStr(Trim(Rng), "(") + 1)
- S2 = Mid(S2, 1, Len(S2) - 1) '舊的股票[代碼]
- xFile = xPath & "\" & S2 & "\*.*" '殺掉所有檔案
- If Dir(xFile) <> "" Then
- ii = ii - 1
- Kill xFile
- xFile = xPath & "\" & S2
- If Dir(xFile, vbDirectory) <> "" Then RmDir xFile '資料夾也刪除了
- End If
- End If
- End With
- ii = ii + 1
- xFile = xPath & "\" & E & "\REVENUE.txt"
- MkDir_Sub xFile
- Maketxt xFile, .QueryTables(1)
- xLnext:
- S1 = " " & Sheets(1).QueryTables(1).ResultRange(1)
- If Val(S1) < 0 Then S1 = " 查無"
- Application.StatusBar = Application.text(Time - t, ["MM分SS秒"]) & " 共匯入 " & ii & " 文字檔, 讀取 " & S1 & " 中..."
- Next
- End With
- End With
- Application.ScreenUpdating = True
- Application.StatusBar = Application.text(Time - t, ["MM分SS秒"]) & " 共匯入 " & ii & " 文字檔, 讀取完畢 !! "
- MsgBox "匯入 文字檔" & ii & " 費時 " & Application.text(Time - t, ["MM分SS秒"])
- ThisWorkbook.Save
- End Sub
複製代碼- Private Sub Mark_Code(S As Integer)
- With ThisWorkbook.Sheets(2).Range("A:A") '重複的股票 放置A攔
- .Cells(Application.Count(.Cells) + 1, "A") = S
- '股票代號放置 A攔最後一個儲存格
- '執行一次後,下次再執行 [抓季月營收資料]程式時,For 迴圈中可踢除這股票代號
- End With
- End Sub
複製代碼
|
|