vba - 公式自动填充多列,使用 VBA 中的特定单元格

标签 vba excel formula autofill

特定细胞:K4 和 K8

包含公式所需数据的列:A(名为 devm 的值)、C(名为 slice 的值)和 D(名为 point 的值)

包含公式的列:E、F 和 G(希望将这些值命名为角度、devmm 和高度)

进入E1的公式:
=(D1 - 1) * $K$8

进入F1的公式:
=A1 * 1000

进入G1的公式:
=(C1 - 1) * $K$4

^^^这些仅与第 1 行有关

我希望我的宏将这些公式输入到这些第 1 行单元格中,然后自动填充到最后一个事件行(我已经对其代码进行了排序)。我的代码的相关部分如下。

K = 1
ender = Tier * increment
last = LastTier * increment
starter = ender - (increment - 1)
If starter = 0 Then
    starter = 1
End If

sheetname1 = "Sheet1"
ActiveSheet.Name = sheetname1
ActiveSheet.Range("K2") = TankHeight
ActiveSheet.Range("K3") = LastTier - 1
ActiveSheet.Range("K4").Formula = "=$K$2/$K$3"
ActiveSheet.Range("K6").Value = 360
ActiveSheet.Range("K7") = increment
ActiveSheet.Range("K8").Formula = "=$K$6/$K$7"

' ********************************************************************
Set Range1 = Range("A1:J65536")
With Range1
    Rows(last + 2).Delete
End With

For K = starter To ender
    Devm = ActiveSheet.Range("A" & K).Value
    Rad = ActiveSheet.Range("B" & K).Value
    slice = ActiveSheet.Range("C" & K).Value
    point = ActiveSheet.Range("D" & K).Value
    ' ***Automation settings for Formulas and Autofill down to last***
    ActiveSheet.Range("E1").Formula = "=(D1-1)*$K$8"
    ActiveSheet.Range("F1").Formula = "=A1*1000"
    ActiveSheet.Range("G1").Formula = "=(C1-1)*$K$4"
    Angle = ActiveSheet.Range("E" & K).Value
    Devmm = ActiveSheet.Range("F" & K).Value
    height = ActiveSheet.Range("G" & K).Value

    K = K + 1

    ActiveSheet.Range("C1").Select

Next

最佳答案

不需要循环。您可以一次在所有单元格中输入公式

Range("E1:E" & lastRow).Formula = "=(D1 - 1) * $K$8"
Range("F1:F" & lastRow).Formula = "=A1 * 1000"
Range("G1:G" & lastRow).Formula = "=(C1 - 1) * $K$4"

在哪里 lastRow是列中的最后一行。

您可以使用“Error in finding last used cell in VBA”找到它。

关于vba - 公式自动填充多列,使用 VBA 中的特定单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25621448/

相关文章:

vba - Excel VBA 搜索日期范围并删除旧条目

homebrew - 在 Mac OS Catalina 上安装 Wine。错误 : No available formula with the name "wine"

algorithm - 我不明白如何实现的概率(指数?)推理公式

c - 用于 RuneScape 体验的 C 数学公式

excel - 在单个单元格中查找多个短语计数

VBA宏将数据从一个excel文件复制到另一个

excel - Do Until 循环不起作用。有任何想法吗?

excel - DataSource.Error AnalysisServices : The XML for Analysis request timed out before it was completed. 超时值:3600 秒

c# - 检查是否选择了整张纸?

excel - 如何使用循环简化我的代码