|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
从楼主的附件内容看,其实际操作场景是不做行删除的。
即便删除了行,导致公式出现了#Ref!错误,手动拖曳公式复制下即可。
有兴趣的话,看看我的代码(参照你的代码思路作了大的变动):
- Private Sub Worksheet_SelectionChange(ByVal Target As Range)
- Dim theRow&, theEndRow&, theAns&
- '
- With Target
- If .EntireRow.Address = .Address Then
- theRow = .Row
- If theRow > 6 Then
- With .Parent
- If WorksheetFunction.CountA(.Range(.Cells(theRow, 4), .Cells(theRow, 5))) = 0 Then
- theAns = MsgBox("插入行吗?", vbYesNo + vbExclamation, "确认")
- If theAns = vbYes Then
- .Cells(theRow, 1).Resize(Target.Rows.Count).EntireRow.Insert Shift:=xlDown
- theEndRow = .Cells(.Rows.Count, 6).End(xlUp).Row
- If theEndRow > theRow + Target.Rows.Count - 1 Then
- .Range(.Cells(7, 6), .Cells(theEndRow, 6)).FormulaR1C1 = "=IF(RC[-2]+RC[-1]=0,"""",R[-1]C+RC[-2]-RC[-1])"
- Else
- .Range(.Cells(7, 6), .Cells(theRow + Target.Rows.Count - 1, 6)).FormulaR1C1 = "=IF(RC[-2]+RC[-1]=0,"""",R[-1]C+RC[-2]-RC[-1])"
- End If
- Application.EnableEvents = False
- .Cells(theRow, 1).Select
- Application.EnableEvents = True
- End If
- End If
- End With
- End If
- End If
- End With
- End Sub
复制代码
|
|