You are here:

Excel/insert rows without losing formulas

Advertisement


Question
QUESTION: Hi Tom,  Is there a formula/macro that would enable support staff to enter a new row in a worksheet but only copy the formulas from the previous row?  I found a way to insert a new row even when the worksheet is protected, but it copies everything including the formulas, which is good, but it alos means a lot of deleting data in cells before inserting new data.  The macro code I found/used is below, but as I said it copies everyhting when inserting a new row instead of just the formulas.

   Activesheet.Unprotect ("password")
   Selection.Copy
   Selection.Insert Shift:=xlDown
   Application.CutCopyMode = False
   Activesheet.Protect Password:="password"


ANSWER: Dave,

Well you could modify that

Sub abc()
ActiveSheet.Unprotect ("password")
  Selection.Copy
  Selection.Insert Shift:=xlDown
  Selection.SpecialCells(xlConstants).ClearContents
  Application.CutCopyMode = False
  ActiveSheet.Protect Password:="password"

End Sub

or

Sub abc()
ActiveSheet.Unprotect ("password")
  Selection.Copy
  Selection.Insert Shift:=xlDown
  Selection.Offset(1,0).SpecialCells(xlConstants).ClearContents
  Application.CutCopyMode = False
  ActiveSheet.Protect Password:="password"
End Sub

depending on from which row you want the constants cleared.

--
Regards,
Tom Ogilvy



---------- FOLLOW-UP ----------

QUESTION: Tom,

This is very neat.  Thanks.

Can it also be done so that:

1) it inserts the new row below the one you selected at the time the macro is run? right now either example you sent is inserting the new row above the selected row.  I've tried finding an options/preferences to fix it but no luck.

2) can it be done so that if they forget to select a whole row (just in a single cell when they run the macro) that it will still insert a new row below with everything but the formulas cleared?

Answer
Dave,

Try this (worked for me)

Sub abc()
Dim r As Range
ActiveSheet.Unprotect ("password")
 Set r = Selection.EntireRow
 r.Copy
 r.Offset(1, 0).Insert Shift:=xlDown
 r.Offset(1, 0).SpecialCells(xlConstants).ClearContents
 Application.CutCopyMode = False
 ActiveSheet.Protect Password:="password"

End Sub

--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

All Answers

Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Worked with the program for many years - provided assistance on MS Excel Newsgroups since 1997. Have received the Microsoft MVP award annually since 1999. I don't answer questions on using Excel in a browser Since I have no way to test this. Prefer not to answer charting questions. I consider myself to be particularly knowledgeable about using VBA internal to Excel but have no problems with formulas and pivot tables either.

Experience

Have Used Excel for 15 - 20 years. Answered in excess of 70,000 Excel related questions in MS Excel newsgroups. Unless obvious, please specify whether you want a worksheet function or macro/VBA solution.

Education/Credentials
BS General Engineering (concentration in Industrial Engineering) MS Operations Research Systems Analysis

©2009 About.com, a part of The New York Times Company. All rights reserved.