在 Excel 中粘贴值而不是宏中的公式
Pasting Values in Excel not Formulas from a Macro
我目前有一个宏可以从一个 sheet 复制数据并将其归档到另一个 sheet。我遇到的问题是它正在复制和粘贴公式,它只需要复制和粘贴值。
Sub Archive_Execution()
Dim mainworkbook As Workbook
Set mainworkbook = ActiveWorkbook
Dim rangeName
Dim strDataRange As Range
Dim keyRange As Range
rangeName = "Archive_Execution"
Application.Goto Reference:=rangeName
Selection.Copy
Sheets("Archive Execution").Activate
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
mainworkbook.Sheets("Archive Execution").Paste
Sheets("Archive Execution").Activate
Set strDataRange = Range("A2:AA1000000")
Set keyRange = Range("D1")
strDataRange.Sort Key1:=keyRange, Order1:=xlAscending
End Sub
任何帮助调整我的代码将不胜感激,以便只粘贴值而不是公式
如评论中所述,如果您只想复制值,请使用 Range().PasteSpecial xlPasteValues
。
我还建议使用 Intersect
方法和 Worksheets("").UsedRange
来 trim 降低您的范围。
Sub Archive_Execution()
Dim strDataRange As Range
Dim keyRange As Range
With Sheets("Archive Execution")
Range("Archive_Execution").Copy
.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
End With
With Sheets("Archive Execution")
Set strDataRange = Intersect(.Range("A2:AA" & .Rows.Count), .UsedRange)
Set keyRange = .Range("D1")
strDataRange.Sort Key1:=keyRange, Order1:=xlAscending
End With
End Sub
要避免使用复制和粘贴,您可以尝试类似以下的操作,比复制粘贴快得多。
Sub Archive_Execution()
Dim mainworkbook As Workbook, rangeName
Dim strDataRange As Range, keyRange As Range
Dim r As Integer, c As Integer
Set mainworkbook = ActiveWorkbook
rangeName = "Archive_Execution"
r = Range(rangeName).Rows.Count
c = Range(rangeName).Columns.Count
With Sheets("Archive Execution").Range("A" & Rows.Count).End(xlUp).Offset(1)
.Resize(r, c) = Range(rangeName).Value
End With
Sheets("Archive Execution").Activate
Set strDataRange = Range("A2:AA1000000")
Set keyRange = Range("D1")
strDataRange.Sort Key1:=keyRange, Order1:=xlAscending
End Sub
我目前有一个宏可以从一个 sheet 复制数据并将其归档到另一个 sheet。我遇到的问题是它正在复制和粘贴公式,它只需要复制和粘贴值。
Sub Archive_Execution()
Dim mainworkbook As Workbook
Set mainworkbook = ActiveWorkbook
Dim rangeName
Dim strDataRange As Range
Dim keyRange As Range
rangeName = "Archive_Execution"
Application.Goto Reference:=rangeName
Selection.Copy
Sheets("Archive Execution").Activate
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
mainworkbook.Sheets("Archive Execution").Paste
Sheets("Archive Execution").Activate
Set strDataRange = Range("A2:AA1000000")
Set keyRange = Range("D1")
strDataRange.Sort Key1:=keyRange, Order1:=xlAscending
End Sub
任何帮助调整我的代码将不胜感激,以便只粘贴值而不是公式
如评论中所述,如果您只想复制值,请使用 Range().PasteSpecial xlPasteValues
。
我还建议使用 Intersect
方法和 Worksheets("").UsedRange
来 trim 降低您的范围。
Sub Archive_Execution()
Dim strDataRange As Range
Dim keyRange As Range
With Sheets("Archive Execution")
Range("Archive_Execution").Copy
.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
End With
With Sheets("Archive Execution")
Set strDataRange = Intersect(.Range("A2:AA" & .Rows.Count), .UsedRange)
Set keyRange = .Range("D1")
strDataRange.Sort Key1:=keyRange, Order1:=xlAscending
End With
End Sub
要避免使用复制和粘贴,您可以尝试类似以下的操作,比复制粘贴快得多。
Sub Archive_Execution()
Dim mainworkbook As Workbook, rangeName
Dim strDataRange As Range, keyRange As Range
Dim r As Integer, c As Integer
Set mainworkbook = ActiveWorkbook
rangeName = "Archive_Execution"
r = Range(rangeName).Rows.Count
c = Range(rangeName).Columns.Count
With Sheets("Archive Execution").Range("A" & Rows.Count).End(xlUp).Offset(1)
.Resize(r, c) = Range(rangeName).Value
End With
Sheets("Archive Execution").Activate
Set strDataRange = Range("A2:AA1000000")
Set keyRange = Range("D1")
strDataRange.Sort Key1:=keyRange, Order1:=xlAscending
End Sub