将公式复制到动态列中(没有 header)

Copy a formula down a dynamic column (without header)

我有table,会定期更新。由于会插入列,所以我没有固定的范围。

我找到了一种方法来查找特定值 ("x") 和 select 下面的单元格。我设法更新了所有必要的公式并将它们打印在 header.
下方的单元格中 它一直工作到复制点。

我试图将公式复制到列中:

Sub Sum_three_months()

    Set Three_months = Range("A1:ZZ10000").Find("x")
    Three_months.Select
    FormularCell = ActiveCell.Offset(1, 0).Select
    Selection.Resize(Selection.Rows.Count, _
    Selection.Columns.Count).Select

    ActiveCell.FormulaR1C1 = "=SUM(R[-0]C[-4]:R[-0]C[-2])"
    
    Sum_six_months ' starts the next update, but has the same format as above
End Sub

我尝试了 AutofillFillDown,但是使用自动填充时我在 range/selected 单元格中遇到错误,而使用 Filldown 它只是复制了 header.

请试试这个代码:

Sub Sum_three_months()
  Dim sh As Worksheet, Three_months As Range, rngLast As Range, x As String, lastCol As Long
 
  Set sh = ActiveSheet 'use here your sheet
  lastCol = sh.Cells(1, Columns.Count).End(xlToLeft).Column
  x = "x" 'use here your search string...
  Set Three_months = sh.Range(sh.Cells(1, 1), sh.Cells(1, lastCol)).Find(x)
  If Not Three_months Is Nothing Then
      Set rngLast = sh.Cells(sh.Cells(Rows.count, Three_months.Offset(0, -4).Column).End(xlUp).row, Three_months.Column)
    
      sh.Range(Three_months.Offset(1, 0), rngLast).formula = _
                   "=SUM(" & sh.Range(Three_months.Offset(1, -4), Three_months.Offset(1, -2)).address(0, 0) & ")"
  Else
     MsgBox "Not a ""x"" range could be found...": Exit Sub
  End If
    
 Sum_six_months ' starts the next update, but has the same format as above
End Sub