Results 1 to 3 of 3

Macro to insert rows within a specified range

Threaded View

  1. #1
    Forum Contributor
    Join Date
    02-09-2010
    Location
    Constanta
    MS-Off Ver
    Excel 2007
    Posts
    128

    Question Macro to insert rows within a specified range

    I am using the below code for inserting rows in my worksheet. Rows 1 to 6 in the worksheet contain cells that have either label (headings) or notes. I do not want any row inserts to happen in this range.

    How should the below code be modified to accommodate this?

    Thanks in advance
    AGS

    'adds desired # of lines below the current line and
    ' copies the formulas to that/those lines
    'added selection of more than one worksheet
    ' - Gary L. Brown
    ' - Kinneson Corp. 01/17/2001
    ' - modification from thread discussion in
    ' Microsoft.Public.Excel.Programming newsgroup
    ' on 01/17/2001
    ' Re: Insert Rows -- 1997/09/24 Mark Hill
    ' The original macro is described in
    ' http://www.geocities.com/davemcritchie/excel/insrtrow.htm
    Dim vRows As Long
    Dim sht As Worksheet, shts() As String, i As Long
    
    ' row selection based on active cell --
    ' rev. 2000-09-02 David McRitchie
    ActiveCell.EntireRow.Select
    vRows = _
    Application.InputBox(prompt:= _
    "How many rows do you want to add?", Title:="Add Rows", _
    Default:=1, Type:=1) 'type 1 is number
    
    If vRows = False Then Exit Sub
    'if you just want to add cells and not entire rows
    ' then delete ".EntireRow" in the following line
    
    ReDim shts(1 To Worksheets.Application.ActiveWorkbook. _
    Windows(1).SelectedSheets.Count)
    i = 0
    
    'insert rows on grouped worksheets
    ' rev. 2001-01-17 Gary Brown
    For Each sht In _
    Application.ActiveWorkbook.Windows(1).SelectedSheets
    Sheets(sht.Name).Select
    i = i + 1
    shts(i) = sht.Name
    
    Selection.Resize(rowsize:=2).Rows(2).EntireRow. _
    Resize(rowsize:=vRows).Insert Shift:=xlDown
    Selection.AutoFill Selection.Resize(rowsize:=vRows + 1), _
    xlFillDefault
    On Error Resume Next
    ' to remove the non-formulas -- 1998/03/11 Bill Manville
    Selection.Offset(1).Resize(vRows).EntireRow. _
    SpecialCells(xlConstants).ClearContents
    Next sht
    
    'reselect original group - Dave McRitchie 01/17/2001
    Worksheets(shts).Select
    
    
    End Sub
    Last edited by asha3010; 03-10-2010 at 03:49 AM.

Thread Information

Users Browsing this Thread

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

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