+ Reply to Thread
Results 1 to 7 of 7

Add a row, copy formulas, and formatting

Hybrid View

  1. #1
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Add a row, copy formulas, and formatting

    Hello sp135,

    I have created an example workbook. This has a button below the first entry on row 8. Clicking the button will add a new row. There are named ranges on the sheet "Named Ranges" for the drop down validation and the starting sequence number. This is computed by subtracting 7 from the current entry row. Since the first entry row is 8, the line item number equals 1. Here is the macro called by the button.
    Sub AddRow()
    
      Dim Cell As Range
      Dim CmdBtn As OLEObject
      Dim Rng As Range
      
        Set CmdBtn = ActiveSheet.OLEObjects("CommandButton1")
        Set Rng = CmdBtn.TopLeftCell.Offset(-1, 0).Resize(1, 7)
        
          CmdBtn.Top = CmdBtn.TopLeftCell.Offset(1, 0).Top + 3
          Rng.Copy Rng.Offset(1, 0)
            
          For Each Cell In Rng.Offset(1, 0)
            If Not Cell.HasFormula Then Cell = ""
          Next Cell
          
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Add a row, copy formulas, and formatting

    This worked for me. (Although I had to make my own test book. My Mac doesn't play well with ActiveX controls)

    Sub MakeNewRows()
        Dim rowsToAdd As Long
        Dim LastCell As Range, newRange As Range
        rowsToAdd = Application.InputBox("How many", Default:="1", Type:=1)
        If 0 < rowsToAdd Then
            Rem cancel not pressed
            With ThisWorkbook.Sheets("Entry Form").Range("A:A")
                Set LastCell = .Cells(.Rows.Count, 1).End(xlUp)
            End With
            With LastCell.Resize(rowsToAdd + 1, 1).EntireRow
                .FillDown
                On Error Resume Next
                .Offset(1, 0).SpecialCells(xlCellTypeConstants).ClearContents
                On Error GoTo 0
            End With
        End If
    End Sub
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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