Excel VBA:如何在范围中插入新行并复制公式

编程入门 行业动态 更新时间:2024-10-10 17:32:15
本文介绍了Excel VBA:如何在范围中插入新行并复制公式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个命名范围,如下所示:A2:D3

I have a named range like the following covering A2:D3

ITEM PRICE QTY SUBTOTAL 1 10 3 30 1 5 2 10 TOTAL: 40

我正在寻找一些VBA代码将新行插入范围复制公式不值。

I am looking for some VBA code to insert a new row into the range copying the formulas not values.

任何提示/链接都非常感谢。

Any tips/links greatly appreciated.

推荐答案

应该这样做:

Private Sub newRow(Optional line As Integer = -1) Dim target As Range Dim cell As Range Dim rowNr As Integer Set target = Range("A2:D3") If line <> -1 Then rowNr = line Else rowNr = target.Rows.Count End If target.Rows(rowNr + 1).Insert target.Rows(rowNr).Copy target.Rows(rowNr + 1) For Each cell In target.Rows(rowNr + 1).Cells If Left(cell.Formula, 1) <> "=" Then cell.Clear Next cell End Sub

更多推荐

Excel VBA:如何在范围中插入新行并复制公式

本文发布于:2023-10-26 18:10:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530940.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:公式   如何在   Excel   VBA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!