在使用VBA的Excel中,如何分割由不同分隔符(即:逗号,冒号等)分隔的多个值

我将数据从networking拉入Excel电子表格。 所有数据都列在A列Sheet1中。 例如:

Name: Smith John A. License: General - Master License Status: Active City/State: Anytown, USA County: Thatoneyouknow Contact Information Cell Phone: (555) 555-555 Email Address: johnsmith@johnsmith.com Region: One Ever Been Disciplined?: No Notes: None 

然后它会重复下一个人的相似信息,等等。

我已经能够通过使用下面的代码分隔数据的冒号拆分文本:

 Sub DataReOrganizer() Dim s1, s2 As Worksheet Dim Cook, i, K As Long Dim v As String Set s1 = Sheets("Sheet1") Set s2 = Sheets("Sheet2") Cook = s1.Cells(Rows.Count, "A").End(xlUp).Row K = 2 For i = 1 To Cook v = s1.Cells(i, "A").Text If v = "Contact Information" Then K = K + 1 Else ary = Split(v, ": ") If ary(0) = "Name" Then s2.Cells(K, 1) = ary(1) If ary(0) = "License" Then s2.Cells(K, 2) = ary(1) If ary(0) = "License Status" Then s2.Cells(K, 3) = ary(1) If ary(0) = "City/State" Then s2.Cells(K, 4) = ary(1) If ary(0) = "County" Then s2.Cells(K, 5) = ary(1) If ary(0) = "Home Phone" Then s2.Cells(K, 6) = ary(1) If ary(0) = "Work Phone" Then s2.Cells(K, 7) = ary(1) If ary(0) = "Cell Phone" Then s2.Cells(K, 8) = ary(1) If ary(0) = "Email Address" Then s2.Cells(K, 9) = ary(1) If ary(0) = "Region" Then s2.Cells(K, 10) = ary(1) If ary(0) = "Ever Been Disciplined?" Then s2.Cells(K, 11) = ary(1) If ary(0) = "Note" Then s2.Cells(K, 12) = ary(1) End If Next I End Sub 

到目前为止,通过获取数据,这种方法运行良好,将每个新行中的值分隔成不同的列,作为sheet2上列表中的下一个人。 但是,为了简化事情,是否有可能将这些代码转换成用逗号分隔名称,与许可证字段相同,而是使用连字符?

提前致谢!

不知道如果你想分开的单元格的名字和姓氏,因为你想分裂,但你可以改变'YourRange下面,如果是这样的话。 您不需要将原始string拆分为数组,只需使用Like和通配符*

 Sub DataReOrganizer() Dim s1, s2 As Worksheet Dim Cook, i, K As Long Dim v As String Set s1 = Sheets("Sheet1") Set s2 = Sheets("Sheet2") Cook = s1.Cells(Rows.Count, "A").End(xlUp).Row K = 2 For i = 1 To Cook v = s1.Cells(i, "A").text If v = "Contact Information" Then K = K + 1 Else If v Like "Name:*" Then 'YourRange = Split(Split(v, ":")(1), ",")(0) 'Last Name s2.Cells(K, 1) = Split(v, ",")(1) 'First Name End If If v Like "License:*" Then s2.Cells(K, 2) = Split(v, "-")(1) If v Like "License Status:*" Then s2.Cells(K, 3) = Split(v, ":")(1) If v Like "City/State:*" Then s2.Cells(K, 4) = Split(v, ":")(1) If v Like "County:*" Then s2.Cells(K, 5) = Split(v, ":")(1) If v Like "Home Phone:*" Then s2.Cells(K, 6) = Split(v, ":")(1) If v Like "Work Phone:*" Then s2.Cells(K, 7) = Split(v, ":")(1) If v Like "Cell Phone:*" Then s2.Cells(K, 8) = Split(v, ":")(1) If v Like "Email Address:*" Then s2.Cells(K, 9) = Split(v, ":")(1) If v Like "Region:*" Then s2.Cells(K, 10) = Split(v, ":")(1) If v Like "Ever Been Disciplined?:*" Then s2.Cells(K, 11) = Split(v, ":")(1) If v Like "Note:*" Then s2.Cells(K, 12) = Split(v, ":")(1) End If Next i End Sub 

使用Split()时有几个选项。 通常,它用于将分割添加到数组,比如你所做的。 但是,您可以完全跳过数组,并通过将(i)添加到分割的末尾来返回单个string。 例如,如果i = 0 ,则会在第一个分隔符之前返回整个string。 同样, i = 1将在第一个分隔符后返回整个string, i = 2将在第二个之后返回所有内容,等等。

你可以做另一个拆分 – 不知道名称,因为没有逗号 – 但是许可证。 结合@Sorceribuild议的连字符存在的检查。 严格地说,你也应该对冒号做同样的事情。

  If ary(0) = "License" Then s2.Cells(K, 2) = Split(ary(1), "-")(0) If InStr(ary(1), "-") > 0 Then s2.Cells(K, 3) = Split(ary(1), "-")(1) End If End If