0

我做了一个子。但它给出了一个错误。!我想检查 A 列是否有值,然后检查列“H 和 I”。这些必须填写。否则文件不会保存..!!

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim cell As Range
    Dim LastRow As Long
    Dim oneRange As Range
    Dim aCell As Range

    LastRow = Cells(Rows.Count, "A").End(xlUp).Row

    If cell.Value(Range("A8:A" & LastRow)) Is Nothing Then
        Exit Sub
    Else
        For Each cell In Range("H8:I" & LastRow)
            If IsEmpty(cell.Value) Then
                MsgBox "Please Select Value from Dropdown Menu...." & cell.Address
                Application.Goto cell
                Cancel = True
                Exit For
            End If
        Next cell

    End If
End Sub
4

2 回答 2

1

久经考验

这是你正在尝试的吗?

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim ws As Worksheet
    Dim lRow As Long
    Dim aCell As Range

    Set ws = ThisWorkbook.Sheets("receivings")

    With ws
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row

        If Application.WorksheetFunction.CountA(.Range("A8:A" & lRow)) = 0 _
        Then Exit Sub

        For Each aCell In Range("H8:I" & lRow)
            If IsEmpty(aCell.Value) Then
                MsgBox "Please Select Value from Dropdown Menu...." _
                & aCell.Address

                Application.Goto aCell
                Cancel = True
                Exit For
            End If
        Next aCell
    End With
End Sub
于 2013-11-05T14:03:08.427 回答
0

sheet1 和 sheet(1) 通常不一样。最好是使用工作表的名称。你可能会错过一个“。” 在第二个 for (...in .range...) ?

PS,我是 VBA 的新手,application.goto aCell 有什么作用?因为它在“goto”之后放置一个“exit for”......

于 2013-11-06T14:50:38.000 回答