【问题标题】:Nested for...each loops with If statements嵌套 for...each 带有 If 语句的循环
【发布时间】:2014-10-28 15:14:02
【问题描述】:

我是 vb.script 的新手,所以这可能只是一个我找不到答案的格式问题。 问题在于验证同一工作簿中不同工作表上的单元格中的数据。

遍历工作表,然后遍历单元格区域:

Private Sub Validate(ByRef objWorkbook As Workbook)

Dim strPattern As String: strPattern = "^\-{0,1}\d+(.\d+){0,1}$"
Dim regEx As New VBScript_RegExp_55.RegExp
Dim strInput As String
Dim strOutput As String
Dim Myrange As Range
Dim regExCount As Object
Set regExCount = CreateObject("vbscript.regexp")
On Error Resume Next

For Each objWorksheet In objWorkbook.Worksheets
    If (UCase(objWorksheet.Name) = "Foo") Then
        objWorksheet.Select
        Range("Q2").Select

    ElseIf (UCase(objWorksheet.Name) = "Bar") Or (UCase(objWorksheet.Name) = "Poo") Then
        objWorksheet.Select
        Set Myrange = ActiveSheet.Range("D51:AA76")

        For Each cell In Myrange.Cells
            If strPattern <> "" Then
            strInput = cell.Value
            strReplace = ""

            With regEx
                .Global = True
                .MultiLine = True
                .IgnoreCase = False
                .Pattern = strPattern
            End With

            Set regExCount = regEx.Execute(strInput)
            If regExCount.Count = 0 Then
                strOutput = strOutput + "Illegal character at " + cell.AddressLocal + "\r\n"
            End If
        Next cell
    End If

Next
MsgBox (strOutput)

End Sub

当我编译时,我在 Next Cell 处得到一个没有 For 循环的 Next 错误。删除该行,如果没有突出显示 End Sub 的 End Ff,我会收到 Block If 错误。在 End 子之前添加一个 End If,我得到一个没有 For 错误的 Next。

【问题讨论】:

    标签: excel vbscript vba


    【解决方案1】:

    这块不是缺了if就结束了吗?

            If strPattern <> "" Then
                strInput = cell.Value
                strReplace = ""
    

    【讨论】:

      【解决方案2】:

      你的

      If strPattern <> "" Then
      

      之间没有闭合

          End If ' regExCount.Count
      Next cell ' In Myrange.Cells
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-13
        • 1970-01-01
        相关资源
        最近更新 更多