首页 > 解决方案 > How to paste values across multiple worksheets in Excel VBA

问题描述

Could anyone share knowledge about how to paste values from one worksheet to multiple worksheets, starting from a specific column?

I want to copy formulas from the worksheet ("Template") and paste it to other worksheets starting from the column F1 all the way to the last column/row. Following is my codes. I was stuck from Line 5 and can't figure a way to paste values without looking at the names of the sheets. Could anyone advise how to solve this challenge? Thank you :)

Dim Y as workbook
Set Y = Thisworkbook
Dim copydata as Long
copydata=y.worksheets("Template").UsedRange.Rows.Count
Dim i as Long
For i=3 to sheets.count
Y.worksheets("name").Range("F1:x"&copydata).Formula= _
Y.worksheets("Template").Range("F1:X"&copydata).Formula

标签: excelvba

解决方案


Here's the code from my comment above. Suggestion is to switch to a For Each loop to iterate through all of the worksheets in your workbook. You can employ an If block to test and skip any sheet where you don't want to copy this formula ("Template" would be the one I know for a fact you should skip).

Dim Y as workbook
Set Y = Thisworkbook

Dim copydata as Long
copydata=y.worksheets("Template").UsedRange.Rows.Count

Dim ws As Worksheet
For Each ws in Y.Worksheets
    If ws.Name <> "Template" Then ws.Range("F1:x"&copydata).Formula = _
            y.worksheets("Template").Range("F1:X"&copydata).Formula
Next ws

推荐阅读