我很遗憾不熟悉正则表达式,因为我不是程序员,但我猜这个问题很容易使用正则表达式解决(我绝对愿意接受其他建议)
我想使用split函数来分割单元格的值并将其分散到多个单元格中。分隔符是逗号。但问题是有些用户在评论中使用逗号,而Split函数用它来分割字符串中间的注释。
例如包含值的单元格:
0001,"姓名","地址","喜欢苹果,橘子 和李子"
需要分成多个单元格,分别为0001" name" "地址"和#34;喜欢苹果,橘子和李子"。
我的代码也会拆分评论,我希望它忽略评论或其他所有带双引号的内容。这是一个样本:
Sub SplittingStrings()
Dim wb As Workbook
Dim ws As Worksheet
Dim strInput As String
Dim counter As Integer
Dim cell As Variant
Dim splitCount As Integer
Dim splitString() As String
Dim category As Variant
Set wb = ActiveWorkbook
Set ws = wb.ActiveSheet
counter = 1
For Each cell In Range("A1", "A2000")
If cell.Value <> "" Then
strInput = cell.Value
splitCount = 2
splitString = Split(strInput, ",")
For Each category In splitString
Cells(counter, splitCount).Value = category
splitCount = splitCount + 1
Next category
End If
counter = counter + 1
Next cell
End Sub
如何排除带有双引号的东西被分割函数考虑?
答案 0 :(得分:1)
请试一试,看看你是否得到了想要的输出。
如果需要,调整变量。
Sub SplittingStringsUsingRegEx()
Dim lr As Long, c As Long
Dim Rng As Range, cell As Range
Dim RE, Match, Matches
Application.ScreenUpdating = False
lr = Cells(Rows.Count, 1).End(xlUp).Row
Set Rng = Range("A1:A" & lr)
Set RE = CreateObject("VBScript.RegExp")
With RE
.Global = True
.Pattern = "\d+|"".+?"""
End With
c = 2
For Each cell In Rng
If RE.test(cell.Value) Then
Set Matches = RE.Execute(cell.Value)
For Each Match In Matches
Cells(cell.Row, c) = Replace(Match, """", "")
c = c + 1
Next Match
End If
c = 2
Next cell
Application.ScreenUpdating = True
End Sub
答案 1 :(得分:1)
没有正则表达式:
我们需要“保护”用双引号封装的逗号:
Sub ProtectStuff()
Dim i As Long, N As Long, v As String, v2 As String
Dim ProtectMode As Boolean, DQ As String, rep As String
Dim CH As String, arr
DQ = """"
rep = Chr(1)
N = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To N
v = Cells(i, "A").Value
If v <> "" Then
ProtectMode = False
v2 = ""
For j = 1 To Len(v)
CH = Mid(v, j, 1)
If CH = DQ Then ProtectMode = Not ProtectMode
If CH = "," And ProtectMode Then CH = rep
v2 = v2 & CH
Next j
End If
arr = Split(v2, ",")
j = 2
For Each a In arr
Cells(i, j) = Replace(a, rep, ",")
j = j + 1
Next a
Next i
End Sub
答案 2 :(得分:0)
文本到列将按照您所需的方式执行,与分割功能不同。