如何从用户表单中选择另一个工作表的单元格范围 [英] How to select a range of cells of another worksheet from a user form
问题描述
亲爱的,
我正在处理工作表1,我从工作表1调用了一个用户表单来选择Sheet3的一系列单元格。因此我编写了以下代码:
I am working on worksheet 1 and I have invoke a user form from worksheet1 to select a range of cells of Sheet3. Hence I wrote the following code:
Private Sub CommandButton1_Click()
Private Sub CommandButton1_Click()
如果ActiveWorkbook.Worksheets(3).Visible那么
ActiveWorkbook.Worksheets(3)。范围("A1:B5")。选择
其他
ActiveWorkbook.Worksheets(3).Visible = xlSheetVisible
ActiveWorkbook.Worksheets(3).Range(" A1:B5")。选择
If ActiveWorkbook.Worksheets(3).Visible Then
ActiveWorkbook.Worksheets(3).Range("A1:B5").Select
Else
ActiveWorkbook.Worksheets(3).Visible = xlSheetVisible
ActiveWorkbook.Worksheets(3).Range("A1:B5").Select
结束如果
结束子
End If
End Sub
代码生成错误。什么应该是正确的代码?请注意,如果我删除粗体部分,即 范围("A1:B5")
,则代码可以正常工作。请回复
The code generates an error. What should be the proper code ? Kindly note that if I remove the bold portion i.e. Range("A1:B5") then the code works properly. Kindly reply
问候,
Somnath
推荐答案
您有两种选择:
You have two options:
1)首先选择工作表(3),然后选择范围:
1) First select Worksheets(3), then select the range:
Private Sub CommandButton1_Click()
If Not Worksheets(3).Visible Then
Worksheets(3).Visible = xlSheetVisible
End If
Worksheets(3).Select
Worksheets(3).Range("A1:B5").Select
End Sub
- 或 -
2)使用Application.GoTo:
2) Use Application.GoTo:
Private Sub CommandButton1_Click()
If Not Worksheets(3).Visible Then
Worksheets(3).Visible = xlSheetVisible
End If
Application.Goto Worksheets(3).Range("A1:B5")
End Sub
请注意,我稍微重新构建了代码。
Note that I restructured the code slightly.
这篇关于如何从用户表单中选择另一个工作表的单元格范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!