Excel VBA-Macro值从一列到下一列的只有字母数字值

时间:2013-10-22 16:42:34

标签: excel excel-vba vba

这适用于EXCEL Visual Basic宏。

我需要将所有B列数据更改为C列上只有字母数字字符。

我的代码仅适用于一个单元格。我想为每个活动单元循环此代码。有人可以帮我吗?

Sub macroalphanum()
    Dim a$, b$, c$, i As Integer
    a$ = Range("C1").Value
    For i = 1 To Len(a$)
       b$ = Mid(a$, i, 1)
    If b$ Like "[A-Z,a-z,0-9, ]" Then
        c$ = c$ & b$
    End If
    Next i
    Range("D1").Value = c$
End Sub

1 个答案:

答案 0 :(得分:2)

您只需将For Loop嵌套在另一个遍历每个单元格的For循环中

快速示例:

Sub macroalphanum()
    Dim a$, b$, c$, i As Integer, r as Integer
    For r=1 to 100 step 1  ' This will do it for all rows from 1 to 100. Change it to suit your needs
       a$ = Cells(r,3).Value
       c$ = "" ' (Re)Initialize c
       For i = 1 To Len(a$)
          b$ = Mid(a$, i, 1)
          If b$ Like "[A-Z,a-z,0-9, ]" Then
              c$ = c$ & b$
          End If
       Next i
    Cells(r,4).Value = c$
    Next r
End Sub