我是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
当我编译时,我在下一个单元格中没有For循环而得到Next的错误。删除该行,如果没有End Ff突出显示End Sub,我会收到Block的错误。添加结束如果在结束子之前,我得到一个没有For错误的Next。
答案 0 :(得分:1)
不是这个区块丢失并且结束了吗?
If strPattern <> "" Then
strInput = cell.Value
strReplace = ""
答案 1 :(得分:0)
您
If strPattern <> "" Then
之间没有关闭
End If ' regExCount.Count
Next cell ' In Myrange.Cells