Excel-Vba 用于在数字 + 点字符后的单元格中添加多行的脚本
Excel-Vba script to add multiple rows in cells after number + dot char
希望您能帮助我解决困扰我一段时间的问题。我太菜鸟了,无法自己解决这个问题,所以这里是:
有没有办法让VBA遍历列中的所有单元格并在每个数字+点字符后添加一个空行
单元格中的示例数据:
1. 文本A2。正文B3。文本C4。文本D5。文本E
脚本后所需的输出:
1.文本A
2.文本B
3.文本C
4.文本D
5.文本E
谢谢大家
希望对您有所帮助:
Sub parsingText()
Dim a 'to store the parsed text
Dim t
Dim myAscii As Integer
a = Split(Range("A1"), ".")
For t = 1 To UBound(a)
myAscii = Asc(Right(a(t), 1)) 'ASCII code of the last char
If myAscii >= 48 And myAscii <= 57 Then 'the 0-9 numbers are 48-57 ASCII codes
Cells(t, 2).Value = t & ". " & Left(a(t), Len(a(t)) - Len(t)) 'Take just the text and replace the index counter
Else
Cells(t, 2).Value = t & ". " & a(t) 'If the last text has no index (as the 5th)
End If
Next t
结束子
注意:在Cells(t, 2).Value
我用的是B栏,你可以随便抓取其他的。并且文本始终具有相同的格式 number[.][space][text]
RANGE A1: 1. textA2. textB3. textC4. textD5. textE
结果:
Range B1: 1. textA
Range B2: 2. textB
Range B3: 3. textC
Range B4: 4. textD
Range B5: 5. textE
新编辑:如您在评论中所问
为源数据添加大范围。
Sub parsingText()
Dim a 'to store the parsed text
Dim t
Dim myAscii As Integer
Dim rng As Range
Dim R
Set rng = Range("A1:A50")
For Each R In rng
a = Split(R, ".")
For t = 1 To UBound(a)
myAscii = Asc(Right(a(t), 1)) 'ASCII code of the last char
If t = 1 Then
If myAscii >= 48 And myAscii <= 57 Then 'the 0-9 numbers are 48-57 ASCII codes
Cells(R.Row, 2).Value = t & ". " & Left(a(t), Len(a(t)) - Len(t)) 'Take just the text and replace the index counter
Else
Cells(R.Row, 2).Value = t & ". " & a(t) 'If the last text has no index (as the 5th)
End If
Else
If myAscii >= 48 And myAscii <= 57 Then 'the 0-9 numbers are 48-57 ASCII codes
Cells(R.Row, 2).Value = Cells(1, 2).Value & vbCrLf & t & ". " & Left(a(t), Len(a(t)) - Len(t + 1)) 'Take just the text and replace the index counter
'Here the update.
Else
Cells(R.Row, 2).Value = Cells(1, 2).Value & vbCrLf & t & ". " & a(t) 'If the last text has no index (as the 5th)
End If
End If
Next t
Next R
End Sub
希望您能帮助我解决困扰我一段时间的问题。我太菜鸟了,无法自己解决这个问题,所以这里是:
有没有办法让VBA遍历列中的所有单元格并在每个数字+点字符后添加一个空行
单元格中的示例数据: 1. 文本A2。正文B3。文本C4。文本D5。文本E
脚本后所需的输出:
1.文本A
2.文本B
3.文本C
4.文本D
5.文本E
谢谢大家
希望对您有所帮助:
Sub parsingText()
Dim a 'to store the parsed text
Dim t
Dim myAscii As Integer
a = Split(Range("A1"), ".")
For t = 1 To UBound(a)
myAscii = Asc(Right(a(t), 1)) 'ASCII code of the last char
If myAscii >= 48 And myAscii <= 57 Then 'the 0-9 numbers are 48-57 ASCII codes
Cells(t, 2).Value = t & ". " & Left(a(t), Len(a(t)) - Len(t)) 'Take just the text and replace the index counter
Else
Cells(t, 2).Value = t & ". " & a(t) 'If the last text has no index (as the 5th)
End If
Next t
结束子
注意:在Cells(t, 2).Value
我用的是B栏,你可以随便抓取其他的。并且文本始终具有相同的格式 number[.][space][text]
RANGE A1: 1. textA2. textB3. textC4. textD5. textE
结果:
Range B1: 1. textA
Range B2: 2. textB
Range B3: 3. textC
Range B4: 4. textD
Range B5: 5. textE
新编辑:如您在评论中所问
为源数据添加大范围。
Sub parsingText()
Dim a 'to store the parsed text
Dim t
Dim myAscii As Integer
Dim rng As Range
Dim R
Set rng = Range("A1:A50")
For Each R In rng
a = Split(R, ".")
For t = 1 To UBound(a)
myAscii = Asc(Right(a(t), 1)) 'ASCII code of the last char
If t = 1 Then
If myAscii >= 48 And myAscii <= 57 Then 'the 0-9 numbers are 48-57 ASCII codes
Cells(R.Row, 2).Value = t & ". " & Left(a(t), Len(a(t)) - Len(t)) 'Take just the text and replace the index counter
Else
Cells(R.Row, 2).Value = t & ". " & a(t) 'If the last text has no index (as the 5th)
End If
Else
If myAscii >= 48 And myAscii <= 57 Then 'the 0-9 numbers are 48-57 ASCII codes
Cells(R.Row, 2).Value = Cells(1, 2).Value & vbCrLf & t & ". " & Left(a(t), Len(a(t)) - Len(t + 1)) 'Take just the text and replace the index counter
'Here the update.
Else
Cells(R.Row, 2).Value = Cells(1, 2).Value & vbCrLf & t & ". " & a(t) 'If the last text has no index (as the 5th)
End If
End If
Next t
Next R
End Sub