Excel VBA选择范围

huangapple go评论62阅读模式
英文:

Excel VBA select range

问题

我想要能够通过双击单元格来移动到同一工作簿中的特定位置,在下面的示例中,我想要选择当前行的列B:BR中的单元格。
为此,我使用以下代码:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Not Application.Intersect(Target, Range("o7:o7")) Is Nothing Then
        Cancel = True
        Worksheets("INCOMING").Activate
        Range("r" & ActiveCell.Row & ":br" & ActiveCell.Row).Select
    End If
End Sub

但它一直给我一个"运行时错误 '1004':选择范围类的方法失败"。
始终选择整行。有人知道我做错了什么吗?

英文:

I want to be able by double clicking on a cell to move to a certain location in the same workbook, in the example below, I want to select the cells in Column B:BR of the current row.
For this I am using the following code:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Not Application.Intersect(Target, Range("o7:o7")) Is Nothing Then
        Cancel = True
       Worksheets("INCOMING").Activate
       Range("r" & ActiveCell.Row & ":br" & ActiveCell.Row).Select  
    End If  
End Sub

but it keeps on giving me the "run-time error '1004' select method of range class failed.

There is always an entire row selected.
anyone has any idea what I am doing wrong?

答案1

得分: 0

请尝试下一个适应的代码事件。它假设您代码中的ActiveCell应该是最初双击的单元格("O7"):

Option Explicit

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target.Address = "$O$7" Then
        Cancel = True
        With Worksheets("INCOMING")
            .Activate
            .Range("B" & Target.Row & ":BR" & Target.Row).Select
        End With
    End If
End Sub

双击单元格"O7",将选择"INCOMING"工作表的第七行,位于"B"列和"BR"列之间...

如果您想要选择"INCOMING"工作表的ActiveCell所在的行,请将Target.Row更改为ActiveCell.Row

英文:

Please, try the next adapted code event. It assumes that ActiveCell from your code should be the one initially double clicked ("O7"):

Option Explicit

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target.address = "$O$7" Then
        Cancel = True
        With Worksheets("INCOMING")
            .Activate
            .Range("B" & Target.Row & ":BR" & Target.Row).Select
        End With
    End If
End Sub

Double clicking on cell "O7", the seventh row of "INCOMING" sheet is selected between "B" and "BR" columns...

If you want selecting the row of the ActiveCell of "INCOMING" sheet, you need to change Target.Row with ActiveCell.Row.

huangapple
  • 本文由 发表于 2023年2月16日 15:53:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469262.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定