如何增加与vba循环范围?
问题描述:
Sub Worksheet_Change()
Set Target = ActiveCell
Application.ScreenUpdating = False
[A1:F20].Copy
[H4].PasteSpecial Paste:=xlPasteValues, Transpose:=True
[A22:F42].Copy
[H24].PasteSpecial Paste:=xlPasteValues, Transpose:=True
[A44:F64].Copy
[H46].PasteSpecial Paste:=xlPasteValues, Transpose:=True
[A66:F86].Copy
[H68].PasteSpecial Paste:=xlPasteValues, Transpose:=True
[A88:F108].Copy
[H90].PasteSpecial Paste:=xlPasteValues, Transpose:=True
[A110:F130].Copy
[H112].PasteSpecial Paste:=xlPasteValues, Transpose:=True
[A132:F152].Copy
[H134].PasteSpecial Paste:=xlPasteValues, Transpose:=True
[A154:F174].Copy
[H156].PasteSpecial Paste:=xlPasteValues, Transpose:=True
[A176:F196].Copy
[H178].PasteSpecial Paste:=xlPasteValues, Transpose:=True
[A198:F218].Copy[H200].PasteSpecial Paste:=xlPasteValues, Transpose:=True
Application.CutCopyMode = False
Target.Select
End Sub
答
你可以试试这个:
Application.ScreenUpdating = False
[A1:F20].Copy
[H4].PasteSpecial Paste:=xlPasteValues, Transpose:=True
With [A22:F42]
For i = 1 To 9
.Offset((i - 1) * 22).Copy
[H24].Offset((i - 1) * 22).PasteSpecial Paste:=xlPasteValues, Transpose:=True
Application.CutCopyMode = False
Next i
End With
Application.ScreenUpdating = True
+0
但是如何做n个表? –
+0
不客气。那么你可能想标记为已被接受。谢谢!如果您希望在每次工作表更改时都这样做,则可以在'ThisWorkbook'代码窗格中的'Private Sub Workbook_SheetChange(ByVal Sh As Object,ByVal Target As Range)'事件处理程序中将该代码放入。当然,你必须引用所有范围到'Sh'工作表对象,你可以通过以下方式轻松完成:1)将所有代码封装在'With Sh ... End With'块中,2)添加一个点('.')在_every_开头括号之前('[')。 – user3598756
请通过缩进这四个空格格式化你的代码;它会在您的文章中显示为代码而不是文本的一部分。此外,你的问题是指一个循环,但我没有看到代码中的任何循环? –
我想应用循环,我已经写了复制和粘贴特殊 –