Solved

Reset to Macro in Spreadsheet

Posted on 2011-02-18
13
278 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
  • 8
  • 5
13 Comments
 
LVL 41

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 41

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 41

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
 

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 41

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 41

Accepted Solution

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

Enjoy!

Dave
AddRowAndDuplicate-r3.xls
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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 41

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 41

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 41

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

The canonical version of this article is on my web site here: http://iconoun.com/articles/collisions/ A companion presentation is available here: http://iconoun.com/articles/collisions/Unicode_Presentation.pdf
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.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

707 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

9 Experts available now in Live!

Get 1:1 Help Now