excel - 复制一行并仅粘贴那些包含公式的单元格
问题描述
我有一个命名范围。我正在通过代码插入一个新行并调整命名范围的大小。有些列包含公式,有些列包含值。我想从上面的单元格中复制公式。下面是我的代码:
Sub InsertRow()
Dim lrInputRange As Long
Set wbMacro = ThisWorkbook
Set wsInput = wbMacro.Sheets("INPUT")
'Insert new row at bottom
lrInputRange = wsInput.Range("Input_Range").Rows.Count
wsInput.Rows(lrInputRange + 1).EntireRow.Insert shift:=xlUp
wsInput.Rows(lrInputRange).EntireRow.Copy
wsInput.Rows(lrInputRange + 1).PasteSpecial xlPasteFormats
wsInput.Rows(lrInputRange).EntireRow.Copy
'Here I want to paste only those cells that contains Formulas
Application.CutCopyMode = False
'Resize the range
ThisWorkbook.Names.Add Name:="Input_Range", _
RefersTo:=Range("Input_Range").Resize(Range("Input_Range").Rows.Count + 1)
End Sub
解决方案
用于SpecialCells()
选择没有公式的单元格并清除它们:
... your previuous code
wsInput.Rows(lrInputRange).EntireRow.Copy
wsInput.Rows(lrInputRange + 1).PasteSpecial
wsInput.Rows(lrInputRange + 1).SpecialCells(xlCellTypeConstants).ClearContents
'Resize the range
. rest of your code
推荐阅读
- python - ROS2 python节点位置
- kubernetes - 如何在 kubectl 中将 READY、STATUS、RESTARTS、AGE 等作为自定义列?
- python - 连续绘制两个不同的 Matplotlib 图
- css - Firefox RDM Touch Simulation 不模拟悬停?
- python - 如何在 for 循环中使用多处理
- android - 如何将数据推送到固件 11 上的 sdcard/Android/数据
- python - Keras模型类总结
- java - Angular 10上传zip,获取路径
- typescript - 使用 CDK 将 AWS SM 密钥传递到 Lambda 环境
- react-native - 如何在 UI Kitten 中使用自定义主题和运行时主题?