You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Excel VBA如何对指定行范围的I至Z列批量应用公式

场景1:I到Z列使用相同逻辑的公式(支持相对引用自动偏移)

直接调整原代码中Range的范围参数即可,修改后的完整代码如下:

Sub Calculation()
    Dim ws1 As Worksheet
    Dim lastRow As Long
    Set ws1 = Sheets("Sheet 2")

    With ws1
        lastRow = .Range("C" & .Rows.Count).End(xlUp).Row
        ' 直接指定范围为I3到Z列对应最后一行
        .Range("I3:Z" & lastRow).Formula = "=1+2"
    End With
End Sub

如果你的公式使用了相对单元格引用(比如引用同行C列写=C3),赋值到不同列后会自动按偏移量调整,适配大部分使用场景。

场景2:不同列需要写入不同公式

如果I到Z列的公式逻辑存在差异,可以通过遍历列号的方式逐个赋值,示例代码如下:

Sub Calculation()
    Dim ws1 As Worksheet
    Dim lastRow As Long
    Dim colNum As Long ' 存储当前循环的列号
    Set ws1 = Sheets("Sheet 2")

    With ws1
        lastRow = .Range("C" & .Rows.Count).End(xlUp).Row
        ' I列对应列号9,Z列对应列号26,循环遍历每一列
        For colNum = 9 To 26
            ' 可根据列号判断写入对应公式
            Select Case colNum
                Case 9 ' I列
                    .Range(.Cells(3, colNum), .Cells(lastRow, colNum)).Formula = "=1+2"
                Case 10 ' J列
                    .Range(.Cells(3, colNum), .Cells(lastRow, colNum)).Formula = "=3+4"
                ' 其他列的公式规则可自行补充
                Case Else
                    .Range(.Cells(3, colNum), .Cells(lastRow, colNum)).Formula = "=C3" ' 示例默认公式
            End Select
        Next colNum
    End With
End Sub

内容的提问来源于stack exchange,提问作者Siciliana89

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.10 12:42:00