1

我是 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 是我跌倒的地方。

4

2 回答 2

0

如果 B15:M34 中的每个单元格都应该是非空白的,那么你可以这样做:

If Application.CountBlank(InputRange)>0 Then
    If Msgbox(Msg, vbYesNo) = vbYes Then
        'rest of your code
    End If
End If

编辑:这将根据相应的标题单元格检查每个数据单元格。

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, rw As Range
    Dim HeaderRange As Range
    Dim x As Long, Msg As String

    Set ws = Worksheets("PreparationData")
    Set WS1 = Worksheets("ColdWeatherPreparation")
    Set HeaderRange = WS1.Range("B14:M14")
    Set InputRange = WS1.Range("B15:M34")

    'are you sure about this next line?
    'once validation has failed once how does it re-run?
    If AbortProc Then Exit Sub

    For Each rw In InputRange.Rows

        For x = 1 To rw.Cells.Count
            If Len(rw.Cells(x).Value) = 0 And _
               Len(Headerange.Cells(x).Value) > 0 Then

                Msg = "All fields are not populated. Stop submission" & _
                       " to resume editing?"

                If MsgBox(Msg, vbYesNo) = vbYes Then
                    AbortProc = True
                    Exit Sub
                Else
                    MsgBox "Run without Correcting?"
                    AbortProc = False
                    Exit Sub
                End If
            End If
        Next x

    Next rw

    'more code here that seems to be working
End Sub
于 2012-08-22T16:27:25.783 回答
0

Len 线上的错误?也许是因为 Cells 有 2 个参数?Cells(RowIndex,ColumnIndex).

此外,您可以通过以下方式设置 LastColumn:

LastColumn = ActiveSheet.UsedRange.Columns.Count

可以对行做同样的事情:

LastRow = ActiveSheet.UsedRange.Rows.Count

也许你应该If AbortProc Then Exit Sub进入 For 循环(作为第一行/最后一行)

于 2012-08-23T13:52:12.187 回答