[算表] EXCEL VBA 将网址修改成有变量

楼主: ahlfsri (40+大叔)   2020-01-06 12:52:14
软件: EXCEL VBA
版本: office 365
请问我要将https://goodinfo.tw/StockInfo/EquityDistributionClassHis.asp?STOCK_ID=3049中的3049改为sheet(Company)中的储存格B2,
试了""https://goodinfo.tw/StockInfo/EquityDistributionClassHis.asp?STOCK_ID="" &
Sheets("Company").Range("B2").Value,VBA说语法错误,请问版上高手是否能帮忙解惑?
Application.CutCopyMode = False
ActiveWorkbook.Queries.Add Name:="Table 3", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " 来源 =
Web.Page(Web.Contents(""https://goodinfo.tw/StockInfo/EquityDistributionClassHis.asp?STOCK_ID=3049"")),"
& Chr(13) & "" & Chr(10) & " Data3 = 来源{3}[Data]," & Chr(13) & "" &
Chr(10) & " 已变更类型 = Table.TransformColumnTypes(Data3,{{""周别"", type
text}, {""统计 日期"", type date}, {""当月股价 收盘"", type number}, {""当月股
价 涨跌 (元)"", type number}, {""当月股价 涨跌 (%)"", type number}, {""各持股
等级股东之持股比例(%) 10张 以下"", type number" & _
"}, {""各持股等级股东之持股比例(%) 10至 50张"", type number}, {""各持
股等级股东之持股比例(%) 50至 100张"", type number}, {""各持股等级股东之持股比
例(%) 100至 200张"", type number}, {""各持股等级股东之持股比例(%) 200至 400张
"", type number}, {""各持股等级股东之持股比例(%) 400至 800张"", type number},
{""各持股等级股东之持股比例(%) 800至 1千张"", type number}, {""各持股等级股东
之持股比例(%) 超过 1千张"", type number}})" & Chr(13) & "" & Chr(10) & "in" &
Chr(13) & "" & Chr(10) & " 已变更类型" & _
""
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data
Source=$Workbook$;Location=""Table 3"";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Table 3]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table_3"
.Refresh BackgroundQuery:=False
End With
End Sub
作者: soyoso (我是耀宗)   2020-01-06 12:53:00
"https//....STOCK_ID" & sheets(..).range("b2").value字串连接上,以"字串" & 储存格值 的方式,改左右各一个双引号如用于内文宏formula的字串的话https://i.imgur.com/ocM9egW.jpg 类似这样
楼主: ahlfsri (40+大叔)   2020-01-06 13:46:00
感谢soyoso,终于可用了
作者: soyoso (我是耀宗)   2020-01-07 07:35:00
更新方面,activeworkbook.queries(...).formula如出现...范围重叠的错误讯息的话先range.clear再listobjects.add
楼主: ahlfsri (40+大叔)   2020-01-07 08:36:00
更新方面,activeworkbook.queries(...).formula, 对不起,我太菜了,有点试不出来,这一句要怎么用呢?
作者: soyoso (我是耀宗)   2020-01-07 08:39:00
...formula = 要调整的字串字串为let起至最后的已变更类型

Links booklink

Contact Us: admin [ a t ] ucptt.com