[英]Excel VBA - Copy range from one sheet paste to all sheets after certain sheet in workbook
I feel like this is too simple to be stuck on, but I have a workbook with about 100 sheets, and I need to copy a range from one sheet (Sheet2 Range a1:H200) to Sheet5 AF1:AM200 and every sheet after (Sheet5 through Sheet100 or more). 我觉得这太简单了,无法粘贴,但是我的工作簿大约有100张纸,因此我需要将一个纸页(Sheet2范围a1:H200)复制到Sheet5 AF1:AM200以及(Sheet5)之后的每一页通过Sheet100或更多)。 I have tried creating a loop and copying the original range and pasting to each sheet, but it hasn't worked.
我尝试创建一个循环,然后将原始范围复制并粘贴到每张纸上,但是没有用。 I feel like this is the closest I've gotten
我觉得这是我最近得到的
Sub CopyPasteLoop()
Dim wsVar As Worksheet
For Each wsVar In ThisWorkbook.Sheets
With wsVar
ThisWorkbook.Worksheets("Sheet2").Range("A1:H200").Value = ThisWorkbook.Worksheets("Sheet5").Range("AF1").Value
End With
Next wsVar
End Sub
I feel like it should be simpler, but I can't make it work. 我觉得它应该更简单,但是我无法使其工作。 Thanks!
谢谢!
Almost there. 快好了。 Try this:
尝试这个:
Sub CopyPasteLoop()
Dim wsVar As Worksheet
Dim i as Integer
For i = 5 to ThisWorkbook.Worksheets.Count
ThisWorkbook.Worksheets(i).Range("AF1:AM200").Value = ThisWorkbook.Worksheets("Sheet2").Range("A1:H200").Value
Next i
End Sub
Or for better performance, use this: 为了获得更好的性能,请使用以下命令:
Dim vRange as Variant
vRange = ThisWorkbook.Worksheets(2).range("A1:H200")
Dim i as Integer
For i = 5 to ThisWorkbook.Worksheets.Count
ThisWorkbook.Worksheets(i).Range("AF1:AM200").Value = vRange
Next i
Hopefully @Scott Holtzman's answer will work for you (providing your sheets are indexed in the same order as they're named). 希望@Scott Holtzman的答案对您有用(假设工作表的索引顺序与命名顺序相同)。 This approach will also work.
这种方法也将起作用。
Dim wb As Workbook, ws As Worksheet
Dim rng As Range
Set wb = ThisWorkbook
Set rng = wb.Sheets("Sheet2").Range("A1:H200")
For Each ws In wb.Sheets
If CInt(Right(ws.Name, Len(ws.Name) - Len("Sheet"))) >= 5 Then
ws.Range("AF1:AM200").Value = rng.Value
End If
Next ws
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.