Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA: activating/selecting a worksheet/row/cell

Tags:

excel

vba

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

like image 974
himura Avatar asked Apr 24 '26 18:04

himura


1 Answers

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.

like image 104
Barranka Avatar answered Apr 27 '26 00:04

Barranka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!