New Excel Forum

This forum has been moved to TeachExcel.com

Ask all future questions in the New Excel Forum.

ExcelKey

Need help with ActiveCell.offset(1).EntireRow.Insert

Macros, VBA, Excel Automation, etc.

Need help with ActiveCell.offset(1).EntireRow.Insert

Postby No0dles58 » Wed Dec 23, 2015 11:12 am

Hi,
I am relatively new to VBA. Apologies if I'm not speaking the proper lingo.
I am trying to add a new row (record) to my sheet where each time I add the row, cell b2 will assign a unique ID..
For Instance if cell b2 = 10 when I add a new row b3 would be 10 and B2 would be 11

This is what I have - which is not working:
Each time I run the code it highlights the Activecell line...

[
Code: Select all
Sub Add_record()
Range("i2").Select
ActiveCell.Offset(1).EntireRow.Insert
Range("b2").Value = Range("b3").Value + 1
End Sub
]

Would appreciate some assistance with this code or any other ways to incorporate this onto my sheet.

Many Thanks!
  • 0

No0dles58
Rookie
 
Posts: 1
Joined: Dec 23, 2015
Reputation: 0
Excel Version: 2013

Re: Need help with ActiveCell.offset(1).EntireRow.Insert

Postby pecoflyer » Wed Dec 23, 2015 11:33 am

Hi and welcome

in red font on every VBA page one is asked to wrap code with code tags.
Please do so in the future.
I will do it for you this time
  • 0

A relevant topic title helps get faster and more answers
pecoflyer
Moderator
 
Posts: 1274
Joined: Jan 24, 2012
Location: Belgium
Reputation: 39
Excel Version: 2003/2007/2010

Re: Need help with ActiveCell.offset(1).EntireRow.Insert

Postby NoSparks » Wed Dec 23, 2015 2:09 pm

The code you have will always insert a new row 3 because Range("i2") is always the same cell, offset 1 row it's always row 3.
The value in B2, will always be 1 because the newly inserted row 3 will have no value in column B and nothing plus 1 will always be 1.
Perhaps you're wanting to add 1 to the existing value in B2 and then write that to B3 ?

Assuming that it really is a new row 3 you're wanting and an incremented number in column B on that row, then you could try this
Code: Select all
Sub Add_record()
Rows(3).Insert
Range("B2").Value = Range("B2").Value + 1
Range("B3").Value = Range("B2").value
End Sub


Hope that helps, if this isn't what you're wanting you'll need to re-explain.
  • 0

NoSparks
Excel Hobbyist
 
Posts: 637
Joined: May 28, 2014
Reputation: 103
Excel Version: 2010


Return to Macros and VBA Questions

Who is online

Users browsing this forum: No registered users and 223 guests

cron