+ Reply to Thread
Results 1 to 8 of 8

Macro adds rows with values, not blanks

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    02-26-2010
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2003/2007/2010/2016
    Posts
    432

    Talking Macro adds rows with values, not blanks

    Hello!

    Using the modified Dave McRitchie InsertRowsAndFillFormulas macro as well as a highlight active row macro. Rows are being added, but they have values in them. Workbook attached. Anyone see how to fix this code?

    Thanks!

    Lost

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    ActiveSheet.Unprotect Password:="SECRET"
    Cells.FormatConditions.Delete
    With Target.EntireRow
    .FormatConditions.Add Type:=xlExpression, Formula1:="TRUE"
    With .FormatConditions(1)
    With .Borders(xlTop)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = 5
    End With
    With .Borders(xlBottom)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = 5
    End With
    End With
    .FormatConditions(1).Interior.ColorIndex = 20
    End With
     ActiveSheet.Protect Password:="SECRET"
    End Sub
    Private Sub cmbAddRow_Click()
    If Intersect(ActiveCell, Range("A12:B40")) Is Nothing Then
        MsgBox "Your cursor is at cell " & Selection.Address & ". Please place your cursor within the index."
        Exit Sub
    Else
    MsgBox "You are going to add rows below cell " & Selection.Address & "."
    Call InsertRowsAndFillFormulas
    End If
    End Sub
    '/=======Start of Code==========================/
    Sub InsertRowsAndFillFormulas()
    '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 blnProtectContents As Boolean
    Dim blnProtectDrawingObjects As Boolean
    Dim blnProtectScenarios As Boolean
    Dim vRows As Long, i As Long
    Dim strAddress As String, shts() As String
    Dim sht As Worksheet
    
    'set default for whether worksheet is protected or not
    blnProtectContents = False
    blnProtectDrawingObjects = False
    blnProtectScenarios = False
    strAddress = Selection.Address
    
    'rev. 2005-08-02 - check if worksheet unprotected
    ' if it's protected, get various information
    If Application.ActiveSheet.ProtectContents = True Then
    blnProtectContents = True
    If Application.ActiveSheet.ProtectDrawingObjects = True Then
    blnProtectDrawingObjects = True
    End If
    If Application.ActiveSheet.ProtectScenarios = True Then
    blnProtectScenarios = True
    End If
    ActiveSheet.Unprotect "SECRET"
    If Application.ActiveSheet.ProtectContents = True Then
    'not unprotected so stop process
    Exit Sub
    End If
    End If
    
    ' 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?" & vbCr & vbCr & _
    "Rows will be added UNDERNEATH this row.", _
    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
    ActiveSheet.Unprotect "SECRET"
    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
    ' and go back to original selected cells
    Worksheets(shts).Select
    Range(strAddress).Select
    
    'set worksheet back to original protected/unprotected state
    ActiveSheet.Protect Password:="SECRET", DrawingObjects:=blnProtectDrawingObjects, _
    Contents:=blnProtectContents, Scenarios:=blnProtectScenarios
    
    End Sub
    '/=======End of Code==========================/
    Attached Files Attached Files
    Last edited by leaning; 06-21-2011 at 11:01 AM. Reason: Solved!

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,447

    Re: Macro adds rows with values, not blanks

    Hi Learning,

    I see you have a single worksheet in this workbook. You also have 3 userforms. There is about 400 lines of code. You have event code behine the worksheet that fires each time a change happens on it.

    When I tried to open this workbook I got a debug stop message on the Unprotect line.

    My suggestion is to take the Subs that are behind the worksheet that are NOT event macros and put them into a MODULE. I see the code in the Sub you mention goes through all sheets. I'm not sure that is allowed when in code behind a single worksheet.

    Read http://www.cpearson.com/excel/Events.aspx for more background
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Macro adds rows with values, not blanks

    hi, leaning, try to comment out the following line or check attachment:
    Selection.AutoFill Selection.Resize(rowsize:=vRows + 1), _
    xlFillDefault
    in Sub InsertRowsAndFillFormulas()
    Attached Files Attached Files
    Last edited by watersev; 06-16-2011 at 10:55 AM.

  4. #4
    Valued Forum Contributor
    Join Date
    02-26-2010
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2003/2007/2010/2016
    Posts
    432

    Re: Macro adds rows with values, not blanks

    MarvinP,

    I cleaned up the attached workbook. Thanks for the help!

    Watersev: Your help fixed the problem!

    Two last things:

    1) The "data table" itself needs to be unlocked to allow user edit. And the cells below it need to be locked. If the user adds rows, those rows need to have just the A and B cells unlocked.

    2) Sometimes when the user adds rows, the formatting (borders, text alignment, don't make it to the new row. So, the user has to unprotect the document, do edits, and then reprotect. (If you look at the attached, I added a row under the "17", and the borders didn't carry over.)

    Any ideas about these?

    Lost
    Attached Files Attached Files
    Last edited by leaning; 06-16-2011 at 11:39 AM. Reason: typos

  5. #5
    Valued Forum Contributor
    Join Date
    02-26-2010
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2003/2007/2010/2016
    Posts
    432

    Talking Adds Blank Row to Bottom of Sheet

    All,

    In case anyone needs this thread, here's what the attached workbook has:

    1) Table-like section with 2 columns for user to add data. (This section has nothing below it.)
    2) Button to add new blank row to the bottom of that section.
    3) New row code allows changing the properties (border, font, data validation, etc.) for each of the individual cells added (Columns A:G for this).
    4) Column G has a SUM formula (via VBA) and is locked.
    5) There is a named range (WONum) that looks at Col A of the section (without listing the blanks), so you can use those values for data validation in other places.
    6) There is code to highlight each row as you are working on it.
    7) The Delete Rows button can delete multiple rows selected, not just one.

    Thanks to everyone for your help on this!

    Regards,

    Lost
    Attached Files Attached Files
    Last edited by leaning; 06-20-2011 at 10:11 AM. Reason: Code change in workbook.

  6. #6
    Valued Forum Contributor
    Join Date
    02-26-2010
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2003/2007/2010/2016
    Posts
    432

    Talking Adds Blank Row to Middle Section of Sheet

    All,

    The attached workbook has the same setup as the one above, only with this one, you can add a variable number of rows between rows. It also adds new rows to the middle of a sheet vice always adding them to the bottom (last unused row).

    This gem was almostly entirely the work of realniceguy5000!

    Good stuff! HTH!

    Regards,

    Lost
    Attached Files Attached Files

  7. #7
    Valued Forum Contributor
    Join Date
    02-26-2010
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2003/2007/2010/2016
    Posts
    432

    Talking (Updated) Adds Blank Row to Middle of Sheet

    All,

    The code that highlights the active row was printing the highlight.

    Fixed in the attached!

    HTH!

    Lost
    Attached Files Attached Files

+ Reply to Thread

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