Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.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
RegExp55修改大量Excel公式_Regex_Excel_Vba_Excel Formula - Fatal编程技术网

RegExp55修改大量Excel公式

RegExp55修改大量Excel公式,regex,excel,vba,excel-formula,Regex,Excel,Vba,Excel Formula,我在Excel中有以下电子表格: +---+----+-----------------------------+----------------------------+ | | A | B | C | +---+----+-----------------------------+----------------------------+ | 1 | |

我在Excel中有以下电子表格:

+---+----+-----------------------------+----------------------------+
|   | A  |              B              |             C              |
+---+----+-----------------------------+----------------------------+
| 1 |    |                             |                            |
| 2 | 12 | =IF(ISERROR(A2/0),"",A2/0)  | =IF(ISERROR(A2*4),"",A2*4) |
+---+----+-----------------------------+----------------------------+
我想得到以下信息

+---+----+--------+-------+
|   | A  |   B    |   C   |
+---+----+--------+-------+
| 1 |    |        |       |
| 2 | 12 | =A2/0  | =A2*4 |
+---+----+--------+-------+
所以我写了这个VBA代码:

Sub DeleteIfError()


    Dim c As Integer
    Dim r As Integer
    Dim regex As Object, str As String
    Set regex = CreateObject("VBScript.RegExp")

    With regex
    .Pattern = "=IF(ISERROR\([A-Za-z0-9]+\)"
    .Global = False 'Only First
    End With

For c = 1 To 3
    For r = 1 To 2
        If Cells(r, c).HasFormula Then

            Set matches = regex.Execute(str)
                For Each Match In matches
                    Cells(r, c) = Match.Value
                Next Match
        End If

    Next r
Next c

End Sub
但它给了我一个运行时错误5020。
我认为问题在模式中,但我真的不明白如何解决它。有人能帮我吗?

您可以使用以下修复程序:

1) 正则表达式必须是
^=IF\(iError\([^)]+).
,替换模式应设置为
=$1
(请参见)
2) 您需要使用
.Replace
而不是
。执行
以替换公式
3) 传递的字符串必须是公式,更新后的字符串应分配给单元格公式

正则表达式匹配:

  • ^
    -字符串的开头
  • =IF\(ISERROR\)(
    -文字字符序列
    =IF(ISERROR)(
  • ([^)]+)
    -捕获组1(稍后用
    $1
    引用)匹配1+字符,而不是
  • \)
    -文字
  • *
    -该行的其余部分一直到其末尾
代码:


您似乎错过了第一个左括号的转义?您在哪里为
str
赋值?@WiktorStribiżew是对的,您似乎错过了最重要的部分:
str=Cells(r,c)。在执行正则表达式之前,请使用公式
!在这之后,它应该是:
单元格(r,c)。Formula=Match.Value
(因为在模式中有
=
,所以只有一个匹配,所以循环不是很有用)好吧,我认为正则表达式也做错了:
.pattern=“=IF\(iError\([^)]+).*”
amd然后是
单元格(r,c)。Formula=regex.Replace(str,=$1)
(参见)谢谢,我一直在为这件事“伤”我的头,谢谢你的详细解释刚刚注意到我复制/粘贴了一些我的旧
到代码片段中:)删除了。为什么在正则表达式中使用
[a-zA-Z](?==IF\(ISERROR\()
不会返回
“=IF(ISERROR)(”之后的第一个字符
,意思是
“A”
?@ShaiRado:
A
后面不跟
=IF
,而是跟在它前面。在
regex.Execute
中使用的模式应该是
=IF\(iError\([A-Za-z])
。然后它看起来是这样的:1)用
=IF\(iError提取中间的所有子字符串((^[)+)
使用
regex模式。执行
然后访问第一个匹配的
子匹配(0)
,2)拆分或仅循环子字符串中的字符,检查它们是否为alpha字符。
Sub DeleteIfError()
    Dim c As Integer
    Dim r As Integer
    Dim regex As Object, str As String
    Set regex = CreateObject("VBScript.RegExp")

    With regex
    .pattern = "^=IF\(ISERROR\(([^)]+)\).*"
    .Global = False 'Only First
    End With

For c = 1 To 3
    For r = 1 To 2
        If Cells(r, c).HasFormula Then
             Cells(r, c).Formula = regex.Replace(Cells(r, c).Formula, "=$1")
        End If

    Next r
Next c

End Sub