从Web导入数据后,在Excel中重命名工作表

时间:2014-05-19 16:23:17

标签: excel vba excel-vba

我想使用vba中的以下代码下载我们证券交易所的每日价格。虽然代码工作似乎无法将工作表重命名为获得价目表的相应日期。

Dim DownloadDay As Date

DownloadDay = #3/4/2014#

Do While DownloadDay < #4/4/2014#

ActiveWorkbook.Worksheets.Add

 Call website(Format(DownloadDay, "YYYYMMDD"))

'INCREMENT THE DAY

Sheets.Add.Name = "DownloadDay"

DownloadDay = DownloadDay + 1



  Loop

 End Sub

 Sub website(sDate As String)

With ActiveSheet.QueryTables.Add(Connection:= _
 "URL;http://live.mystocks.co.ke/price_list/" & DownloadDay & "/", Destination:=Range("$A$1"))


.Name = DownloadDay
'To rename each work sheet with the corresponding day'


.FieldNames = True

.RowNumbers = False

.FillAdjacentFormulas = False

.PreserveFormatting = True

.RefreshOnFileOpen = False

.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlTables
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False


 End With

 End Sub

1 个答案:

答案 0 :(得分:1)

这行VBA将设置工作表的名称:

Sheets("Sheet2").Name = "NewName"

你不能使用&#34; /&#34;但是名称中的字符,变量设置为日期而不是字符串。

试试这个。

.name = CSTR(FORMAT(DownloadDay,"YYYYMMMDD"))