[英]Unable to get the file from web using workbook.open
我正在嘗試將 CSV 文件從網站導入到我的工作表中。 我的本地 excel 與使用電源查詢獲取數據的網站有其他連接。
以下是我遇到問題的代碼。 我無法第二次導航到 CSV 文件。 即..只有當我關閉並打開我的本地 excel 時,只有 workbook.open() 才能正常工作。
我得到的錯誤是:抱歉,我們無法打開“”,因為服務器沒有響應。 (實際上我可以導航到 csv 文件。服務器沒有問題)。
此外,當我使用 workbook.open 時,我可以看到在 C:\Users\DELL\AppData\Local\Microsoft\Office\16.0\OfficeFileCache 上創建了一些名為 centraltable.laccdb 的文件。 如果我關閉我的本地 excel laccb 文件將被刪除。
請幫助我如何第二次運行代碼,因為我每 10 分鍾觸發一次宏。
Application.ScreenUpdating = False
URLOI = "https://www1.nseindia.com/content/nsccl/fao_participant_oi_" & dt & ".csv"
On Error Resume Next
DoEvents
Set oWB = Application.Workbooks.Open(Filename:=URLOI)
If Not oWB Is Nothing Then
Instructions.Range("A" & k) = Format(Now - 11, "dd-mmm-yy")
If Not IsError(Application.Match("FII", oWB.Sheets(1).Columns("A").Cells, 0)) Then
Var = Application.Match("FII", oWB.Sheets(1).Columns("A").Cells, 0)
Instructions.Range("B" & k) = oWB.Sheets(1).Range("B" & Var).Value
End If
End If
With Application
.Calculation = xlCalculationAutomatic
.EnableEvents = True
.ScreenUpdating = True
End With
oWB.Close False
Set oWB = Nothing
DoEvents
如果我理解你的問題,這應該對你有用。 需要添加您的參考資料。
Sub CSVHelp()
'DECLARE
Dim URL As String
Dim filePath As String
Dim sheetName As String
'DEFINE
URL = "https://people.sc.fsu.edu/~jburkardt/data/csv/cities.csv"
filePath = "C:\Desktop\myCSVfile.csv"
sheetName = "Sheet1"
'DELETE
Worksheets(sheetName).Activate
Cells.Select
Selection.Delete Shift:=xlUp
'Get Data
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
objHTTP.Open "POST", URL, False
objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.send ("")
replyTXT = objHTTP.responseText
'Store in File
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile(filePath, True)
a.WriteLine (replyTXT)
a.Close
'Import to Sheet
i = 0
Dim fso As FileSystemObject: Set fso = New FileSystemObject
Set txtStream = fso.OpenTextFile(filePath, ForReading, False)
Do While Not txtStream.AtEndOfStream
Worksheets(sheetName).Cells(i + 1, 1).Value = txtStream.ReadLine
i = i + 1
Loop
txtStream.Close
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False, TrailingMinusNumbers:=True
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.