【问题标题】:Get result of macro in a cell in VBA在 VBA 中的单元格中获取宏的结果
【发布时间】:2022-08-21 11:17:48
【问题描述】:

当我运行代码时,我会使用Debug.Print (cookieValues) 在即时窗口中获得结果。

我的请求

我需要Sheet1A1 单元格中的结果。

我用MsgBox (cookieValues) 代替Debug.Print (cookieValues) 想将结果放入MsgBox,然后将其放入单元格A1,但失败了。

我的代码做了什么

从站点获取 cookie 值并在结果中给出如下所示的输出。

编码

Public Function NSEDataCall(website, setCookies) As String

Dim XMLHTTP As WinHttp.WinHttpRequest

\'Initialize XMLHttp Object
\'Use the best/proper XMLHttp object available on your system
Set XMLHTTP = CreateObject(\"WinHttp.WinHttpRequest.5.1\") \' needs Microsoft WinHTTP Services 5.1 reference
    
\' XMLHTTP.Option(WinHttpRequestOption_EnableRedirects) = False \' WinHttpRequestOption_EnableRedirects=6
        
XMLHTTP.Open \"GET\", website, False
    
\' Set headers.
XMLHTTP.setRequestHeader \"REFERER\", website
XMLHTTP.setRequestHeader \"User-Agent\", \"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\"
XMLHTTP.setRequestHeader \"Accept\", \"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\"
XMLHTTP.setRequestHeader \"Accept-Language\", \"en-us,en;q=0.5\"
XMLHTTP.setRequestHeader \"Accept-Charset\", \"ISO-8859-1,utf-8;q=0.7,*;q=0.7\"

\' Set cookie value - used for second call
If Len(setCookies) > 0 Then
    XMLHTTP.setRequestHeader \"cookie\", setCookies
Else
End If

XMLHTTP.send

If Len(setCookies) > 0 Then

    \' Get response headers
    response = XMLHTTP.getAllResponseHeaders
    \' Debug.Print response

    \' Split by new line
    responseArray = Split(response, vbCrLf)
    \' Debug.Print responseArray(7)

    \' Helps to identify dataType - output comes as code numbers
    \' MsgBox (VarType(Trim(Split(Split(responseArray(5), \";\")(0), \":\")(1)) & \"; \" & Trim(Split(Split(responseArray(6), \";\")(0), \":\")(1))

    \' Return the sv_bm cookie in response array from indices 7 (indices start from 0)
    NSEDataCall = setCookies & \"; \" & Trim(Split(Split(responseArray(7), \";\")(0), \":\")(1))
   
Else

    \' Get response headers
    response = XMLHTTP.getAllResponseHeaders
    \' Debug.Print response

    \' Split by new line
    responseArray = Split(response, vbCrLf)

    \' Helps to identify dataType - output comes as code numbers
    \' MsgBox (VarType(Trim(Split(Split(responseArray(5), \";\")(0), \":\")(1)) & \"; \" & Trim(Split(Split(responseArray(6), \";\")(0), \":\")(1))

    \' Return the cookies in response array from indices 5 to 9
    NSEDataCall = Trim(Split(Split(responseArray(5), \";\")(0), \":\")(1)) & \"; \" & Trim(Split(Split(responseArray(6), \";\")(0), \":\")(1)) & \"; \" & Trim(Split(Split(responseArray(7), \";\")(0), \":\")(1)) & \"; \" & Trim(Split(Split(responseArray(8), \";\")(0), \":\")(1)) & \"; \" & Trim(Split(Split(responseArray(9), \";\")(0), \":\")(1))
    \'Debug.Print (responseArray(5) + responseArray(6))

End If
End Function


\' My Macro
Sub GetNSECookies()

Dim website As String
Dim cookieValues As String
Dim website2 As String
Dim cookieValuesFinal As String

\' First call
website = \"https://www.nseindia.com/market-data/securities-lending-and-borrowing\"
cookieValues = NSEDataCall(website, cookieValues)
\' Debug.Print (cookieValues)

\' Second call for sv_bm cookie
website = \"https://www.nseindia.com/market-data/securities-lending-and-borrowing\"
cookieValues = NSEDataCall(website, cookieValues)

\'Shows value in Immediate window
Debug.Print (cookieValues)

End Sub

立即窗口中显示的代码结果

nsit=p8XRMHoQSM5uEQUM7XIJdT8B; nseappid=eyJhbGciOiJIUzI1NiIsInUl2PkrpgUZp9w8r1UF-yXGo4Os; AKA_A2=A; ak_bmsc=520CE4F35658A3B15048CCCE60A4E7547D38DEAjbZNKcjEdm4LTLidgmXX0=; bm_mi=C5AE845425DB55CAB9626B7A4DD0F7FD~D8U6FxMuj0HFHR1iPY=; bm_sv=E2881456097AB72A45E379FB86952E6f7nV/M=

    标签: excel vba cookies


    【解决方案1】:

    将此行 Debug.Print (cookieValues) 替换为

    Sheets("Sheet1").Range("A1")= cookieValues
    

    【讨论】:

    • 效果很好!谢谢,但我有一个问题我使用Range("Cookie[cookie]") = cookieValues而不是你给定的代码,因为我做了一些更改,比如我在表格中转换了单元格A1A2,我发现我想要我的数据的当前单元格看起来像``Range("Cookie[cookie]")` 目前它工作正常,但是根据 VBA 代码,它有什么问题,因此它以后不会开始出错。
    猜你喜欢
    • 2013-07-11
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多