简体   繁体   中英

Excel prompt user for number to use in macro

I have a recorded macro to pull select fields from a web service, the URL is something like http://xxxx.com/reportviewer.aspx?ids= 123456789012 I would like to prompt the user to input that number as a variable and have the number be passed to the 2 locations were that number is used in the macro.

I know that i have to create another macro to have the user input a value for after that I'm unsure of how pass that along to be filled in the correct location?

Here is my code so far

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

You can use InputBox to collect the user's input. After collecting, you can test their input before continuing to execute the code. here's the input/validation example:

'-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

Your code can reference the variable by refering to the sUserInput here's a complete version:

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

You can do it easily

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM