Excel 使用特定单词分割文本+;文字换行
首先,我还想根据特定的单词分割文本。然后,我想自动一个接一个地拆分文本 1 ID001公司名称:ABC有限公司地址:Central ID002公司名称:Delino公司地址:旺角ID003公司名称:Moria公司地址:沙田ID004公司名称:Sherlyn公司地址:旺角ID005公司名称:Coco公司地址:沙田 2 ID010公司名称:Toro Limited地址:Central ID012公司名称:奔驰公司地址:旺角ID013公司名称:Korz公司地址:沙田ID014公司名称:Chopra公司地址:旺角ID015公司名称:Toto公司地址:沙田 我尝试使用vbaExcel 使用特定单词分割文本+;文字换行,excel,vba,Excel,Vba,首先,我还想根据特定的单词分割文本。然后,我想自动一个接一个地拆分文本 1 ID001公司名称:ABC有限公司地址:Central ID002公司名称:Delino公司地址:旺角ID003公司名称:Moria公司地址:沙田ID004公司名称:Sherlyn公司地址:旺角ID005公司名称:Coco公司地址:沙田 2 ID010公司名称:Toro Limited地址:Central ID012公司名称:奔驰公司地址:旺角ID013公司名称:Korz公司地址:沙田ID014公司名称:Chopra公司
Dim rng As Range
Dim FullName As Varient
With ActiveSheet
Set rng = .Range(.Cells(1, 1), .Cells(5, 1))
FullName = split(rng,"ID")
End With
之前:之后:
你可以试试这样的东西 如果数据表不是表1,请记住在测试代码之前更改数据表
Sub SplitText()
Dim wsData As Worksheet, dws As Worksheet
Dim rng As Range, cel As Range
Dim FullName() As String
Dim lr As Long, i As Long, dlr As Long
Dim str()
Set wsData = Sheets("Sheet1") 'Sheet with Data
lr = wsData.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = wsData.Range("A1:A" & lr)
Set dws = Worksheets.Add
For Each cel In rng
FullName = Split(cel.Value, "ID")
ReDim str(1 To UBound(FullName), 1 To 2)
For i = 1 To UBound(FullName)
str(i, 1) = "ID" & Left(FullName(i), InStr(FullName(i), " ") - 1)
str(i, 2) = VBA.Trim(Right(FullName(i), Len(FullName(i)) - 3))
Next i
If dws.Range("A1").Value = "" Then
dlr = 1
Else
dlr = dws.Cells(Rows.Count, 1).End(xlUp).Row + 1
End If
dws.Range("A" & dlr).Resize(UBound(str, 1), 2).Value = str
Erase str
Next cel
End Sub
你可以试试这样的 如果数据表不是表1,请记住在测试代码之前更改数据表
Sub SplitText()
Dim wsData As Worksheet, dws As Worksheet
Dim rng As Range, cel As Range
Dim FullName() As String
Dim lr As Long, i As Long, dlr As Long
Dim str()
Set wsData = Sheets("Sheet1") 'Sheet with Data
lr = wsData.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = wsData.Range("A1:A" & lr)
Set dws = Worksheets.Add
For Each cel In rng
FullName = Split(cel.Value, "ID")
ReDim str(1 To UBound(FullName), 1 To 2)
For i = 1 To UBound(FullName)
str(i, 1) = "ID" & Left(FullName(i), InStr(FullName(i), " ") - 1)
str(i, 2) = VBA.Trim(Right(FullName(i), Len(FullName(i)) - 3))
Next i
If dws.Range("A1").Value = "" Then
dlr = 1
Else
dlr = dws.Cells(Rows.Count, 1).End(xlUp).Row + 1
End If
dws.Range("A" & dlr).Resize(UBound(str, 1), 2).Value = str
Erase str
Next cel
End Sub
您可以使用正则表达式来实现这一点。以下内容将匹配两个“ID”块之间或一个“ID”和行尾之间范围内的所有值
Public Sub RegExDemo()
Dim RegExp As Object
Dim arr As Variant
Dim submatches, match, matches
Dim RowIndex As Long, j As Long
Dim c
With Sheet2
arr = .Range(.Cells(1, 1), .Cells(2, 1)).Value2
End With
Set RegExp = CreateObject("vbscript.regexp")
With RegExp
.Global = True
.ignorecase = False
.MultiLine = True
.Pattern = "(ID[0-9]{1,}) (.*?)(?= ID[0-9]{1,}|$)"
RowIndex = 1
For Each c In arr
If .test(c) Then
Set matches = .Execute(c)
For Each match In matches
Set submatches = match.submatches
For j = 0 To submatches.Count - 1
ActiveSheet.Cells(RowIndex, 1).Offset(0, j).Value2 = Trim(submatches(j))
Next j
RowIndex = RowIndex + 1
Next match
End If
Next c
End With
With ActiveSheet
With .Range(.Cells(1, 1), .Cells(.Cells(.Rows.Count, 1).End(xlUp).Row, 2))
.Columns.AutoFit
.Rows.AutoFit
End With
End With
End Sub
制作:
您可以使用正则表达式来实现这一点。以下内容将匹配两个“ID”块之间或一个“ID”和行尾之间范围内的所有值
Public Sub RegExDemo()
Dim RegExp As Object
Dim arr As Variant
Dim submatches, match, matches
Dim RowIndex As Long, j As Long
Dim c
With Sheet2
arr = .Range(.Cells(1, 1), .Cells(2, 1)).Value2
End With
Set RegExp = CreateObject("vbscript.regexp")
With RegExp
.Global = True
.ignorecase = False
.MultiLine = True
.Pattern = "(ID[0-9]{1,}) (.*?)(?= ID[0-9]{1,}|$)"
RowIndex = 1
For Each c In arr
If .test(c) Then
Set matches = .Execute(c)
For Each match In matches
Set submatches = match.submatches
For j = 0 To submatches.Count - 1
ActiveSheet.Cells(RowIndex, 1).Offset(0, j).Value2 = Trim(submatches(j))
Next j
RowIndex = RowIndex + 1
Next match
End If
Next c
End With
With ActiveSheet
With .Range(.Cells(1, 1), .Cells(.Cells(.Rows.Count, 1).End(xlUp).Row, 2))
.Columns.AutoFit
.Rows.AutoFit
End With
End With
End Sub
制作:
那么您面临什么问题?vba不工作。可能代码不正确。如果文本在一行中,您可以使用
Data | TextToColumns | Fixed Width
您不需要VBA?那么您面临什么问题?VBA不起作用。可能代码不正确。如果文本在一行中,您可以使用数据| TextToColumns |固定宽度
您不需要VBA吗?