VBA excel to copy formula from a worksheet and paste to multiple worksheets

纵然是瞬间 提交于 2019-12-20 05:21:44

问题


VBA excel to copy formula from a sheet and paste to an array of worksheets. As an example, I want to copy data from worksheet Data!C4:CX204 and paste to worksheets Test1:Test50. The Worksheets from Test1:Test50 will be right after each other. I was thinking I can name one cell Test1 and another Test 50 and link to those cells in order to make the array more flexible.

Sub Button4_Click() 
Dim WS_Count As Integer 
Dim I As Integer 

WS_Count = ActiveWorkbook.Worksheets.Count 

Dim Source As Range 
Set Source = ThisWorkbook.Worksheets(1).Range("C10") 

' Begin the loop. 
For I = 1 To WS_Count 
    ThisWorkbook.worksheets(i).Select ' just select the sheet 
    Source.Copy Range("C11:C300").Select 
    ActiveSheet.Paste 
Next I 

End Sub

回答1:


Based on the code you posted, you probably want something like this:

Sub Button4_Click()
Dim src As Range, sel As Range
Dim i as long

Set sel = Selection
Set src = ActiveWorkbook.Sheets("Data").Range("C10")

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

For i = 1 to 50
    src.Copy ActiveWorkbook.Sheets("Test" & i).Range("C11:C300")
Next i

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

Application.Goto sel, False

End Sub



回答2:


Is this what you are looking for? The i = 1 to worksheets count can be changed to what ever you need. The second part of the copy/paste is the destination.

Sub Button4_Click()
Dim I As Integer

' Begin the loop.
For I = 1 To ActiveWorkbook.Worksheets.Count - 1
Worksheets(1).Range("C11:C300").Copy Worksheets("Test" & I).Range("C11:C300")
Next I

End Sub


来源:https://stackoverflow.com/questions/45842369/vba-excel-to-copy-formula-from-a-worksheet-and-paste-to-multiple-worksheets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!