You just found a bug in the macro (from investexcel.net isn't it), you can contact the author to tell him about this.
His method is using QueryTable to fetch the data from GoogleFinance, the result of this fetch is one column of data (at column A), he then use TextToColumn to split the data to multiple columns. Under normal circumstances, the content of each cell should be in string datatype (so the TextToColumn using comma as delimited character can be used), but this GAIL symbol is something unexpected. For the row(s) where the error occurred, the cell is in numeric datatype.
You can examine this by adding "Exit Sub" statement before the code reach TextToColumn job :
Exit Sub
DataSheet.Range("a1").CurrentRegion.TextToColumns Destination:=DataSheet.Range("a1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=True, Space:=False, other:=False
Now if you look at sheet "Data", for cell A150, A151, A215, A250, the datatype for these cells is numeric (they are right aligned), and if you change the number format to General for example, the format is ruined. This is not happened with other "normal" cells, because on these normal cells, there is mixed of commas and dots, so Excel automatically treats them as string (so can be splitted using TextToColumn using comma as delimiter), but for the failed cells, Excel assumes the comma(s) is just a thousand separator symbol (no comma at all), so the TextToColumn is failed.
And since it is not always the data is grouped in 3 digits format (for example the cell A250, there is one extra digit compared to other failed cells), we cannot manually fix the error.
-----------------------------------
Here I use another method to fetch the data using WinHTTPRequest object, the return is in string datatype (so no automatic data Conversion as occurred using QueryTable), and then split manually (not using TextToColumn feature). It seemed that the result is correct :
Sub GetData2()
Dim interval As Long, numPastTradingDays As Long, timeStamp As Long, timeZoneOffset As Long, exchange As String, ticker As String, qurl As String
Dim i As Long, strOut As String, v1, v2, vDate
With Sheets("Parameters")
ticker = .Range("ticker").Value
exchange = .Range("exchange").Value
interval = .Range("interval").Value
numPastTradingDays = .Range("numTradingDays").Value
End With
qurl = "http://www.google.com/finance/getprices?" & "q=" & ticker & "&i=" & interval & "&p=" & numPastTradingDays & "d" & "&f=d,o,h,l,c,v"
With CreateObject("WINHTTP.WinHTTPRequest.5.1")
.Open "GET", qurl, False
.send
v1 = Split(.responseText, Chr$(10))
End With
timeZoneOffset = CLng(Split(v1(6), "=")(1))
strOut = "Symbol,Date,Time,OPEN,HIGH,LOW,CLOSE,VOLUME" & vbCrLf
For i = 7 To UBound(v1) - 1
v2 = Split(v1(i), ",")
If Not IsNumeric(v2(0)) Then
timeStamp = CLng(Mid$(v2(0), 2)) + timeZoneOffset * 60
vDate = timeStamp / 86400 + 25569
Else
vDate = (CLng(v2(0)) * interval + timeStamp) / 86400 + 25569
End If
strOut = strOut & ticker & "," & Format$(vDate, "YYYYMMDD") & "," & Format$(vDate, "HH:MM:") & "00," & v2(4) & "," & v2(2) & "," & v2(3) & "," & v2(1) & "," & v2(5) & vbCrLf
Next i
Open ThisWorkbook.Path & "\" & ticker & ".txt" For Output As #1
Print #1, strOut
Close #1
End Sub
Bookmarks