Solved

Macro for Adding Row and Adding to DB

Posted on 2011-02-17
5
199 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
  • 3
  • 2
5 Comments
 
LVL 41

Expert Comment

by:dlmille
Comment Utility
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
Comment Utility
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 41

Expert Comment

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

Dave
0
 
LVL 41

Accepted Solution

by:
dlmille earned 500 total points
Comment Utility
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
Comment Utility
GREAT JOB DAVE.  JUST WHAT THE DOCTOR ORDERED!

Best regards,

Bright
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now