如何在复制和清除数据宏Excel后保留公式和格式
我有一些VBA代码,当单击时链接到一个按钮,Sheet1上的数据将复制到Sheet2并清除Sheet1上的数据。我的问题是Sheet1上的公式和格式最终也会被删除。在过去,我可以在find和select下使用常量,但这不起作用。是否有任何代码我可以添加,以确保Sheet1保持公式和格式,我不需要他们在sheet2,但如果它是更容易保持他们比这将工作如何在复制和清除数据宏Excel后保留公式和格式,excel,vba,excel-formula,Excel,Vba,Excel Formula,我有一些VBA代码,当单击时链接到一个按钮,Sheet1上的数据将复制到Sheet2并清除Sheet1上的数据。我的问题是Sheet1上的公式和格式最终也会被删除。在过去,我可以在find和select下使用常量,但这不起作用。是否有任何代码我可以添加,以确保Sheet1保持公式和格式,我不需要他们在sheet2,但如果它是更容易保持他们比这将工作 Sub CopyPasteClear() 'Source Const cSource As String = "Receive Tracker"
Sub CopyPasteClear()
'Source
Const cSource As String = "Receive Tracker" ' Worksheet Name
Const cFirstRsrc As Long = 7 ' First Row Number
Const cClr As String = "A7,J7" ' Clear Cells
Const cRowClr As Long = 7 ' First Clear Row
Const cFinal As String = "A7" ' Final Select Cell Address
' Target
Const cTarget As String = "ReceiveData" ' Worksheet Name
' Both
Const cCol1 As Variant = "A" ' First Column Letter/Number
Const cCol2 As Variant = "J" ' Second Column Letter/Number
Dim vntVal As Variant ' Value Array
Dim LastRsrc As Long ' Source Last Row Number
Dim LastRtgt As Long ' Target Last Row Number
' Source Range into Source Array
With ThisWorkbook.Worksheets(cSource)
' Calculate Source Last Row Number of First Column.
LastRsrc = .Columns(cCol1).Find("*", , -4123, , 2, 2).Row
' Prevent copying data above First Row. Rows from First Row to
' one less than First Clear Row will still be copied. To prevent this,
' change cFirstRsrc to cRowClr in the following line only.
If LastRsrc < cFirstRsrc Then Exit Sub
' Copy Source Range into Source Array
vntVal = .Range(.Cells(cFirstRsrc, cCol1), .Cells(LastRsrc, cCol2))
End With
' Source Array into Target Range
With ThisWorkbook.Worksheets(cTarget)
' Calculate Target Last Row Number of First Column.
LastRtgt = .Columns(cCol1).Find("*", , -4123, , 2, 2).Row
' Copy Source Array into Target Range. Note that Target Last Row
' Number has to be inreased by 1 to get the first empty row.
.Cells(LastRtgt + 1, cCol1) _
.Resize(UBound(vntVal), UBound(vntVal, 2)) = vntVal
End With
With ThisWorkbook.Worksheets(cSource)
' Prevent deleting data above First Clear Row.
If LastRsrc < cRowClr Then Exit Sub
' Clear contents of Clear Cells and modified Source Range.
Union(.Range(cClr), .Range(.Cells(cRowClr, cCol1), _
.Cells(LastRsrc, cCol2))).ClearContents
' Activate Source Worksheet if it is not active (not the ActiveSheet).
' The following Select method will produce an error if the program
' was started while a different worksheet than the Source Worksheet
' was active.
If .Parent.ActiveSheet.Name <> .Name Then
.Activate
End If
' Select Final Select Cell.
.Range(cFinal).Select
End With
End Sub
子复制粘贴清除()
"来源:
Const cSource As String=“Receive Tracker”'工作表名称
Const CFIRSTRRC长度=7'第一行编号
Const cClr As String=“A7,J7”清除单元格
Const cRowClr长度=7'第一排净空
常量cFinal为String=“A7”'最终选择单元地址
"目标"
Const cTarget As String=“ReceiveData”工作表名称
”“都是
常量cCol1为Variant=“A”'第一列字母/数字
常量cCol2为Variant=“J”'第二列字母/数字
作为变量值数组的Dim vntVal
Dim LastRsrc作为“长”源最后一行编号
Dim LastRtgt作为“长”目标最后一行编号
'源范围到源数组中
使用此工作簿。工作表(cSource)
'计算第一列的源最后行数。
LastRsrc=.Columns(cCol1).Find(“*”,-4123,2,2).Row
'防止在第一行上方复制数据。从第一行到第二行的行
'仍将复制比第一个清除行少的一行。为防止此,,
'仅在下一行中将CFIRSTRRC更改为cRowClr。
如果LastRsrc
旁注-我建议您在工作表中使用工作表代码名而不是字符串常量。这还是一个新概念,有什么好处?更多详细信息。您认为该解决方案可以解决我的问题吗?