检查单元格中的值

时间:2013-11-05 13:53:14

标签: excel excel-vba vba

我做了一个Sub。但它给出了一个错误。! 我想检查列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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

PS,我是VBA的新手,application.goto aCell是做什么的?因为在“goto”之后把它放在“退出”之后很难......