?
Solved

Macro for Adding Row and Adding to DB

Posted on 2011-02-17
5
Medium Priority
?
211 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 2000 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

771 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