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

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

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