简体   繁体   English

如何将单元格中的子字符串分成VBA中的数组

[英]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. 如果可能,我想通过vba进行操作。

My end goal is to capture each "name-age(nickname)" couplets into an array. 我的最终目标是将每个“姓名-年龄(昵称)”对联捕获到一个数组中。 Currently what I have started on vba -wise is 目前我在vba -wise上已经开始

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. 您正在将子例程命名为“ Split列”,但是没有使用拆分功能。

You have two options: 您有两种选择:

  1. Split by space 按空间划分

Eg cellvalue is just the variable name that refers to your cell. 例如,cellvalue只是引用您单元格的变量名称。 For multiple cells you may loop through. 对于多个单元,您可能会循环通过。

  vArray As Variant
  vArray = Split(cellvalue, " ") 
  1. Make use of Text to Columns wizard in menu bar. 利用菜单栏中的“ Text to Columns向导。 Then transpose or dump the range into a variant array. 然后将范围转置或转储到变量数组中。

Eg 例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM