[vba] VBA: activating/selecting a worksheet/row/cell

Hello 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

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

This question is related to vba excel

The answer is


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 Workbook2 in the example).


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.