?
Solved

Reset to Macro in Spreadsheet

Posted on 2011-02-18
13
Medium Priority
?
326 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 2000 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

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

801 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