简体   繁体   中英

How to separate substrings in a cell into array in vba

I was wondering if anyone can give me some pointers. I have one column with data arrange like this in each cell:

Column E

E1-  name-age(nickname)   name-age(nickname)   name-age(nickname) ....
E2-  name-age(nickname)   name-age(nickname)   name-age(nickname) ....
etc...

the problem is its all in one column, and I know I can use text to column feature and delimit them by space. However, is there a more elegant way to do this? I'd like to do it over vba if possible.

My end goal is to capture each "name-age(nickname)" couplets into an array. Currently what I have started on vba -wise is

Sub SplitColumn()
    Details = Cells(i, 5).Value  // where the big column data is located
    tempString = Left(Details, InStr(Details, "  "))
End Sub

With this, I could only get the first name-age(nickname) couplet in that cell.. is there a better way to approch this?

Thanks in advance for help.

You are naming your subroutine as Split columns but there's no split function used.

You have two options:

  1. Split by space

Eg cellvalue is just the variable name that refers to your cell. For multiple cells you may loop through.

  vArray As Variant
  vArray = Split(cellvalue, " ") 
  1. Make use of Text to Columns wizard in menu bar. Then transpose or dump the range into a variant array.

Eg

vArray = Sheets(1).Range("A1:H10").value

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