Solved

Reset to Macro in Spreadsheet

Posted on 2011-02-18
13
309 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 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

792 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