- 帖子
- 96
- 主題
- 18
- 精華
- 0
- 積分
- 125
- 點名
- 0
- 作業系統
- Windows 7
- 軟體版本
- Office 2007
- 閱讀權限
- 20
- 性別
- 男
- 註冊時間
- 2014-3-23
- 最後登錄
- 2022-8-2
|
75#
發表於 2014-5-26 09:50
| 只看該作者
回復 73# GBKEE
版主,不好意思,上一篇回覆其中一個檔案有錯誤,我再重新附上
另外我花了一些時間做出了另一個版本的程式,是利用儲存格輸入日期當作迴圈,已測試可以抓資料,但有些小問題:
1.有些語法我不太懂怎麼簡化,所以可能寫的比較複雜一點
2.利用儲存格當日期迴圈的缺點,就是每個月都要更新儲存格中的日期
3.我自己寫的程式碼執行到代碼1340時還是會出錯,不過如果把第一個代碼重新設成1340開始抓資料又正常
4.因為表頭的文字會重覆抓取,但我只需要一次,所以用一列一列刪除的笨方法
這個程式寫的比較粗糙,抓資料的速度似乎也比較慢,不過確實可以達到我需要的結果,除了遇到某些代碼會卡住需要重新設定外
其他都還OK,再請您幫忙看一下是否有錯誤的地方需要修正,謝謝!
集保新的.zip (42.48 KB)
- Option Explicit
- Sub 集保完成()
- Dim E As Range, X As Range, URL As String, xPath As String, xFile As String, rng As Range, rng1 As Range
- Dim Msg As Boolean, I As Integer, t As Date, S As String, BB As String, CC As String, rng2 As Range
- t = Time
- URL = "URL;http://www.tdcc.com.tw/smWeb/QryStock.jsp?SCA_DATE="
- BB = "&SqlMethod=StockNo&StockNo="
- CC = "&sub=%ACd%B8%DF"
- xPath = "D:\財報資料"
- With ThisWorkbook
- With .Sheets(3)
- Set rng = .Range("A1", .Range("A1").End(xlDown))
- Set rng1 = .Range("B1", .Range("B1").End(xlDown))
- End With
- ' .Sheets(3).Activate '兩種寫法都可以 不過第一種比較簡化 所以第二種跳過
- ' .Sheets(3).Range("a1").Select
- ' Range(Selection, Selection.End(xlDown)).Select
- ' Set rng = Selection
- '' Set rng = .Sheets(3).Range("A:A") '這裡這樣設定會變成無窮迴圈
- ' .Sheets(1).Activate
- 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
-
- For Each E In rng
- With ThisWorkbook
- .Sheets(2).Cells.Clear
- ' .Activate
- .Sheets(1).Cells.Clear '下載資料置於此工作表,變換股票時:清空
- End With
- For Each X In rng1
- With .QueryTables(1)
- .Connection = URL & X & BB & E & CC
- .PreserveFormatting = True
- .BackgroundQuery = True
- .RefreshStyle = xlInsertDeleteCells
- .SaveData = True
- .AdjustColumnWidth = True
- .RefreshPeriod = 0
- .WebSelectionType = xlSpecifiedTables
- .WebFormatting = xlWebFormattingNone
- .WebTables = "6,7,8"
- On Error GoTo xlnext
- .WebPreFormattedTextToColumns = True
- .WebConsecutiveDelimitersAsOne = True
- .Refresh BackgroundQuery:=False
- End With
- Set rng2 = Sheets(1).UsedRange
- If Sheets(2).Range("a1") = "" Then
- rng2.Copy Sheets(2).Range("a" & .Rows.Count).End(xlUp)
- Else
- rng2.Copy Sheets(2).Range("a" & .Rows.Count).End(xlUp).Offset(2, 0)
- End If
- Next X
- xlnext:
- Sheets(2).Range("2:2,22:22,43:43,64:64,85:85,106:106,127:127,148:148,169:169,190:190,211:211,232:232,253:253").Delete
- xFile = xPath & "\" & E & "\SHD.txt"
- MkDir_Sub xFile '10#的程式 'C槽下的季損益表資料夾不需先建立
- Maketxt xFile, Sheets(2).UsedRange
- ' S = " " & Sheets(1).QueryTables(1).ResultRange(1)
- ' If Val(S) < 0 Then S = " 查無"
- I = I + 1
- Application.StatusBar = Application.Text(Time - t, ["MM分SS秒"]) & " " & E & "匯入" & I & "個文字檔"
- Msg = False
- Next E
- End With
- End With
- MsgBox "共匯入 文字檔" & I & " 費時 " & Application.Text(Time - t, ["MM分SS秒"])
- End Sub
- Sub MkDir_Sub(S As String)
- Dim AR, I As Integer, xPath As String
- If Dir(S) = "" Then
- AR = Split(S, "\")
- xPath = AR(0)
- For I = 1 To UBound(AR) - 1
- xPath = xPath & "\" & AR(I)
- If Dir(xPath, vbDirectory) = "" Then MkDir xPath
- Next
- End If
- End Sub
- Sub Maketxt(xF As String, Q As Range) '將匯入資料存入指定的txt
- Dim fs As Object, E As Range, C As Variant
- Set fs = CreateObject("Scripting.FileSystemObject")
- Set fs = fs.CreateTextFile(xF, True) '創見一個檔案,如檔案存在可覆蓋掉
- For Each E In Q.Rows
- C = Application.Transpose(Application.Transpose(E.Value))
- C = Join(C, vbTab)
- fs.WriteLine C
- Next
- fs.Close
- End Sub
複製代碼 |
|