从Web导入数据后,将Excel工作表重命名为Excel

我想用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 

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

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

您不能在名称中使用“/”字符,而将variables设置为date而不是string。

尝试这个。

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