VBA - 删除字符

时间:2015-11-15 06:55:01

标签: excel vba excel-vba

我有一个工作簿,其中包含一列(" M"或者#34; 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..<br>

我的客户使用的旧索引命名法从1.1到3.3

我需要刮掉这一栏并删除所有这三个数字,使得它们之后的任何文字仍然出现在单元格内。

例如:

Residential - Bla bla bla<br>
Residential:  Bla bla bla<br>
Residential  Bla bla bla<br>
Residential  Bla bla bla<br>
Multi Residential  Bla bla bla<br>
etc..

2 个答案:

答案 0 :(得分:0)

尝试此操作,它会删除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的值的单元格回答,那么您可以尝试这样做:

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

答案 1 :(得分:0)

或者,以下代码将遍历第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