repeat procedure on three sheets

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
723
Office Version
  1. 2016
Platform
  1. Windows
I have a bit of code that I need to use with Sheet1, Sheet2, and Sheet3.
Is there a more efficient way of doing it without having to select each sheet individually?

Thank you

VBA Code:
    Sheets("Sheet1").Select
    Range("B4:E20").Copy
    Range("B4:E20").PasteSpecial Paste:=xlPasteValues
    Columns("F:Q").Delete
    Select Case strArea
    Case 83
    Rows("20").Delete
    End Select
    Sheets("Sheet2").Select
    Range("B4:E20").Copy
    Range("B4:E20").PasteSpecial Paste:=xlPasteValues
    Columns("F:Q").Delete
    Select Case strArea
    Case 83
    Rows("20").Delete
    End Select
    Sheets("Sheet3").Select
    Range("B4:E20").Copy
    Range("B4:E20").PasteSpecial Paste:=xlPasteValues
    Columns("F:Q").Delete
    Select Case strArea
    Case 83
    Rows("20").Delete
    End Select
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Try this:
I assume str
VBA Code:
Sub My_Script()
'Modified  9/30/2021  6:41:22 PM  EDT
Application.ScreenUpdating = False

With Sheets("Sheet1")
    .Range("B4:E20").Copy
    .Range("B4:E20").PasteSpecial Paste:=xlPasteValues
    .Columns("F:Q").Delete
    Select Case strArea
    Case 83
        .Rows("20").Delete
    End Select
    End With
   
    With Sheets("Sheet2")
     .Range("B4:E20").Copy
     .Range("B4:E20").PasteSpecial Paste:=xlPasteValues
    .Columns("F:Q").Delete
   
    Select Case strArea
        Case 83
        .Rows("20").Delete
    End Select
    End With
   
    With Sheets("Sheet3")
        .Range("B4:E20").Copy
        .Range("B4:E20").PasteSpecial Paste:=xlPasteValues
        .Columns("F:Q").Delete
            Select Case strArea
                Case 83
                    .Rows("20").Delete
            End Select
  End With
Application.ScreenUpdating = True
 
  End Sub
 
Upvote 0
Try this:
VBA Code:
Sub My_Script()
'Modified  9/30/2021  6:41:22 PM  EDT
Application.ScreenUpdating = False

With Sheets("Sheet1")
    .Range("B4:E20").Copy
    .Range("B4:E20").PasteSpecial Paste:=xlPasteValues
    .Columns("F:Q").Delete
    Select Case strArea
    Case 83
        .Rows("20").Delete
    End Select
    End With
   
    With Sheets("Sheet2")
     .Range("B4:E20").Copy
     .Range("B4:E20").PasteSpecial Paste:=xlPasteValues
    .Columns("F:Q").Delete
   
    Select Case strArea
        Case 83
        .Rows("20").Delete
    End Select
    End With
   
    With Sheets("Sheet3")
        .Range("B4:E20").Copy
        .Range("B4:E20").PasteSpecial Paste:=xlPasteValues
        .Columns("F:Q").Delete
            Select Case strArea
                Case 83
                    .Rows("20").Delete
            End Select
  End With
Application.ScreenUpdating = True
 
  End Sub
I like what you did, you taught me a few new tricks. Thank you. What I thought could be done was something like-
Not any sort of VB here, just thinking out loud

For Sheets1,Sheets2,Sheets3
Do
.Range("B4:E20").Copy
.Range("B4:E20").PasteSpecial Paste:=xlPasteValues
.Columns("F:Q").Delete
Select Case strArea
Case 83
.Rows("20").Delete
End Select

Thank you
 
Upvote 0
I like what you did, you taught me a few new tricks. Thank you. What I thought could be done was something like-
Not any sort of VB here, just thinking out loud

For Sheets1,Sheets2,Sheets3
Do
.Range("B4:E20").Copy
.Range("B4:E20").PasteSpecial Paste:=xlPasteValues
.Columns("F:Q").Delete
Select Case strArea
Case 83
.Rows("20").Delete
End Select

Thank you
I assume strArea is a named range
Glad I was able to help you.
Come back here to Mr. Excel next time you need additional assistance.
 
Upvote 0
How about ...

VBA Code:
Sub Example288enzo()
    Dim Sht As Variant
    For Each Sht In Array("Sheet1", "Sheet2", "Sheet3")
        With ThisWorkbook.Worksheets(Sht)
            .Range("B4:E20").Value = .Range("B4:E20").Value
            .Columns("F:Q").Delete
            Select Case strArea
            Case 83
                .Rows("20").Delete
            End Select
        End With
    Next
End Sub
 
Upvote 0
Solution
I'm curious.
Why are you using a case statement when your always using 83
 
Upvote 0
How about ...

VBA Code:
Sub Example288enzo()
    Dim Sht As Variant
    For Each Sht In Array("Sheet1", "Sheet2", "Sheet3")
        With ThisWorkbook.Worksheets(Sht)
            .Range("B4:E20").Value = .Range("B4:E20").Value
            .Columns("F:Q").Delete
            Select Case strArea
            Case 83
                .Rows("20").Delete
            End Select
        End With
    Next
End Sub
I will say this is a nicer way.
 
Upvote 0
In this case the OP could have used
VBA Code:
If strArea = 83 then
    .Rows("20").Delete
End If
but since we don't have al the OP's code, we do not know what strArea is referring to.
 
Upvote 0
In this case the OP could have used
VBA Code:
If strArea = 83 then
    .Rows("20").Delete
End If
but since we don't have al the OP's code, we do not know what strArea is referring to.
Yes I know about case statements but he used case 38 several time in the script as only once would have worked it looks like to me.
 
Upvote 0
he used case 38 several time in the script as only once would have worked it looks like to me.

Then you're missing the point of it all, since the OP was looking for more densed code to act on three different sheets, so each time there's a check needed to see whether row 20 on that particular sheet has to be deleted or not.
 
Upvote 0

Forum statistics

Threads
1,215,540
Messages
6,125,409
Members
449,223
Latest member
Narrian

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