Excel提示用户在macros中使用的号码

我有一个logging的macros从Web服务拉select字段,该URL是类似http://xxxx.com/reportviewer.aspx?ids= 123456789012我想提示用户input该数字作为variables,并有该号码被传递到2个地点是该号码在macros中使用。

我知道我不得不创build另一个macros,让用户input一个值之后,我不确定如何通过这一点,以填补正确的位置?

这是我的代码到目前为止

Sub Macro2() ' ' Macro2 Macro ' ' Keyboard Shortcut: Ctrl+r ' With ActiveSheet.QueryTables.Add(Connection:= _ "URL;http://xxxx.com/reportviewer.aspx?ids=123456789012", _ Destination:=Range("$A$1")) .Name = "reportviewer.aspx?ids=123456789012" .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 = "30,33,37,38,46" .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=False End With End Sub Range("A2").Select 

您可以使用InputBox来收集用户的input。 收集之后,您可以在继续执行代码之前testing其input。 这里是input/validation示例:

 '-store user input in 'sUserInput' variable Dim sUserInput As String sUserInput = InputBox("Enter Number:", "Collect User Input") 'test input before continuing to validate the input If Not (Len(sUserInput) > 0 And IsNumeric(sUserInput)) Then MsgBox "Input not valid, code aborted.", vbCritical Exit Sub End If 

你的代码可以通过引用sUserInput来引用variables,这里是一个完整的版本:

 Sub Macro2() ' ' Macro2 Macro ' ' Keyboard Shortcut: Ctrl+r ' '-store user input in 'sUserInput' variable Dim sUserInput As String sUserInput = InputBox("Enter Number:", "Collect User Input") 'test input before continuing to validate the input If Not (Len(sUserInput) > 0 And IsNumeric(sUserInput)) Then MsgBox "Input not valid, code aborted.", vbCritical Exit Sub End If With ActiveSheet.QueryTables.Add(Connection:= _ "URL;http://network.construction.com/reportviewer.aspx?ids=" & sUserInput, _ Destination:=Range("$A$1")) .Name = "reportviewer.aspx?ids=" & sUserInput .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 = "30,33,37,38,46" .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=False End With End Sub 

你可以轻松地做到这一点

 Sub Macro2() ' ' Macro2 Macro ' ' Keyboard Shortcut: Ctrl+r ' Dim stpo stpo = InputBox("Enter the report number") With ActiveSheet.QueryTables.Add(Connection:= _ "URL;http://network.construction.com/reportviewer.aspx?ids="&stpo, _ Destination:=Range("$A$1"))