【问题标题】:how to increase ranges in loop with vba?如何使用vba增加循环范围?
【发布时间】:2016-12-16 12:00:56
【问题描述】:
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

【问题讨论】:

  • 请通过缩进四个空格来格式化您的代码;它将在您的帖子中显示为代码而不是文本的一部分。另外,您的问题是指一个循环,但我在代码中没有看到任何循环?
  • 我想在我反复写过 copy 和 pasteSpecial 的地方应用循环

标签: vba imacros


【解决方案1】:

你可以试试这个:

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

【讨论】:

  • 但是对于 n 个表该怎么办?
  • 不客气。然后您可能希望将 asnwer 标记为已接受。谢谢!如果您想在每次工作表更改时都这样做,那么您可以将该代码放在ThisWorkbook 代码窗格中的Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) 事件处理程序中。当然,您必须将所有范围引用到 Sh 工作表对象,您可以通过以下方式轻松完成:1) 将所有代码包装在 With Sh .... End With 块中,以及 2) 在 every 之前添加一个点 (.) i> 左括号 ([) .
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-03
  • 2023-03-04
  • 1970-01-01
  • 2020-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多