2016-08-24 36 views
0

按钮我有“工作表Sheet1”点击按钮,并希望它的“Sheet2的”,所有的数据在点击时执行其代码。建议如何激活其他工作表?,点击数据一张纸上另一个表

我是新来的VBA,并在过去几天已经了解一些这方面的帖子,但还没有找到一个满意的答案为止关于如何实现纸张的变化尽可能简单。我见过这样的东西:

Private Sub CommandButton1_Click() 
    Worksheets("sheet2").Cells(1, 1).Value = 1 
    Worksheets("sheet2").Cells(2,1).Value = 1 
    ... 
End Sub 

,在那里我必须不断重新声明对象的范围为“sheet2”。

回答

1

你应该使用WithActivateSelect将聚焦使纸张。

Private Sub CommandButton1_Click() 
    With Worksheets("sheet2") 
     .Cells(1, 1).Value = 1 
     .Cells(2,1).Value = 1 
    ... 

    '/Activate the sheet 
    .Activate 
    End With 

End Sub 
+0

在工作表(“工作表2”)和工作表(“工作表2”)之间有性能差异吗? – TheCharlatan

+0

你只有2-3个细胞来改变然后不是真的,但对于更大的数字,肯定是的。 – cyboashu

+0

我在“sheet2”中有大约10000行数据,并且子包含很多代码。推荐选项是“With”吗? – TheCharlatan

0

你可以切换到sheet2,然后无需执行任何后续命令明确提到sheet2这样的:

Private Sub CommandButton1_Click() 
    Worksheets("sheet2").Activate 'Switch to sheet2 

    Cells(1, 1).Value = 1 'this change will happen in sheet2 
    Celss(2,1).Value = 1  'this too 
    ... 
End Sub 
相关问题