0

一般来说,我对创建宏和编程很陌生。我有一个包含 38 个选项卡的工作表。31 个选项卡用于一个月中的日期。我想创建一个宏,只要在 N 列中为这 31 个选项卡中的每一个选择“MCO”,它就会向用户提示警告消息。那可能吗?

谢谢

4

2 回答 2

2

可以使用工作簿级别的 SheetSelectionChange 事件。在 VBA 项目的 ThisWorkbook 模块中粘贴以下代码:

Option Compare Text
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    Dim SelectedCellsInColN As Range
    ' Make a range of the cells in Target range that intersect with the cells in the N   column
    Set SelectedCellsInColN = Intersect(Target, Target.Parent.Range("N1:N" & CStr(Target.Parent.Rows.Count)))

    If Not SelectedCellsInColN Is Nothing Then
        ' The user has selected a cell in column N
        Dim CurrCell As Range
        For Each CurrCell In SelectedCellsInColN
            ' If the cell's value contains mco (in any case) then prompt the user with a messagebox
            If CurrCell.Value Like "*MCO*" Then
                MsgBox "You've selected MCO"
                ' Exit the sub so we don't keep bugging the user about it...
                Exit Sub
            End If
        Next CurrCell
    End If
End Sub

基本上,代码所做的是查看目标范围以查看是否选择了 N 列中的任何单元格,然后遍历 N 列中选择的任何单元格以查看它们的值是否包含 MCO(您可以摆脱如果您只需要在单元格仅包含“MCO”时出现提示,则为星号),如果是这样,则提示用户并退出。

希望有帮助。

-乔恩

于 2009-01-06T20:42:35.940 回答
0

您是在寻找宏解决方案还是 vba 解决方案。两者是不同的。对于使用宏重新编码器执行步骤的宏,对于 VBA 解决方案,从 Jon 的答案开始

于 2009-01-13T16:58:01.670 回答