如何根据excel 2013 VBA中的单元格值更改url值

我对编程和编程世界相当陌生,在我的VBA编码中遇到了一些障碍。 我需要更改作为Web查询一部分的URLstring中的值,并且无法获取要更改的值。 这里是代码:

Dim FC_Name As String FC_Name = Worksheets("Home").Range("H4") Sheets("PickWorkforce").Visible = True Sheets("PickWorkforce").Select Cells.Select Selection.ClearContents urlStr = "https://picking-nexus.iad.amazon.com/FC_Name/Workforce/data" With Sheets("PickWorkforce").QueryTables.Add(Connection:= _ "URL;" & urlStr, Destination:=Sheets("PickWorkforce").Range("$A$1")) .Name = "employeeRoster?&warehouseId=FC_Name" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .WebSelectionType = xlSpecifiedTables .WebFormatting = xlWebFormattingNone .WebTables = "1,2,3,4,5,6,7,8" .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=False End With Range("L1") = "Employee Id" Range("L2").Select ActiveCell.FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-11], Roster!R2C2:R10000C13, 12,FALSE),"""") " Range("L2").Select Selection.AutoFill Destination:=Range("L2:L1000"), Type:=xlFillDefault Sheets("PickWorkforce").Visible = True End Sub 

“FC_Name”是我尝试在URL中更改的部分,因此它从我input的任何仓库ID中读取到工作簿主页的单元格中。

任何人都可以帮我这个吗?

我build议从您的代码中删除任何Select方法,并简单地引用范围。

当引用一个variables时,它需要在引号之外,并使用&符号连接:

 Dim FC_Name As String FC_Name = Worksheets("Home").Range("H4") Sheets("PickWorkforce").Visible = True Sheets("PickWorkforce").Cells.ClearContents urlStr = "https://picking-nexus.iad.amazon.com/" & FC_Name & "/Workforce/data" With Sheets("PickWorkforce").QueryTables.Add(Connection:= _ "URL;" & urlStr, Destination:=Sheets("PickWorkforce").Range("$A$1")) .Name = "employeeRoster?&warehouseId=" & FC_Name .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .WebSelectionType = xlSpecifiedTables .WebFormatting = xlWebFormattingNone .WebTables = "1,2,3,4,5,6,7,8" .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=False End With Range("L1") = "Employee Id" Range("L2").FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-11], Roster!R2C2:R10000C13, 12,FALSE),"""") " Range("L2").AutoFill Destination:=Range("L2:L1000"), Type:=xlFillDefault Sheets("PickWorkforce").Visible = True End Sub