1

Web サービスから選択フィールドをプルする記録されたマクロがあります。URL は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
4

2 に答える 2

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
于 2012-08-15T14:48:58.653 に答える
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"))
于 2012-08-15T14:50:12.457 に答える