VBA-删除字符、数字
我有一个工作簿,其中的一列(“M”或“13”)如下所示:VBA-删除字符、数字,vba,excel,Vba,Excel,我有一个工作簿,其中的一列(“M”或“13”)如下所示: 1.1 Residential - Bla bla bla<br> 1.1 Residential: Bla bla bla<br> 1.2 - Residential Bla bla bla<br> 2.2 - 1.3 Residential Bla bla bla<br> 3.1 - Multi Residential Bla bla bla<br> etc..&l
1.1 Residential - Bla bla bla<br>
1.1 Residential: Bla bla bla<br>
1.2 - Residential Bla bla bla<br>
2.2 - 1.3 Residential Bla bla bla<br>
3.1 - Multi Residential Bla bla bla<br>
etc..<br>
1.1住宅-Bla Bla Bla
1.1住宅:布拉布拉布拉布拉布拉
1.2-住宅Bla Bla Bla
2.2-1.3住宅Bla Bla Bla
3.1-多住宅Bla Bla Bla
等等…
我的客户使用的旧索引术语从1.1变为3.3
我需要刮除此列并去掉所有这三个数字,使它们后面的任何文本仍然显示在单元格中。例如:
Residential-Bla-Bla-Bla
住宅:布拉布拉布拉布拉布拉
住宅区Bla Bla Bla
住宅区Bla Bla Bla
多人居住区Bla Bla Bla
等
试试这个,它会删除M列中每个单元格开头的数字、句点、连字符和空格。我只做了5个,但将其更改为任意数量的单元格:
Dim i As Integer
For i = 1 To 5
Dim j As Integer
Dim value As String
value = Range("M" & i)
For j = 1 To Len(value)
Select Case Mid(value, j, 1)
Case 0 To 9
Case "-"
Case "."
Case " "
Case Else
Exit For
End Select
Next
Range("M" & i) = Mid(value, j, Len(value))
Next
如果您不想担心指定单元格的数量,而是让所有包含值(如Jonnus answer)的单元格都这样做,那么您可以尝试以下方法:
Dim i As Integer
i = 1
Do Until Range("M" & i) = ""
Dim j As Integer
Dim value As String
value = Range("M" & i)
For j = 1 To Len(value)
Select Case Mid(value, j, 1)
Case 0 To 9
Case "-"
Case "."
Case " "
Case Else
Exit For
End Select
Next
Range("N" & i) = Mid(value, j, Len(value))
i = i + 1
Loop
或者,下面的代码将在第13列中循环,依次查看每个单元格,找到第一个字母字符,并去掉它前面的所有字符
Dim rowNum As Integer
Dim curValue as String
RowNum = 1
' Loop though all cells in the column until a blank one is found
Do While Cells(13, rowNum).Value <> ""
' Get the current value of the cell
curValue = Cells(13, rowNum).Value
' Loop through all the characters until the first alpha character is
' found, and save the index of this first character
Dim x as Long
For x = 1 to Len(curValue)
sChar = Mid(curValue, x, 1)
If sChar Like "[a-zA-z\)\(\[\]]" Then
firstChar = x
Exit For
End If
Next x
' Set the value of the cell to be the current value starting from
' the first alpha character
Cells(13, rowNum).Value = Right(curValue, Len(curValue) - x)
' Go to the next row
rowNum = rowNum + 1
Loop
Dim rowNum作为整数
作为字符串的暗淡曲线值
RowNum=1
'循环遍历列中的所有单元格,直到找到空白单元格
Do While单元格(13,rowNum)。值“”
'获取单元格的当前值
曲线值=单元格(13,rowNum).Value
'循环遍历所有字符,直到找到第一个字母字符
'找到,并保存第一个字符的索引
暗x等长
对于x=1到Len(曲线值)
sChar=中间(曲线值,x,1)
如果沙尔喜欢“[a-zA-z\)\(\[\]]”,那么
firstChar=x
退出
如果结束
下一个x
'将单元格的值设置为从
'第一个字母字符
单元格(13,rowNum).Value=Right(曲线值,Len(曲线值)-x)
“到下一排去
rowNum=rowNum+1
环
你已经试过哪些代码了?正如我提到的Jonnus,我是VBA的新手。我在这里试过很多社区的例子。唯一的问题是绝大多数都是用于文本的。我真的迷路了。使用Value”“
这个主意很酷。这对我的答案是一个很好的增强。至于像“[a-zA-Z]”这样的“
,这是一个小小的假设。它只适用于英语语言,文本不能以任何其他内容(如括号)开头。我已更新了边以包括方括号和大括号().用谷歌搜索正则表达式来找到一些有用的东西,包括非英语字符,因为在不知道你的确切要求的情况下很难提出一些建议。@RacilHilan,我再次尝试了你的代码,它成功了!你是摇滚乐的伙伴。Jonnus,非常感谢你,伙计。我最棒的干杯。太棒了。我是wai我很高兴你已经做到了。请更新投票/标记作为答案,以帮助将来可能会有相同问题的人。我最棒的干杯们。上面的代码块不会产生任何错误。但是,所有1.1、1.2、…3.3等仍然存在,没有任何变化。我认为re肯定是另外一回事。:/我用Excel处理了你的样本数据,效果很好。你说你“又”尝试了乔纳斯的答案,结果成功了。我的答案几乎一样,应该对你有用,所以再试试。如果你愿意,你可以使用他的答案,但我的答案适用于任何语言(不仅仅是英语)和符号,以防你们的文字以其中一个开头。另外,我的答案运行得稍快一些,因为我并没有使用like®ex。我在@racilhilan更正了我的评论。事实上,是你们的台词对我起了作用。我相应地投了赞成票。(抱歉,仍然掌握了这个窍门)。干杯。
Dim rowNum As Integer
Dim curValue as String
RowNum = 1
' Loop though all cells in the column until a blank one is found
Do While Cells(13, rowNum).Value <> ""
' Get the current value of the cell
curValue = Cells(13, rowNum).Value
' Loop through all the characters until the first alpha character is
' found, and save the index of this first character
Dim x as Long
For x = 1 to Len(curValue)
sChar = Mid(curValue, x, 1)
If sChar Like "[a-zA-z\)\(\[\]]" Then
firstChar = x
Exit For
End If
Next x
' Set the value of the cell to be the current value starting from
' the first alpha character
Cells(13, rowNum).Value = Right(curValue, Len(curValue) - x)
' Go to the next row
rowNum = rowNum + 1
Loop