英文:
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论