Solved

Macro for Adding Row and Adding to DB

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

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.
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

696 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