简体   繁体   English

宏中的Nothing会使Excel 2013崩溃

[英]`Nothing` in macro crashes Excel 2013

I'm trying to use RegEx in an Excel 2015 macro. 我正在尝试在Excel 2015宏中使用RegEx。 I don't know if I'm doing something wrong, but every time I run it, my Excel crashes. 我不知道我做错了什么,但是每次运行它时,Excel都会崩溃。 Here's the macro: 这是宏:

Sub MakeExplicit()

    Dim whitespace As RegExp
    Set whitespace = New RegExp
    whitespace.Pattern = "\s+"
    whitespace.MultiLine = True
    whitespace.Global = True

    Dim implicit As RegExp
    Set implicit = New RegExp
    implicit.Pattern = "^\d+-\d+$"

    Dim row As range

    For Each row In ActiveSheet.UsedRange.Rows

        Dim first As range
        Set first = row.Cells(1, 1)

        Dim str As String
        str = first.Text
        str = whitespace.Replace(str, Nothing)

        If implicit.Test(str) Then 'FIXME here it crashes

            Dim FromTo As Variant
            FromTo = Split(str, "-")

            Dim sFrom, sTo As Integer
            sFrom = FromTo(1)
            sTo = FromTo(2)

            ' doplň chybějící číslice
            ' např [2345, 78] doplní
            ' na [2345, 2378]
            sTo = Left( _
                sFrom, _
                Len(sFrom) - Len(sTo) _
            ) + sTo

            Dim iFrom, iTo As Integer
            iFrom = CInt(sFrom)
            iTo = CInt(sTo)

            If iFrom > iTo Then _
                Err.Raise 42, first.Address, _
                "Wrong order of numbers!"


            Dim i As Integer
            For i = iFrom To iTo
                ' some more code
            Next i

        End If

    Next row

End Sub

By using the Debugger I found out it crashes when the code reaches " If implicit.Test(str) Then " implying there's something wrong with RegEx. 通过使用调试器,我发现当代码到达“ If implicit.Test(str) Then ”时,它崩溃了,这表明RegEx有问题。 These are the project's references: 这些是项目的参考:

包含Microsoft VBScript正则表达式5.5

The obvious question is how do I get it to work? 显而易见的问题是如何使它工作? VBA is a very ugly language by itself, so I have no preference about how , just making it work is enough. VBA本身就是一种非常丑陋的语言,因此我不喜欢如何工作,仅使其工作就足够了。

this is the line that crashes 这是崩溃的行

str = whitespace.Replace(str, Nothing)

Nothing is used for destroying objects ... set object = nothing NothingNothing用来破坏对象... set object = nothing

use instead 用代替

str = whitespace.Replace(str, "")

or, as per Mat's Mug 或者,根据Mat的杯子

str = whitespace.Replace(str, vbNullString)    ' uses less memory and is more readable

A few things... 一些东西...

1) The line If implicit.Test(str) Then should not cause an error. 1)该行If implicit.Test(str) Then不应导致错误。

2) To replace one or more spaces with no spaces, use "" instead of Nothing ... 2)要用一个空格替换一个或多个空格,请使用""代替Nothing ...

str = whitespace.Replace(str, "")

3) Since the Split function returns a 0-based array, use... 3)由于Split函数返回一个从0开始的数组,因此请使用...

sFrom = FromTo(0)
sTo = FromTo(1)

4) To concatenate, use the ampersand (&) instead of the plus sign (+)... 4)要串联,请使用&符号代替加号(+)。

    sTo = Left( _
        sFrom, _
        Len(sFrom) - Len(sTo) _
    ) & sTo

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM