Solved

Reset to Macro in Spreadsheet

Posted on 2011-02-18
13
315 Views
Last Modified: 2012-05-11
An outstanding EE Professional built this macro for me.  When you hit "Add Row" it automatically adds a row below the last completed one.  The only problem is when I want to reset the sheet, it doesn't recognize the reset for the macro to begin again at the start.  So after inputting information and having it mirror it, when I now go to reset, it doesn't reset the macro.  I think it's one or two lines of code to do this.

B.
AddRowAndDuplicate-r2.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
  • 8
  • 5
13 Comments
 
LVL 42

Expert Comment

by:dlmille
ID: 34931159
Try changing your reset code to this:


'This Macro Clears the Contents and rests the Worksheet
Sub ResetSolutionFields()
Dim i As Integer
With Worksheets("Benefit")
    .Range("tempsection").clearcontents
End With
End Sub

Open in new window

0
 
LVL 42

Expert Comment

by:dlmille
ID: 34931177
You might also use .clearall to clear the formats.

So

Sub ResetSolutionFields()

worksheets("Benefit").range("tempsection").Clear

end sub

To really clear all of it, you can do this:

Sub ResetSolutionFields()
Dim i As Integer, r As Range

    Set r = Range("FirstItem", Cells(Cells.Rows.Count, 1)).End(xlUp)
    r.Clear
   
End Sub
0
 
LVL 42

Expert Comment

by:dlmille
ID: 34931179
Why?  The macro adds to the bottom of the used range.   Your code put spaces in, so the add macro saw that as used range, and went to the bottom of it.

dave
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

Author Comment

by:Bright01
ID: 34931250
Dave,

YATM (You are the Man)..... as a rookie at this, your responses are easy to follow and very helpful.  I installed the code and it now works appropriately; except for one thing.  I have a validated list in the first cell that drops down a menu of options.  When I select one, I fill out the info. in the corresponding cells in the row.  When I go to add another row (using your macro), it doesn't carry over the validation box.  

Any ideas?

B.
0
 
LVL 42

Expert Comment

by:dlmille
ID: 34931308
Change your code to this:

 
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 (xlPasteAll)
    Application.CutCopyMode = False
    Range(mycell.Offset(1, 0), mycell.Offset(1, Range("itemwidth").Columns.Count - 1)).ClearContents
    
End Sub
Sub MirrorSht1Sht2(sh1 As String, rsh1 As Range, sh2 As String)
    Range("'" & sh1 & "'!" & rsh1.Address).Copy Range("'" & sh2 & "'!" & rsh1.Address)
End Sub

Sub ResetSolutionFields()
Dim i As Integer, r As Range, bottomRow As Range

    Set bottomRow = Cells(Cells.Rows.Count, 1).End(xlUp)

    Set r = Range(Range("FirstItem"), Cells(bottomRow.Row, Range("itemwidth").Columns.Count))
    Debug.Print "Clearing " & r.Address
        r.Clear
    
End Sub

Open in new window


Note - rather than just pasting formats, it pastes all then comes back and clears the entries...

Also, there was an error in the CLEAR as it was only working on column A.  First had to find bottom row using A, then cleared everything from A to rightmost column in range "ItemWidth" which is a defined name you maintain, though it wil take care of itself if you insert inside the table range on the input sheet.

Dave
0
 
LVL 42

Accepted Solution

by:
dlmille earned 500 total points
ID: 34931313
Heres the file.

Enjoy!

Dave
AddRowAndDuplicate-r3.xls
0
 

Author Closing Comment

by:Bright01
ID: 34931358
You know Dave, it's always harder than it seems at first.  Thanks for sticking with me on this!  

Best regards,

B.
0
 
LVL 42

Expert Comment

by:dlmille
ID: 34931367
I"ve been there.  Trying to sort out VBA without manual, etc.  Great to have E-E give an assist.  Recommend getting an intro to VBA for Excel book as a good reference.  I think I need one, too :)

Dave
0
 

Author Comment

by:Bright01
ID: 34931714
Dave,

Can you take one more look at this?  It's still not working just right and I've been trying to troubleshoot it with no success.  If you set up a validation Test sample for the first row, and you add rows as the macro does correctly, and then you reset, you must reset your validation process.  I was trying to make sure that while the contents changed to null or blank in the first row, that the validation remained on.  This way you don't have to reset validation each time you clear the sheet.

B.
0
 
LVL 42

Expert Comment

by:dlmille
ID: 34931738
So you don't want to clear any validation's on reset?  or, just maintain the first row's validations and clear all the rest?  The reset does a CLEAR, which resets everything.  So be clear with what you want and I'll assist.

Dave
0
 

Author Comment

by:Bright01
ID: 34931815
My apologies for not being clearer.  It's ok to clear everything except the first row since that is the row that we are duplicating.  When adding a row, I'm expecting that all the characteristics of the previous row, including validation are introduced.  It appears that's how it works; so if we only clear the contents of the first row, my thinking is that it should work when we duplicate; then when we clear, the first row has no content but still all the hidden characteristics such as validation or formulas.

Is that more clear?

Thank you,

B.
0
 
LVL 42

Expert Comment

by:dlmille
ID: 34931832
Ok revised the reset procedure to clear contents on all, but to clear the range, offset by one row (it clears a row below the data range because of the offset, but there's no data there, so no need to be more specific)

Sub ResetSolutionFields()
Dim i As Integer, r As Range, bottomRow As Range

    Set bottomRow = Cells(Cells.Rows.Count, 1).End(xlUp)

    Set r = Range(Range("FirstItem"), Cells(bottomRow.Row, Range("itemwidth").Columns.Count))
    Debug.Print "Clearing " & r.Address
        r.ClearContents
        r.Offset(1, 0).Clear 'clears one extra row, but no problem...
   
End Sub

Enjoy!

Dave
AddRowAndDuplicate-r3.xls
0
 

Author Comment

by:Bright01
ID: 34931911
Man you're good.

Thank you.

B.
0

Featured Post

Technology Partners: 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

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
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.
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…
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…

749 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