简体   繁体   中英

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

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

You can use the same index that you are using to loop through the worksheets collection.

Dim Y as workbook, copydata as Long, i as Long

Set Y = Thisworkbook

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

For i=3 to worksheets.count
    Y.worksheets(i).Range("F1:x"&copydata).Formula= _
      Y.worksheets("Template").Range("F1:X"&copydata).Formula
next i

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM