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

时间:2017-11-01 22:10:20

标签: excel vba excel-vba split

我将数据从网络上提取到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列表中的下一个人,这种方法效果很好。但是,为简化起见,是否可以将此代码转换为用逗号分隔名称,与许可证字段相同,但是用连字符?

提前致谢!

2 个答案:

答案 0 :(得分:2)

你可以做另一次拆分 - 不确定名称,因为没有逗号 - 但是对于许可证。根据@Sorceri的建议,加入一个连字符存在的检查。严格地说,你也应该为冒号做同样的事情。

  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

答案 1 :(得分:1)

不确定您是否需要单独的单元格作为名字和姓氏,因为您想要拆分它,但如果是这种情况,您可以更改下面的'YourRange。您不需要将原始字符串拆分为数组,只需使用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)添加到拆分的末尾来返回单个字符串。例如,如果i = 0,则将整个字符串 previous 返回到第一个分隔符。同样,i = 1将在第一个分隔符后返回整个字符串,i = 2将返回第二个分隔符之后的所有内容,依此类推。依此类推。