+ Reply to Thread
Results 1 to 5 of 5

Thread: dynamically adding a row

  1. #1
    Registered User
    Join Date
    01-29-2009
    Location
    NYB
    MS-Off Ver
    Excel 2010
    Posts
    5

    dynamically adding a row

    I have been using excel for quite a couple years but all limited to basic functionality. Now I am trying to do some advance tasks so I can create a simple application so people without any of excel knowledge can use it.

    I am using excel 2010.

    One of the task I need to implemented is that:
    0. create a button in one cell. when user click the button, do following:
    1. find a cell that the background is black color.
    2. insert a row above the row contains the above found cell.
    3. do some formatting, restriction(lock cell), put some formula in some cell on that row. etc.

    could you some point me some direction? a few samples or links that can get me started?

    Thanks!

  2. #2
    Valued Forum Contributor
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    244

    Re: dynamically adding a row

    Your post was extremely vague so the macro is extremely vague in response. I am assuming your cell background color is true black, in other words, will have a color index of 1. Just create a command button and assign this macro to it:

    Sub MainMacro()
    Dim icell As Range
    
    For Each icell In Sheets("Sheet1").UsedRange
        If icell.Interior.ColorIndex = 1 Then
            icell.EntireRow.Insert Shift:=xlDown
            icell.Offset(-1, 0).Value = "works" 'in for testing purposes. Change to whatever value you may or may not want in cell above
            icell.Offset(-1, 0).Font.Bold = True 'or whatever format you eluded to
            icell.Offset(-1, 0).Locked = True 'locks the cell.  you need to have the sheet protection on for this to apply
            icell.Offset(-1, 1).Formula = "=A1+B1" 'as you eluded to in post
            Exit Sub 'exits the macro so it only works on one black background cell
        End If
    Next icell
    
    End Sub
    Last edited by stnkynts; 06-24-2011 at 06:53 PM.

  3. #3
    Registered User
    Join Date
    01-29-2009
    Location
    NYB
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: dynamically adding a row

    sorry for the vague question, your answer is exactly what I am looking for.

    let me take a step back and explain what I trying to do. I want to create a simple worksheet for someone else who has not much experience on excel to use. There will be formatting/validation/formula etc on the sheet, I wanted that users can only change those cells they need to and won't mess up all the formatting/validation/formula. I've attached a excel file for demonstration purpose.

    In that "TestSheet1", the top left area that surrounded by the black background cells are user working area. The first 4 columns are allow user to input, and the rest 2 columns are calucated with formula. Note those columns are already have predefined type(date/currency/percetage etc) and also have some validate inplace(such as those prices are between -99999 to 99999). I also put the first 4 columns in unlocked mode and the whole excel is protected(with no password now) so users won't be able to change column E and F. 10 rows are predefined, but in case they need more, click the button(cell I3) to add a new row right before the black row, and automatically put those formatting/validation/formula there. (for users that has experience on excel, the easiest way is to just insert a row and copy from existing row, but for users without much excel experience, it might just mess it up).

    Another functionality I hope to build is to click a button and create a new template sheet. for example, user click cell ("Create a new sheet" in cell I4) and content in "TestSheet2" is created and he can start working on.

    I created the test excel file in excel 2010 and saved as .xls so hope it works on all versions of excel.

    Thanks very much for helping!
    Attached Files Attached Files

  4. #4
    Valued Forum Contributor
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    244

    Re: dynamically adding a row

    For your add a row button:

    Sub AddaNewRow()
    Dim lastrow As Long
    
    Application.ScreenUpdating = False
    lastrow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
    ActiveSheet.Range("A" & lastrow).Offset(1, 0).EntireRow.Insert Shift:=xlDown
    ActiveSheet.Range("A" & lastrow, "F" & lastrow).Copy
    ActiveSheet.Range("A" & lastrow).Offset(1, 0).PasteSpecial xlPasteAll
    ActiveSheet.Range("A" & lastrow, "D" & lastrow).Offset(1, 0).ClearContents
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    
    End Sub
    For your create a new sheet button:

    Sub CreateNewSheet()
    Dim lastrow As Long
    Dim NewName As Variant
    Dim newsht As Worksheet
    
    lastrow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
    Application.ScreenUpdating = False
    ActiveSheet.Cells.Copy
    Set newsht = Sheets.Add(After:=Sheets(Sheets.Count))
    newsht.Paste
    newsht.Range("A2:D" & lastrow).ClearContents
    NewName = Application.InputBox("What would you like to name the new sheet?", "New Sheet Name")
    If NewName = "" Then
    ElseIf NewName = False Then
    Else
    newsht.Name = NewName
    End If
    newsht.Range("A2").Select
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    
    End Sub
    Let me know.

  5. #5
    Registered User
    Join Date
    01-29-2009
    Location
    NYB
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: dynamically adding a row

    got it working, thank you so much stnkynts!

+ 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.2.0