Solved

Macro for Adding Row and Adding to DB

Posted on 2011-02-17
5
209 Views
Last Modified: 2012-05-11
EE Professionals,

I need a small Macro that on Sheet 1, if you push a button, you get a new row autocreated with all the characteristics (i.e. validation, etc.). When a row is added, it is only added as a row within a range so as not to add a row across the entire set of columns. When you add data to the cells associated with the new row, it automatically populates a DB Matrix (also an expanding range based on adding rows) on Sheet 2.

That's it!

B.
Adding-a-Row-Macro.xls
0
Comment
Question by:Bright01
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 42

Expert Comment

by:dlmille
ID: 34921296
Here's the code that adds the row, tied to a command button.  I defined a couple fixed named ranges in the Benefits tab to make it easy - and you can see those names in the code:
 
Sub AddRow()
Dim mycell As Range

    If Range("firstitem").Value = "" Then
        Exit Sub ' no data to copy down
    ElseIf Range("firstitem").Offset(1, 0).Value = "" Then
        Set mycell = Range("firstitem")
    Else
        Set mycell = Range("firstItem").End(xlDown)
    End If
    
    Range(mycell, mycell.Offset(0, Range("itemwidth").Columns.Count - 1)).Copy mycell.Offset(1, 0)
    
End Sub

Open in new window


And here's the code that duplicates entries in the range to the Benefits DB tab:
 
Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Target, Range("ItemWidth").EntireColumn) Is Nothing Then
        Call MirrorSht1Sht2(ActiveSheet.Name, Target, "Benefit DB")
    End If
    
End Sub

Open in new window


The following  does the duplication work:
Sub MirrorSht1Sht2(sh1 As String, rsh1 As Range, sh2 As String)
    Range("'" & sh1 & "'!" & rsh1.Address).Copy Range("'" & sh2 & "'!" & rsh1.Address)
End Sub

Open in new window


See attached file.

Enjoy!

Dave
AddRowAndDuplicate-r1.xls
0
 

Author Comment

by:Bright01
ID: 34921879
Dave,

Very close!  The Added Item to sheet 1 should not be a repeat of the last item but simply an expansion of another row with the same characteristics.  In otherwords, if I colorcoded the first line and we added one with the button, the same colorcode would appear on line 2 but it would have none of the content from row 1.  And again, I expect that any dropdown box in row 1 would transfer to the new row ..... row 2......; data then inputted and the data shows up in Sheet 2.

We are very close on this.

Big Thanks Dave.
0
 
LVL 42

Expert Comment

by:dlmille
ID: 34922294
Would you please provide a sample and I'll quickly adjust.

Dave
0
 
LVL 42

Accepted Solution

by:
dlmille earned 500 total points
ID: 34922309
I think I've got it - just paste formats, yes?
Sub AddRow()
Dim mycell As Range

    If Range("firstitem").Value = "" Then
        Exit Sub ' no data to copy down
    ElseIf Range("firstitem").Offset(1, 0).Value = "" Then
        Set mycell = Range("firstitem")
    Else
        Set mycell = Range("firstItem").End(xlDown)
    End If
    
    Range(mycell, mycell.Offset(0, Range("itemwidth").Columns.Count - 1)).Copy
    mycell.Offset(1, 0).PasteSpecial (xlPasteFormats)
    Application.CutCopyMode = False
    
End Sub

Open in new window


Here's the updated file:

Dave
AddRowAndDuplicate-r2.xls
0
 

Author Closing Comment

by:Bright01
ID: 34923919
GREAT JOB DAVE.  JUST WHAT THE DOCTOR ORDERED!

Best regards,

Bright
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question