Hello everyone,
Let me try to explain you all
I have a "Download" sheet Column "A" has Stocklist when am clicking the "Download All" button it extracts all stock data and the Last 5 days' data shows to K5:P10 and that data copy and paste to the "Calculation" sheet Column "A:F" which also getting paste.
But here my problem start
The "Download" Sheet doing its job perfectly but the "calculation" sheet does not paste correctly, only the last stock pastes fully 5 days of data and the rest only pastes 1 day of data. This is incorrect 5 days of data for each stock need to be pasted into the "calculation" sheet.
Thanks in advance
below providing VBA code for your reference. please help me to solve this problem.
Let me try to explain you all
I have a "Download" sheet Column "A" has Stocklist when am clicking the "Download All" button it extracts all stock data and the Last 5 days' data shows to K5:P10 and that data copy and paste to the "Calculation" sheet Column "A:F" which also getting paste.
But here my problem start
The "Download" Sheet doing its job perfectly but the "calculation" sheet does not paste correctly, only the last stock pastes fully 5 days of data and the rest only pastes 1 day of data. This is incorrect 5 days of data for each stock need to be pasted into the "calculation" sheet.
Thanks in advance
below providing VBA code for your reference. please help me to solve this problem.
VBA Code:
'Copyright 2008-2018 ConnectCode Pte Ltd. All Rights Reserved.
'This source code is protected by International Copyright Laws.
Dim gError As Integer
Sub GetData()
' thanks to Ron McEwan :^)
Dim QuerySheet As Worksheet
Dim DataSheet As Worksheet
Dim SYMBOL As String
Dim i As Integer, N As Integer, pct As Double
Application.DisplayAlerts = False
Application.Calculation = xlCalculationAutomatic
Set DataSheet = ActiveSheet
N = Range("C1")
Clear ' clear old data
Init ' paste headings
' ----------------------------------
For i = 1 To N
Range("A1") = i
Range("B4") = Cells(i + 7, 1) ' get symbol
Cells(i + 7, 1).Select
Call GetOne(Worksheets("Download").Range("$B$4"), Worksheets("Download").Range("$B$2"), Worksheets("Download").Range("$B$3"), "$A$1", freqFlag)
' download one stock
Application.ScreenUpdating = False
Range("K5:P10").Select ' collect calculations
Selection.Copy
Sheets("Calculations").Select ' move to Calculations sheet
Cells(i + 2, 1).Select ' select proper row and paste calculations
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Sheets("Download").Select ' move to Data sheet
Application.ScreenUpdating = True
Next i
Range("A1").Select
Selection.ClearContents
Sheets("Calculations").Select
Formats
Range("C1").Select
End Sub
Sub GetOne(ByVal SYMBOL As String, ByVal startDate As Date, ByVal endDate As Date, ByVal desti As String, ByVal freq As String)
On Error GoTo ErrHandler:
Dim crumb As String
Dim cookie As String
Dim response As String
Dim strUrl As String
Dim DownloadURL As String
Dim period1, period2 As String
Dim httpReq As WinHttp.WinHttpRequest
Dim nQuery As Name
Set httpReq = New WinHttp.WinHttpRequest
DownloadURL = "https://finance.yahoo.com/lookup?s=" & SYMBOL
With httpReq
.Open "GET", DownloadURL, False
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"
.send
.waitForResponse
response = .ResponseText
cookie = Split(.getResponseHeader("Set-Cookie"), ";")(0)
End With
period1 = (startDate - DateValue("January 1, 1970")) * 86400
period2 = (endDate - DateValue("January 1, 1970")) * 86400
Dim counter As Long
Dim startCounter As Long
Dim result As String
Dim dataResult As String
Dim startResult As String
crumb = Chr(34) & "CrumbStore" & Chr(34) & ":{" & Chr(34) & "crumb" & Chr(34) & ":" & Chr(34)
startCounter = InStr(response, crumb) + Len(crumb)
While Mid(response, startCounter, 1) <> Chr(34)
result = result & Mid(response, startCounter, 1)
startCounter = startCounter + 1
Wend
crumb = result
DownloadURL = "https://query1.finance.yahoo.com/v7/finance/download/" & SYMBOL & "?period1=" & period1 & "&period2=" & period2 & "&interval=" + freq + "&events=history&crumb=" & crumb
startResult = ""
startCounter = 0
While (startResult <> "Date" And startCounter < 8)
With httpReq
.Open "GET", DownloadURL, False
.setRequestHeader "Cookie", cookie
.send
.waitForResponse
dataResult = .ResponseText
End With
startResult = Mid(dataResult, 1, 4)
startCounter = startCounter + 1
Wend
If (startResult <> "Date") Then
noErrorFound = 0
GoTo ErrHandler
End If
dataResult = Replace(dataResult, ",", vbTab)
Dim dataObj As New DataObject
dataObj.SetText dataResult
dataObj.PutInClipboard
Set currentWorksheet = ThisWorkbook.ActiveSheet
Set currentRange = currentWorksheet.Range("C7")
dataObj.GetFromClipboard
currentRange.PasteSpecial
noErrorFound = 1
ErrHandler:
If noErrorFound = 0 Then
Application.ScreenUpdating = True
' MsgBox ("Stock " + Symbol + "DONE")
End If
Resume Next
Range("C1:I1").Select
Selection.ColumnWidth = 8
'turn calculation back on
Application.DisplayAlerts = True
Range("C8:I600").Select
Selection.Sort Key1:=Range("C8"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
RemoveNames
'UpdateScale
Range("A1").Select
End Sub
'Sub Sort_Based_on_a_Single_Column()
'Worksheets("Calculations").Range("A5:Q600").Sort Key1:=Range("H8"), Order1:=xlAscending
'End Sub
Sub RemoveNames()
Dim nQuery As Name
For Each nQuery In Names
If IsNumeric(Right(nQuery.Name, 1)) Then
nQuery.Delete
End If
Next nQuery
End Sub
Sub Clear()
'
' Clear Macro
' Macro recorded 8/13/2006 by Ponzo
Sheets("Calculations").Select ' move to Calculations sheet
Worksheets("Calculations").Range("A1:F600").ClearContents ' CLEAR OLD STUFF
Worksheets("Download").Range("C8:I600").ClearContents ' CLEAR OLD STUFF
Formats
Sheets("Download").Select ' move back
End Sub
Sub Move()
'
' Move Macro
' Macro recorded 09/03/2007 by pjPonzo
'
Range("C7:C600").Select
Selection.Copy
Range("K7").Select
ActiveSheet.Paste
Range("I7:I600").Select
Selection.Copy
Range("L7").Select
ActiveSheet.Paste
End Sub
Sub Init()
'
' Init Macro
' Macro recorded 09/03/2007 by pjPonzo
'
'
Range("K3:P10").Select
Selection.Copy
Sheets("Calculations").Select
Range("A1:P2000").Select
ActiveSheet.Paste
Sheets("Download").Select
End Sub
Sub DownloadData()
Application.ScreenUpdating = False
Dim freqFlag As String
Dim numRows As Integer
Dim noErrorFoundInDownloadData As Integer
noErrorFoundInDownloadData = 0
On Error GoTo ErrHandlerDownloadData:
freqFlag = "1d"
If Worksheets("Download").Range("$B$5") = 1 Then
freqFlag = "1d"
'plus 2 due to the intitial two rows
numRows = DateDiff("d", Worksheets("Download").Range("$B$2"), Worksheets("Download").Range("$B$3")) + 2
ElseIf Worksheets("Download").Range("$B$5") = 2 Then
freqFlag = "1wk"
numRows = DateDiff("w", Worksheets("Download").Range("$B$2"), Worksheets("Download").Range("$B$3")) + 2
ElseIf Worksheets("Download").Range("$B$5") = 3 Then
freqFlag = "1mo"
numRows = DateDiff("m", Worksheets("Download").Range("$B$2"), Worksheets("Download").Range("$B$3")) + 2
End If
Worksheets("Download").Select
' Worksheets("Download").UsedRange.Clear
'Stock 1
If Worksheets("Download").Range("$C$13") <> "NONE" Then
Call GetOne(Worksheets("Download").Range("$B$4"), Worksheets("Download").Range("$B$2"), Worksheets("Download").Range("$B$3"), "$A$1", freqFlag)
End If
Range("C7").Select
Columns("C:J").EntireColumn.AutoFit
Dim vvx As Integer
vvx = Application.Version
If (vvx >= 12) Then
'Excel 2007
ActiveWorkbook.Worksheets("Download").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Download").Sort.SortFields.Add Key:=Range("C8:C6550") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Download").Sort
.SetRange Range("C7:J65500")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Else
'Exel 2003
Columns("C:J").Select
Selection.Sort Key1:=Range("C8"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End If
'End If
Worksheets("Download").Select
Worksheets("Download").Range("J8").Select
noErrorFoundInDownloadData = 1
Application.ScreenUpdating = True
ErrHandlerDownloadData:
End Sub
Sub Formats()
'
' Formats Macro
' Macro recorded 9/5/2008 by pjPonzo
'
'
Range("A1:P1").Select
Selection.Copy
Range("A10:P6000").Select
Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("A1").Select
Selection.ClearContents
End Sub
All Stock_testing.xlsm | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | |||
1 | 3 | stocks | 249 | |||||||||||||||
2 | Start Date | 9-Aug-21 | 248 | |||||||||||||||
3 | End Date | 9-Aug-22 | 247 | 9-Aug-22 | ||||||||||||||
4 | Script | INFY.NS | 246 | SYMBOL | DATE | OPEN | HIGH | LOW | CLOSE | |||||||||
5 | 245 | INFY.NS | 1-Aug-22 | 1564.00 | 1564.00 | 1537.90 | 1551.05 | |||||||||||
6 | INFY.NS | 2-Aug-22 | 1546.55 | 1547.00 | 1528.35 | 1543.60 | ||||||||||||
7 | Stocks | Date | Open | High | Low | Close | Adj Close | Volume | INFY.NS | 3-Aug-22 | 1549.00 | 1570.00 | 1539.90 | 1566.10 | ||||
8 | ^NSEI | 9-Aug-21 | 1661.00 | 1667.45 | 1646.40 | 1663.30 | 1,631.41 | 5018477 | INFY.NS | 4-Aug-22 | 1587.50 | 1603.85 | 1574.95 | 1599.90 | ||||
9 | ^NSEBANK | 10-Aug-21 | 1668.00 | 1680.00 | 1661.05 | 1677.25 | 1,645.09 | 6846517 | INFY.NS | 5-Aug-22 | 1608.40 | 1625.70 | 1602.30 | 1616.65 | ||||
10 | INFY.NS | 11-Aug-21 | 1674.80 | 1684.00 | 1668.00 | 1677.55 | 1,645.39 | 4899639 | INFY.NS | |||||||||
11 | 12-Aug-21 | 1679.15 | 1696.50 | 1674.00 | 1689.60 | 1,657.21 | 5659579 | |||||||||||
12 | 13-Aug-21 | 1697.00 | 1722.50 | 1691.30 | 1712.20 | 1,679.37 | 5612790 | |||||||||||
13 | 16-Aug-21 | 1707.70 | 1719.95 | 1690.10 | 1704.40 | 1,671.72 | 4564109 | |||||||||||
14 | 17-Aug-21 | 1703.90 | 1748.90 | 1686.55 | 1741.65 | 1,708.26 | 9510390 | |||||||||||
15 | 18-Aug-21 | 1729.00 | 1755.50 | 1727.00 | 1733.45 | 1,700.22 | 6686090 | |||||||||||
16 | 20-Aug-21 | 1716.10 | 1745.00 | 1716.10 | 1732.95 | 1,699.73 | 6206972 | |||||||||||
17 | 23-Aug-21 | 1735.75 | 1753.15 | 1732.00 | 1738.75 | 1,705.41 | 6189051 | |||||||||||
18 | 24-Aug-21 | 1750.00 | 1757.00 | 1712.70 | 1720.85 | 1,687.86 | 7602939 | |||||||||||
19 | 25-Aug-21 | 1718.00 | 1746.90 | 1718.00 | 1735.55 | 1,702.28 | 6153253 | |||||||||||
20 | 26-Aug-21 | 1734.00 | 1746.00 | 1720.00 | 1727.70 | 1,694.58 | 5302248 | |||||||||||
Download |
Cell Formulas | ||
---|---|---|
Range | Formula | |
C1 | C1 | =COUNTA(A8:A600) |
B2 | B2 | =B3-365 |
B3,K3 | B3 | =TODAY() |
I1 | I1 | =COUNTA($G$8:$G$600) |
I2 | I2 | =COUNTA($I$8:$I$600)-1 |
I3 | I3 | =COUNTA($I$8:$I$600)-2 |
I4 | I4 | =COUNTA($I$8:$I$600)-3 |
I5 | I5 | =COUNTA($I$8:$I$600)-4 |
L5 | L5 | =INDEX($C$8:$C$600,I5) |
M5 | M5 | =INDEX($D$8:$D$600,I5) |
N5 | N5 | =INDEX($E$8:$E$600,I5) |
O5 | O5 | =INDEX($F$8:$F$600,I5) |
P5 | P5 | =INDEX($G$8:$G$600,I5) |
L6 | L6 | =INDEX($C$8:$C$600,I4) |
M6 | M6 | =INDEX($D$8:$D$600,I4) |
N6 | N6 | =INDEX($E$8:$E$600,I4) |
O6 | O6 | =INDEX($F$8:$F$600,I4) |
P6 | P6 | =INDEX($G$8:$G$600,I4) |
L7 | L7 | =INDEX($C$8:$C$600,I3) |
M7 | M7 | =INDEX($D$8:$D$600,I3) |
N7 | N7 | =INDEX($E$8:$E$600,I3) |
O7 | O7 | =INDEX($F$8:$F$600,I3) |
P7 | P7 | =INDEX($G$8:$G$600,I3) |
L8 | L8 | =INDEX($C$8:$C$600,I2) |
M8 | M8 | =INDEX($D$8:$D$600,I2) |
N8 | N8 | =INDEX($E$8:$E$600,I2) |
O8 | O8 | =INDEX($F$8:$F$600,I2) |
P8 | P8 | =INDEX($G$8:$G$600,I2) |
L9 | L9 | =INDEX($C$8:$C$600,I1) |
M9 | M9 | =INDEX($D$8:$D$600,I1) |
N9 | N9 | =INDEX($E$8:$E$600,I1) |
O9 | O9 | =INDEX($F$8:$F$600,I1) |
P9 | P9 | =INDEX($G$8:$G$600,I1) |
K5:K10 | K5 | =$B$4 |
Cells with Data Validation | ||
---|---|---|
Cell | Allow | Criteria |
B4 | List | =SYMBOL!$A$2:$A$1917 |