Excel VBA: Copy Paste task repeat to all sheets

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:

  1. Checks visibility: It ensures the code works only for unhidden (visible) sheets.
  2. Copies and pastes values: The .Value = .Value assignment replaces formulas or formats with the plain values in the same range.
  3. Loops through all sheets: Automatically processes every unhidden worksheet in the workbook.

Steps to Use:

  1. Open your Excel file.
  2. Press Alt + F11 to open the VBA editor.
  3. Go to Insert > Module to create a new module.
  4. Paste the code into the module.
  5. Close the editor and press Alt + F8 in Excel.
  6. Select CopyAndPasteValues and click Run.

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart
Scroll to Top
× Chat