Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get nth element from VBA string array

Tags:

arrays

excel

vba

How would I extract the nth element from a VBA string array?

I have a string, like: TEST - 2017/01/20 = Invoice

I wish to extract out the 2nd and 3rd elements as concisely as possible. I was trying something Javascripty like this, which doesn't work, so there must be a VBA way:

Dim Report As String
Dim CurrLine As String
Dim CurrDate As String
Dim CurrTask As String

CurrLine = "TEST - 2017/01/20 = Invoice"
CurrDate = Trim(Split(CurrLine, '-')[1])
CurrTask = Trim(Split(CurrLine, '=')[1])
Report = Report & CHR(34) & CurrDate & CHR(34) & "," & CHR(34) & CurrTask & CHR(34)

//result: "2017/01/20","Invoice"
like image 513
crashwap Avatar asked Feb 05 '23 04:02

crashwap


1 Answers

a possible solution

Dim Report As String
Dim CurrLine As String
Dim CurrDate As String
Dim CurrTask As String, St 


CurrLine = "TEST - 2017/01/20 = Invoice"
St=Split(replace(CurrLine, "=","-"),"-")
CurrDate = St(1)
CurrTask = St(2)
Report = Report & CHR(34) & CurrDate & CHR(34) & "," & CHR(34) & CurrTask & CHR(34)  //result: "2017/01/20","Invoice"
like image 67
h2so4 Avatar answered Feb 12 '23 08:02

h2so4