Here is an Excel VBA code snippet that will loop through all the visible sheets in your workbook, copy the range F12:J17
, and paste only the values into the same range for each sheet:
Sub CopyAndPasteValues()
Dim ws As Worksheet
Dim targetRange As Range
' Loop through all worksheets in the workbook
For Each ws In ThisWorkbook.Worksheets
' Check if the worksheet is visible
If ws.Visible = xlSheetVisible Then
' Define the target range
Set targetRange = ws.Range("F12:J17")
' Copy and paste values only
targetRange.Value = targetRange.Value
End If
Next ws
MsgBox "Task completed for all visible sheets!", vbInformation
End Sub
How this Excel VBA code works:
- Checks visibility: It ensures the code works only for unhidden (visible) sheets.
- Copies and pastes values: The
.Value = .Value
assignment replaces formulas or formats with the plain values in the same range. - Loops through all sheets: Automatically processes every unhidden worksheet in the workbook.
Steps to Use:
- Open your Excel file.
- Press
Alt + F11
to open the VBA editor. - Go to
Insert > Module
to create a new module. - Paste the code into the module.
- Close the editor and press
Alt + F8
in Excel. - Select
CopyAndPasteValues
and clickRun
.