因為想要製作一個可以自動更新股票的巨集
而在網上找了很多教學
Sub 網路資料抓取()
Application.ScreenUpdating = False
Sheets("收盤價網路").Select
Range("A4").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.ClearContents
'1210 大成
With ActiveSheet.QueryTables.Add(Connection:="URL;http://traderoom.cnyes.com/tse/quote2FB.aspx?code=1210", Destination:= _
Range("$A$5"))
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "4"
.Refresh BackgroundQuery:=False
End With
Range("A1").Select
Application.ScreenUpdating = True
MsgBox "更新完畢!", vbOKOnly, "通知"
End Sub
Sub 收盤價網路()
Application ScreenUpdating = False
Sheets("清單").Select
V = Range("A2").End(xlDown).Row
Dim a()
Dim b()
Dim c()
Dim d()
Dim e()
ReDim a(V - 1)
ReDim b(V - 1)
ReDim c(V - 1)
ReDim d(V - 1)
ReDim e(V - 1)
Sheets("收盤價網路").Select
For i = 0 To V - 1
a(i) = Cells(i * 8 + 9, 2)
b(i) = Cells(i * 8 + 7, 4)
c(i) = Cells(i * 8 + 8, 4)
d(i) = Cells(i * 8 + 9, 4)
e(i) = Cells(i * 8 + 5, 2)
Next
ChDir "C:\Users\user\Desktop\上市公司股票"
Workbooks.Open Filename:="C:\Users\user\Desktop\上市公司股票"
Sheets("清單").Select
For i = 2 To Range("A1").End(xlDown).Row
Sheets("清單").Select
k = Cells(i, 2)
If k = "" Then Exit For
Windows("Focus.xlsm").Activate
Sheets(k).Select
h = Range("B3").End(xlDown).Row
Cells(h + 1, "B") = a(i - 2)
Cells(h + 1, "C") = b(i - 2)
Cells(h + 1, "D") = c(i - 2)
Cells(h + 1, "E") = d(i - 2)
Cells(h + 1, "F") = e(i - 2)
Next
Windows("網路資料抓取.xlsm").Activate
Sheets("收盤價網路").Select
Application.CutCopyMode = False
Workbooks("網路資料抓取.xlsm").Save
Workboos("網路資料抓取.xlsm").Close
Application.ScreenUpdating = Ture
MsgBox "匯出完成!", vbOKOnly, "通知"
End Sub
上面是我的模組,而紅字的部份是我目前出現的問題,
但是我不知道問題是甚麼,可以麻煩知道的人跟我說一下嗎...
這是我第一次製作巨集,因為趕時間所以沒辦法從頭學
只能硬著頭皮亂找東西打進去
謝謝!
其實可以逐步執行或設斷點來DEBUG
因為沒你的檔案,目前猜測是V的範圍不對:
Sub 收盤價網路()
Application.ScreenUpdating = False
'少.
Sheets("清單").Select
Dim V, i, k, h
'變數先宣告
V = Range("A2").End(xlDown).Row
MsgBox V, vbOKOnly
'CHECK一下範圍有抓對否
Dim a()
Dim b()
Dim c()
Dim d()
Dim e()
ReDim a(V - 1)
ReDim b(V - 1)
ReDim c(V - 1)
ReDim d(V - 1)
ReDim e(V - 1)
Sheets("收盤價網路").Select
For i = 0 To V - 1
a(i) = Cells(i * 8 + 9, 2)
b(i) = Cells(i * 8 + 7, 4)
c(i) = Cells(i * 8 + 8, 4)
d(i) = Cells(i * 8 + 9, 4)
e(i) = Cells(i * 8 + 5, 2)
Next
ChDir "C:\Users\user\Desktop"
'切換到目錄就好,不必給檔案名稱
Workbooks.Open Filename:="C:\Users\user\Desktop\上市公司股票"
Sheets("清單").Select
For i = 2 To Range("A1").End(xlDown).Row
Sheets("清單").Select
k = Cells(i, 2)
If k = "" Then Exit For
Windows("Focus.xlsm").Activate
Sheets(k).Select
h = Range("B3").End(xlDown).Row
Cells(h + 1, "B") = a(i - 2)
Cells(h + 1, "C") = b(i - 2)
Cells(h + 1, "D") = c(i - 2)
Cells(h + 1, "E") = d(i - 2)
Cells(h + 1, "F") = e(i - 2)
Next
Windows("網路資料抓取.xlsm").Activate
Sheets("收盤價網路").Select
Application.CutCopyMode = False
Workbooks("網路資料抓取.xlsm").Save
Workbooks("網路資料抓取.xlsm").Close
Application.ScreenUpdating = True
MsgBox "匯出完成!", vbOKOnly, "通知"
End Sub
內文搜尋

X