简体   繁体   中英

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. Basically, URL = Sheets("Sheet2").Range(A:A)

The URL needs to change in every loop. The URL will be listed from A1 and down to some 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))
...the rest of the code
next

change URL line to URL=Sheet2.Cells(x,1)

and Range to Sheet1.Range

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