Loop through worksheets - convert used range to values - NOT LOOPING THROUGH SHEETS :(

jwb1012

Board Regular
Joined
Oct 17, 2016
Messages
167
Hello, I am attempting to loop through all of my worksheets and if the worksheet begins with "Labor BOE" I would like to copy/paste everything as values (there are formulas all over the sheet that I want to paste as values). Unfortunately, all of my attempts only work on the current "Labor BOE" worksheet that is open and doesn't loop through the others to paste values. Any thoughts?



Last row determined by column T.
Last column = Column U (aka Column 21)

Attempt #1: works on active sheet, doesn't loop through all sheets

Code:
Sub Paste_Values_2()
    'STEP 1: Define variables
    Dim Sh As Worksheet
    Dim A As Long
    Dim Dest_End_Row As Long
    
        For A = 1 To Sheets.Count
            If Left(Sheets(A).Name, 9) = "Labor BOE" Then
                Dest_End_Row = Sheets(A).Cells(Rows.Count, "T").End(xlUp).Row
                    With Sheets(A).Range(Cells(1, 1), Cells(Dest_End_Row, 21))
                        .Value = .Value
                    End With
            End If
        Next
End Sub


Attempt 2:

Code:
Sub Paste_Values_2()
    'STEP 1: Define variables
    Dim Sh As Worksheet
    Dim A As Long
    Dim Dest_End_Row As Long
    
        For Each Sh In ActiveWorkbook.Worksheets
            If Left(Sh.Name, 9) = "Labor BOE" Then
                Dest_End_Row = Sh.Cells(Rows.Count, "T").End(xlUp).Row
                    With Range(Cells(1, 1), Cells(Dest_End_Row, 21))
                        .Value = .Value
                    End With
            End If
        Next Sh
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try...

Code:
Sub Paste_Values_2()
    'STEP 1: Define variables
    Dim Sh As Worksheet
    Dim A As Long
    Dim Dest_End_Row As Long
    
        For Each Sh In ActiveWorkbook.Worksheets
            If Left(Sh.Name, 9) = "Labor BOE" Then
                Dest_End_Row = Sh.Cells(Rows.Count, "T").End(xlUp).Row
                    With [COLOR="#FF0000"]Sh.[/COLOR]Range([COLOR="#FF0000"]Sh.[/COLOR]Cells(1, 1), [COLOR="#FF0000"]Sh.[/COLOR]Cells(Dest_End_Row, 21))
                        .Value = .Value
                    End With
            End If
        Next Sh
End Sub

or

Code:
Sub Paste_Values_2()
    'STEP 1: Define variables
    Dim Sh As Worksheet
    Dim A As Long
    Dim Dest_End_Row As Long
    
        For A = 1 To Sheets.Count
            If Left(Sheets(A).Name, 9) = "Labor BOE" Then
                Dest_End_Row = Sheets(A).Cells(Rows.Count, "T").End(xlUp).Row
                    With [COLOR="#FF0000"]Sheets(A).[/COLOR]Range([COLOR="#FF0000"]Sheets(A).[/COLOR]Cells(1, 1), [COLOR="#FF0000"]Sheets(A).[/COLOR]Cells(Dest_End_Row, 21))
                        .Value = .Value
                    End With
            End If
        Next
End Sub
 
Last edited:
Upvote 0
you could also simply activate each sheet.....but I'd go with Marks solution

Code:
Sub Paste_Values_2()
    'STEP 1: Define variables
    Dim Sh As Worksheet, A As Long, Dest_End_Row As Long
        For Each Sh In Worksheets
         sh.activate
          If Left(Sh.Name, 9) = "Labor BOE" Then
                Dest_End_Row = Sh.Cells(Rows.Count, "T").End(xlUp).Row
                    With Range(Cells(1, 1), Cells(Dest_End_Row, 21))
                        .Value = .Value
                    End With
            End If
        Next Sh
End Sub
 
Upvote 0
Try this:
Code:
Sub Convert_Values()
Application.ScreenUpdating = False
Dim i As Long
    
        For i = 1 To Sheets.Count
            If Left(Sheets(i).Name, 9) = "Labor BOE" Then
                With Sheets(i).UsedRange
                .Value = .Value
                End With
            End If
        Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,122
Members
452,381
Latest member
Nova88

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