gpt4 book ai didi

Excel VBA - 添加行并激活

转载 作者:行者123 更新时间:2023-12-04 20:42:06 25 4
gpt4 key购买 nike

晚上好

请参阅附件图片以获取我的数据示例。 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/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com