【问题标题】:Drag formulas from Range with several variables, Error 1004从具有多个变量的 Range 中拖动公式,错误 1004
【发布时间】:2021-02-12 17:32:07
【问题描述】:

我的任务是根据一个特定列的长度(长度可以变化)将公式拖到几列中。

我设法做到了,但必须为每个范围创建新的代码行。
我的变体 1 示例:

Sub DragRows()
        
    Dim LastRowEPS As Integer
    Dim tr As Range
    
    Set tr = Range("A14:G" & LastRowEPS)
    
    Range("A14:G14").Select
    Selection.AutoFill Destination:=tr, Type:=xlFillDefault
    
    Set tr = Range("I14:K" & LastRowEPS)
    
    Range("I14:K14").Select
    Selection.AutoFill Destination:=tr, Type:=xlFillDefault
    
End Sub

我想优化我的代码并在一行代码中包含多个变量范围。
这是我的变体 2:

Sub DragRows()
    
    Dim LastRowEPS As Integer
    Dim tr As Range
    
    LastRowEPS = Sheet1.Cells(14, 12).End(xlDown).Row
    
    Set tr = Range("A14:G" & LastRowEPS & ", I14:K" & LastRowEPS & ", M14:N" & LastRowEPS & ", P14:P" & LastRowEPS)
    
    Range("A14:G14,I14:K14,M14:N14,P14:P14").Select
    Selection.AutoFill Destination:=tr, Type:=xlFillDefault
    
End Sub

选择过程有效,tr.Range 定义正确,但 VBA 自动填充显示错误:

运行时错误“1004”:Range 类的 AutoFill 方法失败

是否可以包含几个变量范围作为自动填充的目标或任何其他方式来优化我的代码?

【问题讨论】:

    标签: excel vba range autofill


    【解决方案1】:

    实际上我找到了一个我正在寻找的解决方案。也许对某人会有所帮助。 .AutoFill 对多个范围不好,这就是为什么简单使用 .FillDown 是这里的答案:

    Sub DragRows()
    
    Dim LastRowEPS As Integer
    Dim tr As Range
    
    LastRowEPS = Sheet1.Cells(14, 12).End(xlDown).Row
    
    Set tr = Sheet1.Range("A14:G" & LastRowEPS & ", I14:K" & LastRowEPS & ", M14:N" & LastRowEPS & ", P14:P" & LastRowEPS)
    tr.FillDown
    
    End Sub
    

    【讨论】:

      【解决方案2】:

      自动填充、公式、向下填充

      • 下面展示了两种不同的方法。
      • OP 已经揭示了优越的解决方案,这将在第二个过程中介绍。

      三种解法(下:应用于四个范围)

      Sub dragRows()
          ' Define constants.
          Const FirstRow As Long = 14
          Const LastRowCol As Long = 12
          Dim Cols As Variant
          Cols = Array("A:G", "I:K", "M:N", "P")
          ' In worksheet...
          With Sheet1
              ' Determine Rows Count.
              Dim RowsCount As Long
              RowsCount = .Cells(FirstRow, LastRowCol).End(xlDown).Row - FirstRow + 1
              ' Declare variables
              Dim rng As Range
              Dim n As Long
              ' Define and fill each range.
              For n = LBound(Cols) To UBound(Cols)
                  Set rng = .Columns(Cols(n)).Rows(FirstRow)
                  ' Choose one of the following solutions
                  rng.AutoFill Destination:=rng.Resize(RowsCount), Type:=xlFillDefault
                  'rng.Resize(RowsCount).Formula = rng.Formula
                  'rng.Resize(RowsCount).FillDown
              Next n
          End With
      End Sub
      

      FillDown 解决方案(优越:应用于一个范围)

      Sub dragRowsFillDown()
          ' Define constants.
          Const FirstRow As Long = 14
          Const LastRowCol As Long = 12
          Dim Cols As Variant
          Cols = Array("A:G", "I:K", "M:N", "P")
          ' In worksheet...
          With Sheet1
              ' Determine Rows Count.
              Dim RowsCount As Long
              RowsCount = .Cells(FirstRow, LastRowCol).End(xlDown).Row - FirstRow + 1
              ' Declare variables
              Dim rng As Range
              Dim n As Long
              ' Define (non-contiguous) range.
              For n = LBound(Cols) To UBound(Cols)
                  If Not rng Is Nothing Then
                      Set rng = Union(rng, .Columns(Cols(n)).Rows(FirstRow) _
                                                            .Resize(RowsCount))
                  Else
                      Set rng = .Columns(Cols(n)).Rows(FirstRow).Resize(RowsCount)
                  End If
              Next n
          End With
          rng.FillDown
      End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-24
        • 2015-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多