我有一行,其中有使用同一行的值的公式。下一行是空的,只是背景颜色不同。
现在,如果我插入一个新行(通过右击空行并“插入”),我得到一个没有背景颜色的新行(这是我想要的),但该行也不包含任何公式:如何让Excel更智能,并在创建新行时复制前一行中的公式?
另一条信息:数据验证信息(即下拉列表)在插入新行时被复制。
谢谢。
发布于 2012-12-13 18:28:51
将包含数据和公式的区域设置为表格:
然后,在下一行中添加新信息将为新行复制该表中的所有公式。数据验证也将应用于新行,就像应用于整个列一样。这确实是Excel更聪明地处理您的数据。
不需要VBA ...
发布于 2015-02-07 05:16:55
关于复制表中的行,我发现的另一个关键问题是您正在处理的工作表需要被激活。如果您的工作簿包含多个工作表,则需要保存从中调用宏的工作表,然后激活包含表的工作表。完成后,您可以重新激活原始工作表。
您可以使用Application.ScreenUpdating = False来确保用户看不到您正在宏内切换工作表。
如果您没有激活工作表,则副本似乎无法正常工作,即某些内容似乎正常工作,而其他内容则不工作??
发布于 2019-04-22 09:28:13
私有子目标(ByVal Worksheet_Change As Range)
'data starts on row 3 which has the formulas
'the sheet is protected - input cells not locked - formula cells locked
'this routine is triggered on change of any cell on the worksheet so first check if
' it's a cell that we're interested in - and the row doesn't already have formulas
If Target.Column = 3 And Target.Row > 3 _
And Range("M" & Target.Row).Formula = "" Then
On Error GoTo ERROR_OCCURRED
'unprotect the sheet - otherwise can't copy and paste
ActiveSheet.Unprotect
'disable events - this prevents this routine from triggering again when
'copy and paste below changes the cell values
Application.EnableEvents = False
'copy col D (with validation list) from row above to new row (not locked)
Range("D" & Target.Row - 1).Copy
Range("D" & Target.Row).PasteSpecial
'copy col M to P (with formulas) from row above to new row
Range("M" & Target.Row - 1 & ":P" & Target.Row - 1).Copy
Range("M" & Target.Row).PasteSpecial
‘确保重新启用(或不重新启用)发生错误的事件并重新保护工作表
ERROR_OCCURRED:
If Err.Number <> 0 Then
MsgBox "An error occurred. Formulas may not have been copied." & vbCrLf & vbCrLf & _
Err.Number & " - " & Err.Description
End If
're-enable events
Application.EnableEvents = True
're-protect the sheet
ActiveSheet.Protect
'put focus back on the next cell after routine was triggered
Range("D" & Target.Row).Select
End If
结束子对象
https://stackoverflow.com/questions/13849629
复制相似问题