Cleaning Entire Row content from two worksheets

moskita

New Member
Joined
Feb 9, 2017
Messages
20
Hello my friends! I have an userform that records the same content at two Worksheets (Worksheet1 and Worksheet 2).
Whenever I need to erase an entire row content, I select a cell, click at trash can icon and it runs this code above:

Code:
Sub EraseActivity_Click()


Worksheets("Worksheet1").Unprotect 123
MsgBox "Content erased"
Selection.EntireRow.ClearContents
Worksheets("Worksheet1").Protect 123


End Sub

But now I'm looking for help to erase the same content from both Worksheets.
Always when I run my userform, content is registered at both Worksheets, but at Worksheet 1 it's recorded 8 rows above comparing Worksheet2, for example:
Worksheet 1 = Content registered at row 10
Worksheet 2 = Same content registered at row 2

So, I need to run "Selection.EntireRow.ClearContents" at Worksheet 1 and "Selection.EntireRow.ClearContents" less 8 rows at Worksheet 2 to clean both of content at same time and I don't know how to do it.

Could you help me? :)
Since now I appreciate your time spent to help me. Have a nice New Year!
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Test the follow:

Code:
Sub EraseActivity_Click()
    fila = ActiveCell.Row
    If fila < 9 Then
        MsgBox "You can not delete this row because it does not exist on the Worksheet2"
        Exit Sub
    End If
    Worksheets("Worksheet1").Unprotect 123
    Selection.EntireRow.ClearContents
    Worksheets("Worksheet1").Protect 123
    '
    Worksheets("Worksheet2").Unprotect 123
    Worksheets("Worksheet2").Rows(fila - 8).EntireRow.ClearContents
    Worksheets("Worksheet2").Protect 123
    '
    MsgBox "Content erased"
End Sub

Regards Dante Amor
 
Upvote 0
Hi Dante Amor, thank you for replying.
Everything works fine, thank you so much! But I can see excel switching worksheets very fast and doing this operation :laugh:, how can I hidde it?
 
Last edited:
Upvote 0
Add this row at the very top of your code (after the Sub line):
Code:
Application.ScreenUpdating = False
and then enter this line at the very end (before the End Sub line):
Code:
Application.ScreenUpdating = True
 
Upvote 0
Thank you so much!
By the way, what if I wanted to clean a range between Columns "A:G", I:I and "K:L", replacing

Worksheets("Worksheet2").Rows(fila - 8).EntireRow.ClearContents

to

Worksheets("Worksheet2").Rows(fila - 8).Columns("A:G, I:I, K:L").ClearContents

Shall work?
 
Upvote 0
No, I don't think that will work.

Try this:
Code:
    Dim rng As Range
    Set rng = Intersect(Worksheets("Worksheet2").Rows(fila - 8), Worksheets("Worksheet2").Range("A:G, I:I, K:L"))
    rng.ClearContents
 
Upvote 0
This:


Code:
Sub EraseActivity_Click()
    f = ActiveCell.Row
    If f < 9 Then
        MsgBox "You can not delete this row because it does not exist on the Worksheet2"
        Exit Sub
    End If
    Application.ScreenUpdating = False
    Worksheets("Worksheet1").Unprotect 123
    Worksheets("Worksheet1").Range("A" & f & ":G" & f & ",I" & f & ",K" & f & ":L" & f).ClearContents
    Worksheets("Worksheet1").Protect 123
    '
    f = f - 8
    Worksheets("Worksheet2").Unprotect 123
    Worksheets("Worksheet2").Range("A" & f & ":G" & f & ",I" & f & ",K" & f & ":L" & f).ClearContents
    Worksheets("Worksheet2").Protect 123
    '
    Application.ScreenUpdating = True
    MsgBox "Content erased"
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,900
Messages
6,175,276
Members
452,629
Latest member
SahilPolekar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top