2

I searched a lot for an answer for my problem, but couldn't find anything that helped me.

I want to use Worksheet_SelectionChange` for a specific worksheet. This specific worksheet I want to define in code.

Example (this example makes no sense, I know, but it is the best way to show my problem):

I have three worksheets in my workbook: ws1, ws2, ws3.

ws1 and ws3 are empty, ws2 has some values. When I open the workbook, I search which worksheet is not empty. This works so far.

Now that I found which worksheet is not empty, I want to set the Worksheet_SelectionChange for this worksheet.

How do I do this?

4

1 回答 1

4

把它放在 THISWORKBOOK 代码区域

当您在该工作表中选择一个单元格时,如果单元格不为空,这将为您提供工作表的名称。

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, _
ByVal Target As Range)
    If Application.WorksheetFunction.CountA(Sh.Cells) <> 0 Then

        MsgBox Sh.Name & " is not blank"

        With Sh
            '~~> Insert code here which you want to run for that sheet
        End With
    End If
End Sub

如果您不想采用这种方式,则必须在相关工作表代码区域中注入代码。在工作表代码区域中注入代码已在 SO 中讨论过很多次

于 2013-09-17T19:26:40.817 回答