【问题标题】:How to autofill multiple cells with a formula using dynamic range如何使用动态范围使用公式自动填充多个单元格
【发布时间】:2017-04-07 13:34:03
【问题描述】:

我有一个动态表,它是根据 Cells(4,"D") 和 Cells(6,"D") 的乘法创建的,它给出了总行数。列号是固定的,从 B 到 G 定义。第一行有数据是第 13 行。

我想写一个公式 deltaMassFormula,它在 B 列中引入以下内容:deltaMassFormula = D13 * (G13 - F13) 并自动填充,直到 countRows 等于总行数(乘法变量)

例如,如果乘法 = 4 那么

E13 = D13 * (G13 - F13)

E14 = D14 * (G14 - F14)

E15 = D15 * (G15 - F15)

E16 = D16 * (G16 - F16)

我的代码

 Dim StartCellM As Range    
 Dim lastRow As Long    
 Dim deltaMassFormula As Integer    
 Dim multiplication As Integer

 multiplication = Cells(4, "D").Value * Cells(6, "D").Value    
 countRows = multiplication - 1

 Set StartCellM = Cells(13, "E")    
 Set lastRow = Cells(13, "E") + countRows

 deltaMassFormula = Cells(13, "D") * (Cells(13, "G") - Cells(13, "F"))

 With ThisWorkbook.Sheets(1)     
    .Range("E13").Formula = deltaMassFormula    
    .Range("E13:E" & lastRow).FillDown    
 End With

有人可以帮助我吗?

【问题讨论】:

  • 您发布的代码遇到了什么问题?
  • 问题是代码没有填满E13下面的单元格。

标签: vba excel autofill


【解决方案1】:

这会将实际公式填充到范围中,然后将其替换为值。我还将 lastrow 更改为 long,以便它可以正确地用于其他行并从您的范围中拉出起始行。

 Dim StartCellM As Range
 Dim lastRow As Long
 Dim multiplication As Integer

 With ThisWorkbook.Sheets(1) 
     multiplication = .Range("D4").Value * .Range("D6").Value
     countRows = multiplication - 1

     Set StartCellM = .Range("E13")
     lastRow = StartCellM.Row + countRows

    .Range("E13:E" & lastRow).Formula = "=D13*(G13-F13)"
    .Range("E13:E" & lastRow).Value = .Range("E13:E" & lastRow).Value 'Remove this line to keep formula
 End With

如果您更改起始单元格所在的行并且不更改公式中的行以匹配,则公式将中断,因此此代码将在公式中使用来自StartCellM 的行,而不是对其进行硬编码。

Dim StartCellM As Range
Dim lastRow As Long, startRow As Long
Dim multiplication As Integer

With ThisWorkbook.Sheets(1)
    multiplication = .Range("D4").Value * .Range("D6").Value
    countRows = multiplication - 1

    Set StartCellM = .Range("E13")

    startRow = StartCellM.Row
    lastRow = startRow + countRows

    .Range("E" & startRow & ":E" & lastRow).Formula = "=D" & startRow & "*(G" & startRow & "-F" & startRow & ")"
    .Range("E" & startRow & ":E" & lastRow).Value = .Range("E" & startRow & ":E" & lastRow).Value 'Remove this line to keep formula
End With

我还移动了With 中的所有内容,因此它会从正确的工作表中提取所有值。

【讨论】:

  • 谢谢 tjb1。它完美地工作。我现在将比较两个代码,看看缺少什么。
  • 如果您使用此解决方案,我建议检查(并且可能使用)“value2”而不是“value”。 (对于它们之间的差异,只需谷歌它......)
  • 使用Value2代替Value,计算时间有什么不同吗?我去搜索差异,它主要在 Value2 中,它不包括日期或货币。由于我是在定义变量类型,它对这个解决方案有何影响?
  • 如果在工作表中将其格式化为日期,则使用valuevalue2 将返回不同的值。
  • 据我所知,它不取决于您的变量类型,而是取决于您的单元格格式。例如:将单元格 E5 格式化为货币,然后运行此宏:Sub test() Dim i As Double Cells(4, 5).Value = 12.12345 Cells(5, 5).Value = 12.12345 i = Cells(5, 5).Value Cells(7, 5).Value = i i = Cells(5, 5).Value2 Cells(8, 5).Value = i End Sub 它将值 12.12345 输入到单元格 E4 和 E5 中。然后它将单元格 E5 的值输入到 Double 变量中,然后将 value 输入到 E7 中,将 value2 输入到 E8 中。使用 value 会丢失精度,而使用 value2 会保持精度。
【解决方案2】:

它用公式填充 B 列:

Sub filler()

    Dim i As Long
    Dim startRow As Long
    Dim lastRow As Long
    Dim multiplication As Integer

    multiplication = Cells(4, 4).Value * Cells(6, 4).Value

    startRow = 13
    lastRow = startRow + multiplication - 1

    For i = startRow To lastRow
        Cells(i, 2).Formula = "=D" & i & "*(G" & i & "-F" & i & ")"
    Next i

End Sub

【讨论】:

  • tretom,您首先定义起始单元格的公式,然后向下移动单元格的位置,对吗?我真的很擅长编程。我从 vba 开始。
  • 哦,抱歉,我只是忘了删除你的 deltaMassFormula。我没有使用它。您只需要告诉第一行和最后一行,然后遍历行,并设置给定单元格的公式属性 - 在本例中为 cell(i, 2),当您使用它时可以写什么: cell(i , "B") - 到字符串,建立在等式的右侧
  • 明白。谢谢特雷姆。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-05
  • 2023-02-02
  • 1970-01-01
  • 1970-01-01
  • 2014-10-26
  • 1970-01-01
相关资源
最近更新 更多