首页 > 解决方案 > Excel 宏脚本停止工作,运行时显示忙碌的鼠标图标,但对电子表格没有任何作用

问题描述

我正在使用这个宏来清理节目标题并将季节和剧集编号排序到他们自己的列中。效果很好!

我已经有大约 6 个月没有运行它了,现在当我打开电子表格并尝试运行它时,它只是以一个忙碌的圆圈旋转。

我完全不知道什么会导致这个宏不起作用。自从我上次运行它以来,我没有对代码进行任何更改并且它确实有效。

Excel 2016

我已经检查了参考资料,并且相当确定我已经包含了所有必要的参考资料。

参考资料:https ://i.imgur.com/m4YCGAP.png

我尝试单步执行 (F8) 宏以找出问题所在,但似乎没有一个宏在运行,尽管它正在运行。

Sub cleanTitles()

Dim strTitle As String, regEx As New RegExp, strPattern As String
Dim strSeason As String, strEpisode As String, strYear As String

With regEx
.Global = True

Dim j As Integer: j = 2
While Not Range("A" & j).Value = vbNullString

If Range("F" & j).Value = vbNullString Then

strTitle = Range("A" & j).Value

.Pattern = "[Ss]\s?(\d{1,2})[xX\s]?[Ee]\s?(\d{1,2}).*"
Set Matches = .Execute(strTitle)
If Matches.Count = 0 Then
.Pattern = "[\W_ ](\d{1,2})\s?[xX]\s?(\d{1,2}).*"
Set Matches = .Execute(strTitle)
End If
If Matches.Count = 0 Then
    Range("F" & j) = vbNullString
    Range("G" & j) = vbNullString
Else
    strSeason = Matches(0).SubMatches(0)
    strEpisode = Matches(0).SubMatches(1)
    Range("F" & j) = strSeason
    Range("G" & j) = strEpisode
    strTitle = .Replace(strTitle, vbNullString)
End If

.Global = True
.Pattern = "\{[^\}]*\}[\W_]?"
Set Matches = .Execute(strTitle)
If Matches.Count <> 0 Then
    strTitle = .Replace(strTitle, vbNullString)
End If
.Pattern = "\[[^\]]*\][\W_]?"
Set Matches = .Execute(strTitle)
If Matches.Count <> 0 Then
    strTitle = .Replace(strTitle, vbNullString)
End If
.Pattern = "\([^\)]*\)[\W_]?"
Set Matches = .Execute(strTitle)
If Matches.Count <> 0 Then
    strTitle = .Replace(strTitle, vbNullString)
End If
.Global = False

.Pattern = "[\W_](\d{4}).*"
Set Matches = .Execute(strTitle)
If Matches.Count <> 0 Then
    strYear = Matches(0).SubMatches(0)
    If strYear <= "1920" Then
        Range("F" & j) = Left(strYear, 2)
        Range("G" & j) = Right(strYear, 2)
    End If
    strTitle = .Replace(strTitle, vbNullString)
End If

.Pattern = "(\d{4})$"
Set Matches = .Execute(strTitle)
If Matches.Count <> 0 Then
    strTitle = .Replace(strTitle, vbNullString)
End If

.Pattern = "^\d+-?([a-zA-Z])"
Set Matches = .Execute(strTitle)
If Matches.Count <> 0 Then
    strTitle = .Replace(strTitle, Matches(0).SubMatches(0))
End If

.Global = True
.Pattern = "([a-z0-9])([A-Z])"
Set Matches = .Execute(strTitle)
strTitle = .Replace(strTitle, "$1" & " " & "$2")
.Pattern = "([a-zA-Z])([0-9])"
Set Matches = .Execute(strTitle)
strTitle = .Replace(strTitle, "$1" & " " & "$2")
.Global = False


Range("A" & j) = IIf(strTitle <> vbNullString, strTitle, ".")

End If

j = j + 1
Wend

End With

End Sub

--- 要清理的示例节目标题 ---生活
大爆炸第 10 季第 15 集:运动混响生活
大爆炸第 10 季第 16 集:津贴蒸发
100 第 3 季第 2 集:万和达:第二部分 生活
大爆炸理论第10季第19集:合作波动
第100季第3季第3集:你进入这里
的好医生第1季第12集:岛屿第二部分

该宏应该从A列中删除诸如“Season”或“Episode”之类的文本,然后将相应的数字添加到F和G列
中。它还应该删除诸如“:”之类的标点符号

示例结果,忽略未突出显示的列https://i.imgur.com/deTNHBs.png

标签: excelvba

解决方案


推荐阅读