vba - 在每个组下插入总计行

标签 vba excel

我需要在每个组下插入一行总计和一个分页符。

我尝试了以下插入行,但是当我只想要一个时,它插入了不止一行。

Sub macro()
Dim sh1 As Worksheet
Dim i As Long, lastrow1 As Long

Set sh1 = Worksheets("Sheet1")     
lastrow1 = sh1.Cells.SpecialCells(xlCellTypeLastCell).Row

For i = 1 To lastrow1

   If sh1.Cells(i, "A").Value = "sell" Then
      sh1.Cells(i, "A").EntireRow.Insert
   End If
Next i
End Sub

最佳答案

我不是 VBA 专家,但看起来你的代码每次找到“sell”时都会插入一行,因此插入了多行。

插入行后尝试添加中断以使您退出 for 循环。

希望这可以帮助。
啊注意,在 VBA Exit For用于跳出 for 循环
所以你的代码是

Set sh1 = Worksheets("Sheet1")     
lastrow1 = sh1.Cells.SpecialCells(xlCellTypeLastCell).Row

For i = 1 To lastrow1
    If sh1.Cells(i, "A").Value = "sell" Then
       sh1.Cells(i, "A").EntireRow.Insert
       Exit For
    End If
Next i
End Sub

关于vba - 在每个组下插入总计行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1733740/

相关文章:

vba - 运行时错误 1004 - 该命令不能用于多项选择

vba - 检查单元格是否是 Excel 范围的最后一个单元格

vba - Confluence:使用 VBA 更新现有页面

c# - NPOI Mapper - map 列表属性

Excel VBA 执行直到 E 列

excel - 将图片从一张纸复制并粘贴到另一张纸上

excel - 当对相邻单元格进行更改时,自动填充单元格中的日期

string - excel中的iserror公式

php - 一个写入excel的表格

python - 用vba截取一个html文件