0

我有问题。如何根据单元格值启用/禁用按钮。

在 excel 表中,我有 2 个按钮。

我需要做的是。

如果列“L”有数据

一键启用

别的

“BQ”有数据

需要启用另一个按钮。其他按钮将被禁用。

如何实现它。

请帮助....提前谢谢

4

2 回答 2

2

我一直在等待你的答复,如果两者都填满了会发生什么。我在代码中添加了该选项。修改它以满足您的需要。

试试这个

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo Whoa

    Application.EnableEvents = False

    CommandButton1.Enabled = False: CommandButton2.Enabled = False

    '~~> If both cols are filled up
    If Application.WorksheetFunction.CountA(Columns(12)) > 0 And _
    Application.WorksheetFunction.CountA(Columns(69)) > 0 Then
        '~~> Change the message as applicable
        MsgBox "Both Columns Cannot have data", vbInformation, "Error"
    Else
        '~~> If Col L is filled up
        If Application.WorksheetFunction.CountA(Columns(12)) > 0 _
        Then CommandButton1.Enabled = True

        '~~> If Col BQ is filled up
        If Application.WorksheetFunction.CountA(Columns(69)) > 0 _
        Then CommandButton2.Enabled = True
    End If
LetsContinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume LetsContinue
End Sub
于 2012-10-04T15:37:28.713 回答
0

将 CommandButton1 和 CommandButton2 替换为您的按钮名称。

Private Sub Worksheet_Change(ByVal Target As Range)

    If Application.WorksheetFunction.CountA(Range("L:L")) > 0 Then
        CommandButton1.Enabled = True
    Else
        CommandButton1.Enabled = False
    End If

    If Application.WorksheetFunction.CountA(Range("BQ:BQ")) > 0 Then
        CommandButton1.Enabled = False
        CommandButton2.Enabled = True
    Else
        CommandButton1.Enabled = True
    CommandButton2.Enabled = False
End If

End Sub
于 2012-10-04T15:26:53.910 回答