+ Reply to Thread
Results 1 to 10 of 10

Prompt for # of new rows, copy formulas and conditional formatting

Hybrid View

  1. #1
    Registered User
    Join Date
    11-15-2010
    Location
    Arlington, VA
    MS-Off Ver
    Excel 365
    Posts
    34

    Prompt for # of new rows, copy formulas and conditional formatting

    Hi EF,

    Per the subject, I have a workbook where people will continuously enter data at the rate of about 100 rows per day. The first and last columns (A and K) contain formulas and/or conditional formatting.

    I found a macro that purports to do 80% of what I want:

    Sub InsertRowsAndFillFormulas_caller()
      '-- this macro shows on Tools, Macro..., Macros (Alt+F8) dialog 
      Call InsertRowsAndFillFormulas
    End Sub
     
    Sub InsertRowsAndFillFormulas(Optional vRows As Long = 0)
    ' Documented:  http://www.mvps.org/dmcritchie/excel/insrtrow.htm
    ' Re: Insert Rows --   1997/09/24 Mark Hill <[email protected]>
       ' row selection based on active cell -- rev. 2000-09-02 David McRitchie
       Dim x as long 
       ActiveCell.EntireRow.Select  'So you do not have to preselect entire row
       If vRows = 0 Then
        vRows = Application.InputBox(prompt:= _
          "How many rows do you want to add?", Title:="Add Rows", _
          Default:=1, Type:=1) 'Default for 1 row, type 1 is number
        If vRows = False Then Exit Sub
       End If
    
       'if you just want to add cells and not entire rows
       'then delete ".EntireRow" in the following line
    
       'rev. 2001-01-17 Gary L. Brown, programming, Grouped sheets
       Dim sht As Worksheet, shts() As String, i As Long
       ReDim shts(1 To Worksheets.Application.ActiveWorkbook. _
           Windows(1).SelectedSheets.Count)
       i = 0
       For Each sht In _
           Application.ActiveWorkbook.Windows(1).SelectedSheets
        Sheets(sht.Name).Select
        i = i + 1
        shts(i) = sht.Name
    
        x = Sheets(sht.name).UsedRange.Rows.Count 'lastcell fixup
    
        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 handle no constants in range -- John McKee 2000/02/01
        ' to remove the non-formulas -- 1998/03/11 Bill Manville
        Selection.Offset(1).Resize(vRows).EntireRow. _
         SpecialCells(xlConstants).ClearContents
       Next sht
       Worksheets(shts).Select
    End Sub
    But I installed and ran it and it crashes my worksheet ("Not enough system resources to display completely"). This is being done on a pretty high-end computer (16GB RAM, i7 processor) so I doubt it couldn't handle such a task.

    Also, this doesn't handle my conditional formatting requirement.

    I've attached the worksheet in question (it's a binary) if anyone wants to take a stab at it or a different approach.
    Attached Files Attached Files

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Prompt for # of new rows, copy formulas and conditional formatting

    Hi tecsbrain

    Try replacing your Module 2 Code with this. CTRL + i will fire the Code
    Public ws As Worksheet
    Option Explicit
    
    Sub Insert_Rows()
      Dim Rng As Range, cel As Range
      Dim LR As Long, vRows As Long
    
      If vRows = 0 Then
        vRows = Application.InputBox(prompt:= _
                                     "How many rows do you want to add?", Title:="Add Rows", _
                                     Default:=1, Type:=1)  'Default for 1 row, type 1 is number
        If vRows = False Then Exit Sub
      End If
    
      Set ws = Sheets("Entries")
      Application.EnableEvents = False
      With ws
        LR = .Range("A" & .Rows.Count).End(xlUp).Row
        .Range("A" & LR).AutoFill Destination:=.Range("A" & LR & ":A" & LR + vRows), Type:=xlFillDefault
        .Range("K" & LR).AutoFill Destination:=.Range("K" & LR & ":K" & LR + vRows), Type:=xlFillDefault
      End With
      Call ResetFormatting
      Application.EnableEvents = True
    End Sub
    
    
    
    Sub ResetFormatting()
      Dim LR As Long
      ' ----------------------------------------------------------------------------------------
      ' Written by..: Julius Getz Mørk; Modified by jaslake for tecsbrain 10/26/2014
      ' Purpose.....: If conditional formatting ranges are broken it might cause a huge increase
      '               in duplicated formatting rules that in turn will significantly slow down
      '               the spreadsheet.
      '               This macro is designed to reset all formatting rules to default.
      ' ----------------------------------------------------------------------------------------
      Set ws = Sheets("Entries")
      With ws
        LR = .Range("A" & .Rows.Count).End(xlUp).Row
        On Error GoTo ErrHandler
    
        ' Disable Events
        Application.EnableEvents = False
    
        ' Delete all conditional formatting rules in sheet
        .Cells.FormatConditions.Delete
    
        ' CREATE ALL THE CONDITIONAL FORMATTING RULES:
        With .Cells(1, 1).FormatConditions.Add(xlCellValue, xlLess, "=0")
          .Interior.Color = RGB(218, 150, 148)
        End With
    
        With .Cells(1, 1).FormatConditions.Add(xlCellValue, xlGreater, "=.00277777777778")
          .Interior.Color = RGB(141, 180, 226)
        End With
    
        ' Modify the "Applies To" ranges
        .Cells.FormatConditions(1).ModifyAppliesToRange .Range("K2:K" & LR)
        .Cells.FormatConditions(2).ModifyAppliesToRange .Range("K2:K" & LR)
      End With
    ErrHandler:
      Application.EnableEvents = True
    End Sub
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Registered User
    Join Date
    11-15-2010
    Location
    Arlington, VA
    MS-Off Ver
    Excel 365
    Posts
    34

    Re: Prompt for # of new rows, copy formulas and conditional formatting

    Thanks a bunch jaslake. Only one issue remains, and it's more an issue of Excel behavior than the macro I think, and something I forgot to mention. Because I am entering about 150 daily records under the same date, I need this code to copy the same date all the way down. When I ran it, it filled the date incrementing by one day all the way down for the number of rows I asked for.

    Also, row B contains a formula that needs to be copied as well. Can you walk me through the code so I can learn how to modify it?

    Possibly an even better solution than hacking it to copy the existing date in the starting cell would be for it to prompt for both a date and a number of cells...but that's just a preference.

    Thanks a bunch -- why there isn't a "buy a drink" feature here, I don't understand.
    Last edited by tecsbrain; 11-06-2014 at 07:16 AM.

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Prompt for # of new rows, copy formulas and conditional formatting

    Hi tecsbrain

    See if this does as required
    Public ws As Worksheet
    Option Explicit
    
    Sub Insert_Rows()
      Dim Rng As Range, cel As Range
      Dim LR As Long, vRows As Long
      Dim myDate As String
    
      myDate = Application.InputBox(prompt:= _
                                    "What Date (Format MM/DD/YYYY)?", Title:="Date???", _
                                    Default:=Date, Type:=2)  'Default for 1 row, type 1 is number
      If myDate = "" Then Exit Sub
    
      If vRows = 0 Then
        vRows = Application.InputBox(prompt:= _
                                     "How many rows do you want to add?", Title:="Add Rows", _
                                     Default:=1, Type:=1)  'Default for 1 row, type 1 is number
        If vRows = False Then Exit Sub
      End If
    
      Set ws = Sheets("Entries")
      Application.EnableEvents = False
      With ws
        LR = .Range("A" & .Rows.Count).End(xlUp).Row
        .Range("A" & LR).AutoFill Destination:=.Range("A" & LR & ":A" & LR + vRows), Type:=xlFillDefault
        .Range("K" & LR).AutoFill Destination:=.Range("K" & LR & ":K" & LR + vRows), Type:=xlFillDefault
        .Range("B" & LR + 1).Resize(vRows, 1).Value = myDate
      End With
      Call ResetFormatting
      Application.EnableEvents = True
    End Sub
    
    
    
    Sub ResetFormatting()
      Dim LR As Long
      ' ----------------------------------------------------------------------------------------
      ' Written by..: Julius Getz Mørk; Modified by jaslake for tecsbrain 10/26/2014
      ' Purpose.....: If conditional formatting ranges are broken it might cause a huge increase
      '               in duplicated formatting rules that in turn will significantly slow down
      '               the spreadsheet.
      '               This macro is designed to reset all formatting rules to default.
      ' ----------------------------------------------------------------------------------------
      Set ws = Sheets("Entries")
      With ws
        LR = .Range("A" & .Rows.Count).End(xlUp).Row
        On Error GoTo ErrHandler
    
        ' Disable Events
        Application.EnableEvents = False
    
        ' Delete all conditional formatting rules in sheet
        .Cells.FormatConditions.Delete
    
        ' CREATE ALL THE CONDITIONAL FORMATTING RULES:
        With .Cells(1, 1).FormatConditions.Add(xlCellValue, xlLess, "=0")
          .Interior.Color = RGB(218, 150, 148)
        End With
    
        With .Cells(1, 1).FormatConditions.Add(xlCellValue, xlGreater, "=.00277777777778")
          .Interior.Color = RGB(141, 180, 226)
        End With
    
        ' Modify the "Applies To" ranges
        .Cells.FormatConditions(1).ModifyAppliesToRange .Range("K2:K" & LR)
        .Cells.FormatConditions(2).ModifyAppliesToRange .Range("K2:K" & LR)
      End With
    ErrHandler:
      Application.EnableEvents = True
    End Sub

  5. #5
    Registered User
    Join Date
    11-15-2010
    Location
    Arlington, VA
    MS-Off Ver
    Excel 365
    Posts
    34

    Re: Prompt for # of new rows, copy formulas and conditional formatting

    This almost works, except it still does the default Excel autofill (date increments by 1 each row) in column A and then # copies of the prompted date in column B. What I need (I wish I could understand enough VBA to make the change!) is for column A to contain the prompted # of rows of the entered date, and the formula in column B (which is =TEXT(DATE, "dddd")) copied down # times.

    Thanks so much for your help!

  6. #6
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Prompt for # of new rows, copy formulas and conditional formatting

    Hi tecsbrain

    We're working with two different Files. My File Column A Formulas are
    Formula: copy to clipboard
    =IF(DATE=0,"",TEXT(DATE, "dddd"))


    Please attach your File that reflects this
    the formula in column B (which is =TEXT(DATE, "dddd"))
    Last edited by jaslake; 11-06-2014 at 04:02 PM.

  7. #7
    Registered User
    Join Date
    11-15-2010
    Location
    Arlington, VA
    MS-Off Ver
    Excel 365
    Posts
    34

    Re: Prompt for # of new rows, copy formulas and conditional formatting

    Oh, goodness! I must have forgotten to update the file. I'm making you do more work than necessary, my bad. I have to continue adding to this file every day and I guess at some point I made a usability change that I subsequently forgot about.
    Attached Files Attached Files

  8. #8
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Prompt for # of new rows, copy formulas and conditional formatting

    Hi tecsbrain

    Change the Column References as indicated
    Public ws As Worksheet
    Option Explicit
    
    Sub Insert_Rows()
      Dim Rng As Range, cel As Range
      Dim LR As Long, vRows As Long
      Dim myDate As String
    
      myDate = Application.InputBox(prompt:= _
                                    "What Date (Format MM/DD/YYYY)?", Title:="Date???", _
                                    Default:=Date, Type:=2)  'Default for 1 row, type 1 is number
      If myDate = "" Then Exit Sub
    
      If vRows = 0 Then
        vRows = Application.InputBox(prompt:= _
                                     "How many rows do you want to add?", Title:="Add Rows", _
                                     Default:=1, Type:=1)  'Default for 1 row, type 1 is number
        If vRows = False Then Exit Sub
      End If
    
      Set ws = Sheets("Entries")
      Application.EnableEvents = False
      With ws
        LR = .Range("A" & .Rows.Count).End(xlUp).Row
    
        '##### Change Column Reference
        .Range("B" & LR).AutoFill Destination:=.Range("B" & LR & ":B" & LR + vRows), Type:=xlFillDefault
        .Range("K" & LR).AutoFill Destination:=.Range("K" & LR & ":K" & LR + vRows), Type:=xlFillDefault
        
        '##### Change Column Reference
        .Range("A" & LR + 1).Resize(vRows, 1).Value = myDate
      End With
      Call ResetFormatting
      Application.EnableEvents = True
    End Sub
    
    
    
    Sub ResetFormatting()
      Dim LR As Long
      ' ----------------------------------------------------------------------------------------
      ' Written by..: Julius Getz Mørk; Modified by jaslake for tecsbrain 10/26/2014
      ' Purpose.....: If conditional formatting ranges are broken it might cause a huge increase
      '               in duplicated formatting rules that in turn will significantly slow down
      '               the spreadsheet.
      '               This macro is designed to reset all formatting rules to default.
      ' ----------------------------------------------------------------------------------------
      Set ws = Sheets("Entries")
      With ws
        LR = .Range("A" & .Rows.Count).End(xlUp).Row
        On Error GoTo ErrHandler
    
        ' Disable Events
        Application.EnableEvents = False
    
        ' Delete all conditional formatting rules in sheet
        .Cells.FormatConditions.Delete
    
        ' CREATE ALL THE CONDITIONAL FORMATTING RULES:
        With .Cells(1, 1).FormatConditions.Add(xlCellValue, xlLess, "=0")
          .Interior.Color = RGB(218, 150, 148)
        End With
    
        With .Cells(1, 1).FormatConditions.Add(xlCellValue, xlGreater, "=.00277777777778")
          .Interior.Color = RGB(141, 180, 226)
        End With
    
        ' Modify the "Applies To" ranges
        .Cells.FormatConditions(1).ModifyAppliesToRange .Range("K2:K" & LR)
        .Cells.FormatConditions(2).ModifyAppliesToRange .Range("K2:K" & LR)
      End With
    ErrHandler:
      Application.EnableEvents = True
    End Sub

  9. #9
    Registered User
    Join Date
    11-15-2010
    Location
    Arlington, VA
    MS-Off Ver
    Excel 365
    Posts
    34

    Re: Prompt for # of new rows, copy formulas and conditional formatting

    You've made me a happy man. What can I do in return?

  10. #10
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Prompt for # of new rows, copy formulas and conditional formatting

    If you're happy I'm happy.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

+ 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] Copy Conditional Formatting to other rows
    By zoktolk in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 10-15-2013, 01:49 PM
  2. Copy Conditional Formatting to other rows
    By Regan_Mitchell in forum Excel General
    Replies: 5
    Last Post: 10-15-2013, 08:19 AM
  3. Replies: 2
    Last Post: 02-07-2012, 05:33 AM
  4. Replies: 2
    Last Post: 06-03-2010, 01:04 AM
  5. Conditional Formatting Copy down Rows
    By SMac in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-30-2005, 12:45 PM

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