Worksheet_BeforeDoubleClick(ByVal目标为范围,取消为布尔值)和范围错误

时间:2012-06-10 02:46:12

标签: excel-vba double-click vba excel

希望有人可以帮我解决我遇到的VBA问题。

当用户双击单元格时,我想更改命名范围的值。有两个工作表,初始工作表在单元格中有一个值,然后是包含命名范围Account_Number的目标工作表。

这是我的代码:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    [Account_Number] = ActiveCell.Value 'assign the value to the Named Range

    Range([Account_Number]).Select ' go to the Named Range on the other worksheet

   'Sheets("Transaction Listing_LINKS").Select 'the workaround to the problem below
End Sub

我期望填充和选择命名范围。

问题是我得到了

  

“1004错误:对象'_Worksheet'的方法'范围'失败”

双击单元格时出现

错误。

奇怪的是,如果我只使用一个工作表,它可以毫无问题地工作。

任何人都可以向我解释为什么会发生这种情况以及如何避免它?

我能想到的唯一修复/解决方法是对工作表的选择进行编码以强制它选择它。

谢谢

1 个答案:

答案 0 :(得分:2)

尝试:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
  'assign the value to the Named Range
  Sheet2.Range("Account_Number").Value = ActiveCell.Value
  ' go to the Named Range on the other worksheet
  Application.GoTo Reference:="Account_Number" 
 'Sheets("Transaction Listing_LINKS").Select 'the workaround to the problem below
End Sub

当然,您需要将“Sheet2”更改为包含命名范围的目标工作表名称。