Excel VBA导出到Excel工作表但删除公式

时间:2018-11-08 20:20:00

标签: excel vba export

我有一个VBA,每天将3个标签导出到excel工作簿。我有一段脚本删除了所有外部链接,这很棒。但是我想知道是否还有另一段代码可以使我也删除公式。字段标题上有一个公式,用于计算日期。

因此,不仅显示11月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

3 个答案:

答案 0 :(得分:1)

怎么样:

def func_1( ... ) :Either[ ... ] = { //a method that returns an Either

  Left(nodeId,clusterId,neightbours) //create a Left expression of the Either
                                     //don't do anything with it, throw it away
  for(x <- neightbours){             //grab all the neightbours (spelling?)
    if(clusterId > -1){              //if clusterId is positive
      Right(x,clusterId)             //create a Right expression of the Either
    }                                //don't do anything with it, throw it away
  }
}                                    //I'm done, return nothing

答案 1 :(得分:1)

另一种方法:

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

答案 2 :(得分:0)

在复制后,您可以参考旧工作簿的值以将内容更改为值。 像

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。

相关问题