[英]Excel VBA Export to Excel sheet but remove formulas
我有一個VBA,每天將3個標簽導出到excel工作簿。 我有一段腳本刪除了所有外部鏈接,這很棒。 但是我想知道是否還有另一段代碼可以使我也刪除公式。 字段標題上有一個公式,用於計算日期。
因此,不僅顯示NOV 7,它還顯示公式= Today()-1在我的VBA中,我想包含僅包含“ Nov 7”值而不是公式的代碼。
我的腳本如下
'excel read only
Application.DisplayAlerts = False
Sheets(Array("Template", "Data Export", "Sales Breakdown")).Copy
Dim ExternalLinks As Variant
Dim x As Long
'Create an Array of all External Links stored in Workbook
ExternalLinks = ActiveWorkbook.LinkSources(Type:=xlLinkTypeExcelLinks)
'Loop Through each External Link in ActiveWorkbook and Break it
For x = 1 To UBound(ExternalLinks)
ActiveWorkbook.BreakLink Name:=ExternalLinks(x), Type:=xlLinkTypeExcelLinks
Next x
ActiveWorkbook.SaveAs Filename:="MY FILENAME", FileFormat:=51, CreateBackup:=False
怎么樣:
Sub FormulaKiller()
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
With sh.Cells
.Copy
.PasteSpecial xlPasteValuesAndNumberFormats
End With
Next sh
End Sub
另一種方法:
On Error Resume Next
Set FormulaCells = ActiveSheet.UsedRange.SpecialCells(xlFormulas, 23)
' Exit if no formulas are found
If FormulaCells Is Nothing Then
Exit Sub
End If
For Each myCell In FormulaCells
myCell.Value = myCell.Value
Next myCell
復制后,您可以立即參考舊工作簿的值以將內容更改為值。 就像是
Dim wb1, wb2 As Workbook
Set wb1 = ActiveWorkbook
wb1.Sheets(Array("Template", "Data Export", "Sales Breakdown")).Copy
Set wb2 = ActiveWorkbook 'the new workbook is now wb2
For Each i In Array("Template", "Data Export", "Sales Breakdown")
wb2.Sheets(i).usedrange.Value = wb1.Sheets(i).usedrange.Value
Next
///rest of yourcode///
或者,如果您具有確切的范圍,則可以使用range(“ A1:Z500”)代替usedrange來使其更快。 使用這種方法,您可以避免使用復制粘貼值(我討厭)。 而不是activeworkbook,您可以在下面的代碼中使用wb2。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.