vba Excel,通过单击单元格,焦点将移动到另一个单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4376052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:23:06  来源:igfitidea点击:

Excel, By clicking on Cell, the Focus is moved to another cell

excelvba

提问by Doron Shai

In Excel, I want to have a cell (B10) that when clicking it, the focus will change to another cell (C12).

在 Excel 中,我想要一个单元格 (B10),单击它时,焦点将更改为另一个单元格 (C12)。

This other cell is defined in another Cell (A2).

该另一个单元格在另一个单元格 (A2) 中定义。

Any Idea?

任何的想法?

GorovDude

哥罗夫

回答by Chris Spicer

Use worksheet_selectChange. In essence, the code will look like this:

使用 worksheet_selectChange。本质上,代码将如下所示:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Address = Range("B10").Address Then
        Dim rng As Range
        Set rng = Range("A2")
        Range(rng.Value).Select
    End If
End Sub

I've hard-coded the range addresses for clarity.

为清楚起见,我对范围地址进行了硬编码。

回答by Alex P

This code will cause Range(A2) to be selected whenever the user clicks in column B:

每当用户单击 B 列时,此代码将导致选择 Range(A2):

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 If Not Application.Intersect(Range("B:B"), Target) Is Nothing Then
 Range("A2").Select
 End If
End Sub