Excel VBA - 添加行并激活

标签 excel vba

晚上好

请参阅附件图片以获取我的数据示例。 A 列中的字符串组合在一起。

enter image description here

以下代码是实现以下目标的 WIP...

  • 查找每个交货地点的最后一次出现并在之后添加新行。
  • 在新创建的行中,在名为 Header11-14 的列中,添加一个公式来汇总上述行中的值
  • 做一些格式化

  • 到目前为止,它在每个交货地点之后添加了新行,但我不知道如何添加总和公式。我知道如何添加字符串,但我不知道如何引用上面的单元格......

    enter image description here

    上面的图像是我想要实现的。
    Sub insertRow_totals()
    Dim changeRow, counter As Integer
    
    counter = 2
    
    While Cells(counter, 1) <> ""
        If Cells(counter, 1) <> Cells(counter - 1, 1) Then
            Rows(counter).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    
            counter = counter + 2
        End If
        counter = counter + 1
    Wend
    
    Rows(2).EntireRow.Delete
    End Sub
    

    最佳答案

    您需要计算有多少具有相同名称的行(或记住第一行的行索引),然后这样的事情应该可以工作

    Sub insertRow_totals()
    Dim changeRow, counter As Integer
    
    counter = 2
    FirstRow = 2
    
    While Cells(counter, 1) <> ""
        If Cells(counter, 1) <> Cells(counter - 1, 1) Then
            Rows(counter).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        For i = 11 To 14
            ActiveSheet.Cells(counter, i).Formula = "=SUM(" & Cells(FirstRow, i).Address & ":" & Cells(counter - 1, i).Address & ")"
        Next i
    
        counter = counter + 1
        FirstRow = counter
    End If
        counter = counter + 1
    Wend
    
    Rows(2).EntireRow.Delete
    End Sub
    

    关于Excel VBA - 添加行并激活,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30877131/

    相关文章:

    vba - 取消通过 VBA 显示的 'Save As' 对话框时出现运行时错误

    vba - .End(xlDown) 与 .End(xlUp) 的性能影响

    excel - 用于复制包含从源工作簿到目标工作簿的命名范围的工作表的 VBA 代码

    excel - ActiveX 组件无法创建对象 : 'TDApiOle80.TDConnection'

    vba - 关于如何加快循环的建议

    vba - 如何标准化范围内列出的文件名

    arrays - 填充随机数数组以在Excel vba中求和

    Excel VBA DBEngine.CreateWorkspace 失败说无法加载 DLL

    vba - 关闭工作簿时隐藏特定工作表

    excel - 检查用户选择的范围或取消