如何在vba中打开工作表?
有趣的是,只有在 Excel 窗口中实际选择了工作表时,以下代码才有效。我真的很想尽快完成这个宏,但似乎无法弄清楚如何选择特定的工作表以便在 Excel 中打开它?如果有人知道怎么做,非常感谢。我必须使用范围等。
sheet.Range(Cells(firstRow, 2).Address(False, False), Cells(lastRow, 50)).Select
With Selection
.Copy
End With
sheet.Range(Cells(firstRow, 3).Address(False, False), Cells(lastRow, 51)).Select
With Selection
.PasteSpecial xlPasteValuesAndNumberFormats
End With
Hilariously the following code only works if the worksheet is actually selected in the excel window. I really want to finish this macro soon but can't seem to work out how to select a specific worksheet so it is open in excel? Many thanks if someone knows how. I have to use range and so on.
sheet.Range(Cells(firstRow, 2).Address(False, False), Cells(lastRow, 50)).Select
With Selection
.Copy
End With
sheet.Range(Cells(firstRow, 3).Address(False, False), Cells(lastRow, 51)).Select
With Selection
.PasteSpecial xlPasteValuesAndNumberFormats
End With
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以按名称或基于 1 的索引(数字 - 第一个工作簿、第二个工作簿等)激活工作表。无论哪种方式,语法都是相同的。
这将激活第三个工作表:
这将激活名为 stats 的工作表:
当然,您不必实际在 Excel 窗口中选择工作表来使用它。您的代码使用一个名为
sheet
的变量,我假设您已将其分配给活动工作表。您可以设置sheet = ActiveWorkbook.Sheets("stats"),然后使用该工作表(即使该工作表不在视图中),而不是这样做。You can activate the worksheet by name or by 1-based index (the number -- 1st workbook, 2nd, and so on). The syntax is the same, either way.
This will activate the 3rd worksheet:
This will activate the worksheet named stats:
Of course, you don't have to actually make the worksheet selected in the Excel window to work with it. Your code uses a variable called
sheet
, which I assume you've assigned to the active worksheet. Instead of doing that, you canset sheet = ActiveWorkbook.Sheets("stats")
, and then work with the sheet even if is not in view.工作簿(x).工作表(x).激活?
Workbooks(x).Worksheets(x).Activate ?