简体   繁体   English

VBA宏用于下一个循环

[英]VBA Macro For next loop

I'm a noob at coding. 我是编码的菜鸟。 I got help creating the following code. 我得到了创建以下代码的帮助。 However, I need to create a for and next loop. 但是,我需要创建一个for和next循环。 Basically, URL = Sheets("Sheet2").Range(A:A) 基本上,URL = Sheets(“ Sheet2”)。Range(A:A)

The URL needs to change in every loop. URL需要在每个循环中更改。 The URL will be listed from A1 and down to some A(X). 该URL将从A1列出,直至A(X)。

I've heard its very easy to do but i've been spending a few hours trying to learn it and its way beyond my skills... 我听说它很容易做,但是我已经花了几个小时试图学习它,而且它超出了我的技能范围...

 Sub Test7() 'Haluk '11/12/2017 Dim objHTTP As Object Dim MyScript As Object Dim myData As Variant Dim myLength As Byte 'Clean the sheet ActiveSheet.Cells.Clear URL = "https://min-api.cryptocompare.com/data/histominute?fsym=BTC&tsym=USD&limit=60&aggregate=3&e=CCCAGG" 'The returned JSon table contents have the primary key/label named as "Data" 'We are going to refer this "Data" in the following two JScripts "getValue" and "getLength" Set MyScript = CreateObject("MSScriptControl.ScriptControl") MyScript.Language = "JScript" MyScript.AddCode "function getValue(JSonList, JItem, JSonProperty) { return JSonList.Data[JItem][JSonProperty]; }" MyScript.AddCode "function getLength(JSonList) { return JSonList.Data.length; }" Set objHTTP = CreateObject("MSXML2.XMLHTTP") objHTTP.Open "GET", URL, False objHTTP.Send 'Get the JSon table Set RetVal = MyScript.Eval("(" & objHTTP.responseText & ")") objHTTP.abort 'Retrieve the value of the key "close" in the 4th item of the data set "Data" 'with the help of the JScript function "getValue" above myData = MyScript.Run("getValue", RetVal, 4, "close") MsgBox "This is a small demo...." & vbCrLf & vbCrLf _ & "Value of the key 'close' of the 4th data in the JSON table is: " & myData 'Get the count of items in the JSon table under "Data" myLength = MyScript.Run("getLength", RetVal) 'Write labels of the key in the table to the sheet Range("B1") = "time" Range("C1") = "close" Range("D1") = "high" Range("E1") = "low" Range("F1") = "open" Range("G1") = "volumefrom" Range("H1") = "volumeto" Range("J1") = "TimeFrom:" Range("J2") = "TimeTo:" Range("B1:H1, J1:J2").Font.Bold = True Range("B1:H1, J1:J2").Font.Color = vbRed 'Get all the values of the JSon table under "Data" For i = 0 To myLength - 1 Range("A" & i + 2) = "Data -" & i Range("B" & i + 2) = MyScript.Run("getValue", RetVal, i, "time") / (CDbl(60) * CDbl(60) * CDbl(24)) + #1/1/1970# Range("C" & i + 2) = MyScript.Run("getValue", RetVal, i, "close") Range("D" & i + 2) = MyScript.Run("getValue", RetVal, i, "high") Range("E" & i + 2) = MyScript.Run("getValue", RetVal, i, "low") Range("F" & i + 2) = MyScript.Run("getValue", RetVal, i, "open") Range("G" & i + 2) = MyScript.Run("getValue", RetVal, i, "volumefrom") Range("H" & i + 2) = MyScript.Run("getValue", RetVal, i, "volumeto") Next 'Get the time info given in the JSon table Range("K1") = RetVal.TimeFrom / (CDbl(60) * CDbl(60) * CDbl(24)) + #1/1/1970# Range("K2") = RetVal.TimeTo / (CDbl(60) * CDbl(60) * CDbl(24)) + #1/1/1970# Set objHTTP = Nothing Set MyScript = Nothing End Sub 

put everything except these lines on a loop 将除这些行以外的所有内容都放在一个循环中

Dim objHTTP As Object
    Dim MyScript As Object
    Dim myData As Variant
    Dim myLength As Byte

    'Clean the sheet

    ActiveSheet.Cells.Clear

for x=1 to Application.Counta(Sheet2.Columns(1)) 对于x = 1到Application.Counta(Sheet2.Columns(1))
...the rest of the code ...其余代码
next 下一个

change URL line to URL=Sheet2.Cells(x,1) 将URL行更改为URL=Sheet2.Cells(x,1)

and Range to Sheet1.Range 和范围到Sheet1.Range

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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