Excel提示用户在宏中使用的数字

时间:2012-08-15 14:36:30

标签: excel excel-vba excel-2007 vba

我有一个录制的宏来从网络服务中提取选择字段,网址类似于http://xxxx.com/reportviewer.aspx?ids= 123456789012 我想提示用户输入该号码作为变量,并将号码传递给2个位置,该号码是在宏中使用的。

我知道我必须创建另一个宏来让用户输入一个值,之后我不确定如何将其传递到正确的位置?

到目前为止,这是我的代码

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

2 个答案:

答案 0 :(得分:1)

您可以使用InputBox收集用户的输入。 收集后,您可以在继续执行代码之前测试其输入。 这是输入/验证示例:

'-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这里的完整版本来引用该变量:

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

答案 1 :(得分:0)

您可以轻松完成

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"))