Link to home
Start Free TrialLog in
Avatar of Bright01
Bright01Flag for United States of America

asked on

Reset to Macro in Spreadsheet

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
Avatar of dlmille
dlmille
Flag of United States of America image

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

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
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
Avatar of Bright01

ASKER

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.
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
ASKER CERTIFIED SOLUTION
Avatar of dlmille
dlmille
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You know Dave, it's always harder than it seems at first.  Thanks for sticking with me on this!  

Best regards,

B.
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
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.
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
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.
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
Man you're good.

Thank you.

B.