+ Reply to Thread
Results 1 to 6 of 6

Copy and insert row

Hybrid View

  1. #1
    Registered User
    Join Date
    10-02-2012
    Location
    Finland
    MS-Off Ver
    Excel 2010
    Posts
    14

    Copy and insert row

    Hi,

    I want to copy current row and insert below.
    Only formulas (and eventually formatting) should be copied.
    It works fine but the problem is that values are copied too.

    My code is
    Public Sub insertRowBelow()
    ActiveCell.Offset(1).EntireRow.Insert shift:=xlDown, CopyOrigin:=xlFormatFromRightOrAbove
    ActiveCell.EntireRow.Copy
    ActiveCell.Offset(1).EntireRow.PasteSpecial xlPasteFormulasAndNumberFormats
    Application.CutCopyMode = False
    End Sub

    I found another thread from 2007 with similar problem but that code is completely different and I don't know how to combine these two codes.
    Part of the solution (if you don't want to read the conversation) was to add:
        If Target.Row = lastrow2 Then
            Rows(lastrow2 - 1).Copy
            Rows(lastrow2).Insert
            On Error Resume Next
            Rows(lastrow2).SpecialCells(xlCellTypeConstants).ClearContents
            On Error GoTo 0
            Application.CutCopyMode = False
        End If

    Can anyone please help me to extend the code to remove values and only keep the formulas?

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,861

    Re: Copy and insert row

    try:
    Public Sub insertRowBelow()
    ActiveCell.Offset(1).EntireRow.Insert shift:=xlDown, CopyOrigin:=xlFormatFromRightOrAbove
    ActiveCell.EntireRow.Copy
    ActiveCell.Offset(1).EntireRow.PasteSpecial xlPasteFormulasAndNumberFormats
    On Error Resume Next
    ActiveCell.EntireRow.SpecialCells(xlCellTypeConstants).ClearContents
    On Error GoTo 0
    Application.CutCopyMode = False
    End Sub
    error handling added to avoid problems if there was no constants if you are sure that there will be always some constants, you can add just this middle line
    Best Regards,

    Kaper

  3. #3
    Registered User
    Join Date
    10-02-2012
    Location
    Finland
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Copy and insert row

    Thank you Kaper, this works fine!

    Just one more thing. To make my file work for other users as well without the need of the Developer bar or quick commands - is it possible in any way to run this macro by a hyperlink?

    I will try to upload my spreadsheet to make it clearer what I mean. In column A I have the code ="+" (must have it as a formula so the + won't disappear with the insert row macro).
    I planned this to be used as a hyperlink to the macro but I really can't find any good solution on the web.
    Attached Files Attached Files

  4. #4
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,861

    Re: Copy and insert row

    My proposition is to write event handler for doubleclick event (it normally enters cell editing mode).
    so doubleclick anywhere in a sheet will do what it does normally, but if it is coubleclick in column A and clicked cell contains "+ as a formula" :-) then main macro is called.
    See code of the R&D Plan sheet in attachment. it contains:
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Not Intersect(Target, Columns("A")) Is Nothing Then
      If Target.Cells.Count = 1 Then
        If Target.Formula = "=""+""" Then
          Call insertRowBelow
          Cancel = True
        End If
      End If
    End If
    End Sub
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    10-02-2012
    Location
    Finland
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Copy and insert row

    You made my day Kaper!

    It really works! Thank's a lot!

    But now I need to spread reputation to others before I can reputate you more ;-)

  6. #6
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,861

    Re: Copy and insert row

    I'm glad to hear it suits you, and thanks for the reputation. It would be also lovely if you could select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Adapt copy and insert VBA code to ask how many times your would like it to copy and insert
    By Jamidd1 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-10-2016, 04:59 PM
  2. Replies: 3
    Last Post: 09-24-2015, 06:46 AM
  3. [SOLVED] VBA codes to insert the rows and copy the first entire row text and insert sheet
    By PRADEEPB270 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-26-2013, 05:05 AM
  4. [SOLVED] Macro to copy row, insert below, invert amt, and insert blank row between each new tx
    By raquel1227 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-07-2013, 12:56 PM
  5. Macro: Copy Row and Insert it ; Insert Odd Year
    By buto87 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 04-25-2012, 08:55 AM
  6. Copy paste data, insert rows, insert data and change . to -
    By xWiZardx in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-16-2010, 01:44 AM
  7. Macro to insert copy and insert formulas only to next blank row
    By bob in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-30-2006, 07:10 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1