【发布时间】: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。
【问题讨论】: