在单个单元格中显示Split()函数的单个子字符串或Join()函数的字符串(VBA Excel)

在单个单元格中显示Split()函数的单个子字符串或Join()函数的字符串(VBA Excel),excel,vba,string,Excel,Vba,String,我是VBA新手,我一辈子都无法解决这个问题。如果我没有用恰当的词语来描述我的问题,我深表歉意: 我在VBA中有一个函数,可以打开下载csv文件的URL。我已将分配给该下载的变量声明为“作为字符串”。然后,我将其拆分为行,并将相关行(我需要第二行中的信息)拆分为列。现在,我可以使用该函数显示第二行七列中任意一列的任何信息,但一次只能显示一列。我还可以使用Join()方法在一个单元格中显示整个第二行,子字符串之间用逗号分隔 目标:我希望能够自定义显示第二行中的哪些信息(子字符串),并希望它们显示在它

我是VBA新手,我一辈子都无法解决这个问题。如果我没有用恰当的词语来描述我的问题,我深表歉意:

我在VBA中有一个函数,可以打开下载csv文件的URL。我已将分配给该下载的变量声明为“作为字符串”。然后,我将其拆分为行,并将相关行(我需要第二行中的信息)拆分为列。现在,我可以使用该函数显示第二行七列中任意一列的任何信息,但一次只能显示一列。我还可以使用Join()方法在一个单元格中显示整个第二行,子字符串之间用逗号分隔

目标:我希望能够自定义显示第二行中的哪些信息(子字符串),并希望它们显示在它们自己的相邻单元格中(按列相邻)。这可以通过使用数据功能区上的“文本到列”功能来实现,但我希望它在visual basic代码本身中转换。谢谢

这是我的密码:

Function DailyRange(YahooTicker As String, Optional dtDate As Variant)
' Date is optional - if omitted, use today. If value is not a date, throw error.
If IsMissing(dtDate) Then
dtDate = Date
Else
If Not (IsDate(dtDate)) Then
DailyRange = CVErr(xlErrNum)
End If
End If

Dim dtPrevDate As Date
Dim strURL As String, strCSV As String, strRows() As String, strColumns() As String
Dim DailyRows As Variant
Dim dbDate As String
Dim dbHigh As Double, dbLow As Double, dbClose As Double, dbVolume As Double

dtPrevDate = dtDate - 7

' Compile the request URL with start date and end date
strURL = "http://ichart.finance.yahoo.com/table.csv?s=" & YahooTicker & _
"&a=" & Month(dtPrevDate) - 1 & _
"&b=" & Day(dtPrevDate) & _
"&c=" & Year(dtPrevDate) & _
"&d=" & Month(dtDate) - 1 & _
"&e=" & Day(dtDate) & _
"&f=" & Year(dtDate) & _
"&g=d&ignore=.csv"

' Debug.Print strURL

Set http = CreateObject("MSXML2.XMLHTTP")
http.Open "GET", strURL, False
http.Send
strCSV = http.responseText

' Debug.Print strCSV

' THIS IS WHERE I RUN INTO THE PROBLEM    
' I want the most recent information which is in row 2, just below the table headings.
' I want date, price open, high, low, close, and volume which positions are shown below
strRows() = Split(strCSV, Chr(10)) ' split the CSV into rows
strColumns = Split(strRows(1), ",") ' split the relevant row into columns. 1 means 2nd row, starting at index 0
DailyRows = Join(strColumns, ",")
dbDate = strColumns(0) ' means 1st position, date
dbHigh = strColumns(2) ' means 3rd position, price high
dbLow = strColumns(3) ' means 4th position, price low
dbClose = strColumns(4) ' 4 means: 5th position, starting at index 0
dbVolume = strColumns(5) ' means 6th position, volume

' Now, how do I display the information I want in their own cells?
'DailyRange = dbDate & dbHigh & dbLow & dbClose & dbVolume
'DailyRange = strColumns
'DailyRange = strCSV

 DailyRange = DailyRows


Set http = Nothing

End Function

如果您试图打开CSV,为什么不使用Workbooks.open()方法打开文件,然后以这种方式操作数据