VBA宏,从给定的范围循环中获取URL

时间:2017-12-11 14:57:25

标签: excel vba excel-vba

我的代码当前设置的方式,它从我在代码中指出的URL中获取数据。但是,我实际上想要提供Sheet2中的URL列表,它将循环遍历,直到它提取所有数据。我不想每次都按URL更新代码。有成千上万......我怎么能这样做?

以下是代码:

SELECT 
    r.session_id                 'Session ID',
    DB_NAME(r.database_id)       'Databse Name',
    st.TEXT                      'Batch Text',
    qp.query_plan                'XML Plan',
    r.start_time                 'Start Time',
    r.status                     'Query Status',
    r.total_elapsed_time/60/1000.0   'Total Elapsed Time'
FROM sys.dm_exec_requests AS r
     CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) AS st
     CROSS APPLY sys.dm_exec_query_plan(r.plan_handle) AS qp
ORDER BY cpu_time DESC;

KILL {r.session_id}

1 个答案:

答案 0 :(得分:2)

我只是假装所有的URL都在A列中:

Public Sub exceljson()
    Dim https As Object, Json As Object, i As Integer, j As Integer

    Dim Item As Variant

    Set https = CreateObject("MSXML2.XMLHTTP")

    For j = 1 to Sheets(2).UsedRange.Rows.count
        If Len(Trim$(Sheets(2).Cells(j, 1).Value2)) > 0 Then
            https.Open "GET", Trim$(Sheets(2).Cells(j, 1).Value2), False
            https.Send

            Set Json = JsonConverter.ParseJson(https.responseText)

            i = 2
            For Each Item In Json.Items
                Sheets(1).Cells(i, 2).Value = Item
                i = i + 1
            Next Item
        End If
    Next j

    MsgBox ("complete")
End Sub

我喜欢使用trim()方法来保证我没有抓到任何额外的东西