Solved

Macro for Adding Row and Adding to DB

Posted on 2011-02-17
5
202 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
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 41

Expert Comment

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

Dave
0
 
LVL 41

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

806 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