Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VB宏值从一列到下一列的字母数字值_Excel_Vba - Fatal编程技术网

Excel VB宏值从一列到下一列的字母数字值

Excel VB宏值从一列到下一列的字母数字值,excel,vba,Excel,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, ]"

这适用于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

您只需要将For循环嵌套在通过每个单元格的另一个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

这个循环只对结果起作用,我需要的是:带特殊字符的A1到只有字母数字的B1。你在这段代码上做的循环是用特殊字符将A1转换成B1:B100字母数字信息。它不是。单元格(r,3)采用第r行第3列。然后我将输出给单元格(r,4),即第r行第4列。所以它是C1>D1 C2>D2 C3>D3等等。我还没有测试过它,但我不明白为什么它不起作用。它似乎在这样做:A1>B1;A1,A2>B2;A1、A2、A3>B3;A1、A2、A3、A4>B4。。。等等,啊哈。正确的。在再次循环之前,您需要清空c:)我会更正上面的代码,很抱歉。我无法测试代码(讨厌Mac上的Excel),我很困惑,我在添加c+1时认为这就是问题所在。幸好你能很快弄明白这一点。你应该避免声明像
dima$、b$、c$
这样的变量。你可能想看看@SiddharthRout,这个答案只是解释了它们的符号。作为一个在GWBASIC/MSBASIC环境中长大的人,美元的使用对我来说是显而易见的。对于新用户来说,符号的使用被贬低了,因此看到类似于
q#=c@/i%*x&
(并不是说我写的代码是天真无邪的)@seanshire:
的代码可能会让人感到困惑,因为答案只是解释了符号是什么。
No:)它还做了其他事情:)