在不同的工作表中和通过表单控件执行模块时,结果不同

时间:2019-06-21 13:04:53

标签: excel vba

我必须纠正一些模块的问题,以检查是否具有固定工作表名称和位置的excel文件,但是不知道行数在强制记录中是否有空字段。它应该通过单独页面中的按钮执行。但是,该模块会根据执行的位置给出不同的结果,甚至从空白表执行时也会出错(仅读取按钮)。另一个要求是他们可以单独执行所有操作

除了大学课程外,我还是excel宏的新手。我尝试的大部分是来自互联网

Sub Required_Fields()
'main sub
Call Required_Fields_DM
Call Required_Fields_EX
Call Required_Fields_SM
Call Required_Fields_TA
Call Required_Fields_TE
Call Required_Fields_TS
End Sub

Sub Required_Fields_DM()
Dim Dataset As Range
Dim lRow As Long
lRow = Range_Find_Method("DOMAIN DM")
'function to determine last row as otherwise the row after the data is also marked
Set Dataset = Worksheets("DOMAIN DM").Range("A5:C" & lRow & ",L5:L" & lRow)
On Error Resume Next
Dataset.SpecialCells(xlCellTypeBlanks).Interior.Color = vbRed
If Dataset Is Nothing Then
   Exit Sub
End If
End Sub

Sub Required_Fields_EX()
Dim Dataset As Range
Dim lRow As Long
lRow = Range_Find_Method("DOMAIN EX")
Set Dataset = Worksheets("DOMAIN EX").Range("A5:C" & lRow)
On Error Resume Next
Dataset.SpecialCells(xlCellTypeBlanks).Interior.Color = vbRed
If Dataset Is Nothing Then
   Exit Sub
End If
End Sub

Sub Required_Fields_SM()
Dim Dataset As Range
Dim lRow As Long
lRow = Range_Find_Method("DOMAIN SM")
Set Dataset = Worksheets("DOMAIN SM").Range("A5:F" & lRow)
On Error Resume Next
Dataset.SpecialCells(xlCellTypeBlanks).Interior.Color = vbRed
If Dataset Is Nothing Then
   Exit Sub
End If
End Sub

Sub Required_Fields_TA()
Dim Dataset As Range
Dim lRow As Long
lRow = Range_Find_Method("DOMAIN TA")
Set Dataset = Worksheets("DOMAIN TA").Range("A5:E" & lRow)
On Error Resume Next
Dataset.SpecialCells(xlCellTypeBlanks).Interior.Color = vbRed
If Dataset Is Nothing Then
   Exit Sub
End If
End Sub

Sub Required_Fields_TE()
Dim Dataset As Range
Dim lRow As Long
lRow = Range_Find_Method("DOMAIN TE")
Set Dataset = Worksheets("DOMAIN TE").Range("A5:C" & lRow)
On Error Resume Next
Dataset.SpecialCells(xlCellTypeBlanks).Interior.Color = vbRed
If Dataset Is Nothing Then
   Exit Sub
End If
End Sub

Sub Required_Fields_TS()
Dim Dataset As Range
Dim lRow As Long
lRow = Range_Find_Method("DOMAIN TS")
Set Dataset = Worksheets("DOMAIN TS").Range("A5:B" & lRow & ",I5:I" & lRow & ",N5:N" & lRow)
On Error Resume Next
Dataset.SpecialCells(xlCellTypeBlanks).Interior.Color = vbRed
If Dataset Is Nothing Then
   Exit Sub
End If
End Sub

Private Function Range_Find_Method(sheetname As String) As Long
'Finds the last non-blank cell on a sheet/range.

Dim lRow As Long

    lRow = Cells.Find(What:="*", _
                    After:=Worksheets(sheetname).Range("A1"), _
                    LookAt:=xlPart, _
                    LookIn:=xlFormulas, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious, _
                    MatchCase:=False).Row
 '* is for any type of value; xlPart searches for one part in the cell; xlFormulas searches in formulas, meaning a formula that returns an empty field will also be picked up; xlprevious defines the search (right-> left)

Range_Find_Method = lRow

End Function

预期:无论执行的方式和位置如何,结果都是相同的。

如果它从任何包含数据的工作表中执行,它将按预期工作。如果它是从控制表运行的,它也会在前4行中标记单元格。控制表中还必须至少有一条记录,以防止函数中出现“对象变量或未设置块变量的错误”

0 个答案:

没有答案