新手製作VBA巨集

因為想要製作一個可以自動更新股票的巨集
而在網上找了很多教學

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



上面是我的模組,而紅字的部份是我目前出現的問題,
但是我不知道問題是甚麼,可以麻煩知道的人跟我說一下嗎...
這是我第一次製作巨集,因為趕時間所以沒辦法從頭學
只能硬著頭皮亂找東西打進去

謝謝!
2017-01-04 13:09 發佈
文章關鍵字 新手 VBA
使用的EXCEL版本?有錯誤訊息截圖嗎?很多問題和變數及SHEET設置和範圍命名有關係.
其實可以逐步執行或設斷點來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
我的版本是2010






我是想把第一二張照片的程式碼
可以透過"匯入"那顆按鈕
分別匯到第三張圖片中的各個工作表中

您把檔案傳上來,也許會比較快得到解答…
so ..沒看到錯誤畫面,問題在哪裡?
樓主自己想想,你提供以上的資料,換做是你來解的話,你自己可以解答嗎?
是我看錯了嗎?紅框這裡是不是簡體字阿?
內文搜尋
X
評分
評分
複製連結
Mobile01提醒您
您目前瀏覽的是行動版網頁
是否切換到電腦版網頁呢?