VBA:激活/选择工作表/行/单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12737297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 14:09:05  来源:igfitidea点击:

VBA: activating/selecting a worksheet/row/cell

vbaexcel-vbaexcel

提问by himura

Hello Stackoverflowers,

你好 Stackoverflowers,

I'm trying to use a button, that first goes to another excel file in a specific directory. While performing something, I want to add a row in a sheet the excel file i'm running the button from. To do that, i need to activate a certain row, or cell to use this

我正在尝试使用一个按钮,该按钮首先转到特定目录中的另一个 excel 文件。在执行某些操作时,我想在我正在运行按钮的 Excel 文件的工作表中添加一行。为此,我需要激活某个行或单元格才能使用它

ActiveCell.EntireRow.Insert

but it keeps telling me:

但它一直告诉我:

activate method of range class failed

my last trail was this:

我的最后一条线索是这样的:

Sheet1.Cells(2, 3).Activate
ActiveCell.EntireRow.Insert

Can anyone tell me how to get this done? i think because i'm in another workbook or something

谁能告诉我如何完成这项工作?我想是因为我在另一本工作簿或其他东西中

Thanks

谢谢

回答by Barranka

This is just a sample code, but it may help you get on your way:

这只是一个示例代码,但它可以帮助您继续前进:

Public Sub testIt()
    Workbooks("Workbook2").Activate
    ActiveWorkbook.Sheets("Sheet2").Activate
    ActiveSheet.Range("B3").Select
    ActiveCell.EntireRow.Insert
End Sub

I am assuming that you can open the book (called Workbook2in the example).

我假设您可以打开这本书(Workbook2在示例中调用)。



I think (but I'm not sure) you can squash all this in a single line of code:

我认为(但我不确定)您可以在一行代码中压缩所有这些:

    Workbooks("Workbook2").Sheets("Sheet2").Range("B3").EntireRow.Insert

This way you won't need to activate the workbook (or sheet or cell)... Obviously, the book has to be open.

这样你就不需要激活工作簿(或工作表或单元格)......显然,这本书必须是打开的。