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

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Is there a more efficient way of doing it without having to select each sheet individually?
VBA Code:
    Dim N%
    
    For N = 1 To 3
        With Sheets("Sheet" & N)
            .[B4:E20] = .[B4:E20]
            .Columns("F:Q").Delete
             If strArea = 83 Then .Rows(20).Delete
        End With
    Next
 
Upvote 0
I'm curious.
Why are you using a case statement when your always using 83
I can understand the question, I hadn't include all only because I didn't think it was important. My mistake.

The full code that I'm trying to repeat over three sheets is -
VBA Code:
    Range("B4:E20").Copy
    Range("B4:E20").PasteSpecial Paste:=xlPasteValues
    Columns("F:Q").Delete
    Range("G1").Font.Color = vbWhite
    Select Case strArea
    Case 80
    Rows("17:20").Delete
    Range("A1:E16").Select
    Case 82
    Rows("13:20").Delete
    Range("A1:E12").Select
    Case 83
    Rows("20").Delete
    Range("A1:E19").Select
    Case 84
    Rows("17:20").Delete
    Range("A1:E16").Select
    Case 87
    Rows("17:20").Delete
    Range("A1:E16").Select
    End Select
 
Upvote 0
VBA Code:
    Dim N%
   
    For N = 1 To 3
        With Sheets("Sheet" & N)
            .[B4:E20] = .[B4:E20]
            .Columns("F:Q").Delete
             If strArea = 83 Then .Rows(20).Delete
        End With
    Next
This looks amazing. I have a few questions to better understand.

Dim N% - what does the % imply?

.[B4:E20] = .[B4:E20] - on the surface this looks like I'm simply copying and pasting over the same set of cells, what I'm trying to accomplish is copying a range of cells that is made up of formulas, and paste the end result (values). If this is what your code does, what in the code is telling it to paste the values? It's very interesting

If strArea = 83 Then .Rows(20).Delete - I hadn't included the full "Case" list. Here would I use ElseIf for all the others?
VBA Code:
    Select Case strArea
    Case 80
    Rows("17:20").Delete
    Range("A1:E16").Select
    Case 82
    Rows("13:20").Delete
    Range("A1:E12").Select
    Case 83
    Rows("20").Delete
    Range("A1:E19").Select
    Case 84
    Rows("17:20").Delete
    Range("A1:E16").Select
    Case 87
    Rows("17:20").Delete
    Range("A1:E16").Select
    End Select

Thank you very much for your help on this
 
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
This is definitely what I was asking for, and I'm happy to say that I actually understand most if it.
 
Upvote 0
VBA Code:
    Dim N%
   
    For N = 1 To 3
        With Sheets("Sheet" & N)
            .[B4:E20] = .[B4:E20]
            .Columns("F:Q").Delete
             If strArea = 83 Then .Rows(20).Delete
        End With
    Next
.[B4:E20] = .[B4:E20] - this seems to be deleting everything in the range B4:E20
 
Upvote 0
Dim N% - what does the % imply?
Integer as you can check in VBA help …​
.[B4:E20] = .[B4:E20] - on the surface this looks like I'm simply copying and pasting over the same set of cells, what I'm trying to accomplish is copying a range of cells that is made up of formulas, and paste the end result (values)
.[B4:E20] = .[B4:E20] - this seems to be deleting everything in the range B4:E20
For safety you can add the properties like .[B4:E20].Formula = .[B4:E20].Value2 …​
If strArea = 83 Then .Rows(20).Delete - I hadn't included the full "Case" list. Here would I use ElseIf for all the others?
No, the better is to keep the Select Case block …​
 
Upvote 0
Integer as you can check in VBA help …​


For safety you can add the properties like .[B4:E20].Formula = .[B4:E20].Value2 …​

No, the better is to keep the Select Case block …​
Thank you for the breakdown.

I'm not sure what I'm doing wrong, but after adding the code it doesn't seem to be deleting the columns or changing the formulas to values.

VBA Code:
Option Explicit
Sub Start()
    Dim strQtr As String
    Dim strArea As String
    Dim x As Long
    Dim Sht As Variant
    Dim N%

VBA Code:
    For N = 1 To 3
        With Sheets("Sheet" & N)
        .[B4:E20].Formula = .[B4:E20].Value2
        .Columns("F:Q").Delete
        End With
    Next
 
Upvote 0

The better is you link the workbook on a files host website like Dropbox as I'm a very beginner in guessing anything …​
 
Upvote 0

Forum statistics

Threads
1,215,580
Messages
6,125,654
Members
449,245
Latest member
PatrickL

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