我是 VBA 的新手,并且是基于其他人的代码构建的,他对 VBA 的了解比我还新!提前感谢您提供的任何提示和建议。
由于我无法发布图像,我将尝试描述数据集。数据来自用户表单,大部分内容在表格范围 A14:M34 中,A 列中包含问题,BM 列中包含数据。第一行是用户填充的标题,用于标识检查的单元。下面的数据填充了带有空白、Yes 和 NO 作为选项的下拉列表,以及带有数字或字符串的几行。
我想在不同大小的范围内测试每个单元格是否有未回答的问题,并通知用户是否有任何问题,并让他们选择在提交之前完成数据集。
Sub new_p()
Static AbortProc As Boolean
Dim iRow As Long
Dim LastColumn As Long
Dim aCol As Long
Dim ws As Worksheet, WS1 As Worksheet
Dim InputRange As Range
Set ws = Worksheets("PreparationData")
Set WS1 = Worksheets("ColdWeatherPreparation")
Set InputRange = WS1.Range("B15:M34")
If AbortProc Then Exit Sub
'find last column in range
LastColumn = WS1.Cells(14, 2).End(xlToRight).Column
'define variable range of columns
For aCol = 2 To LastColumn
'check that the circuit row is not blank
'If Cells(14, aCol) Is Not Nothing Then
If IsEmpty(InputRange) Then
Msg = "All fields are not populated. Stop submission to resume editing?"
Ans = MsgBox(Msg, vbYesNo)
'if yes stop process
If Ans = vbYes Then
AbortProc = True
Exit Sub
End If
'if no run rest of script
If Ans = vbNo Then
MsgBox "Run without Correcting?"
AbortProc = False
Exit Sub
End If
End If
'End If
Next
'more code here that seems to be working
End Sub
你会看到我已经注释掉了我认为多余的一行。如果 End(xlToRight) 生成标题行的最后填充列,则它们不是空白的,因此无需测试。尽管如此,我会保留我不使用的代码,直到最终检查完成并且它被证明是完全无用的。过多的评论是为了帮助一大群非 VBA 工作人员在实施之前跟踪并验证我的代码。
所以 LastColumn 的定义似乎起作用了,我稍后再使用它。当我单步执行代码时,它会为我的虚假数据集循环正确的次数。我觉得 isEmpty 是我跌倒的地方。