VBA将公式保存到非宏启用工作表而不带有原始工作表引用。

huangapple go评论69阅读模式
英文:

VBA Saving formulas to non-macro enabled sheet without original sheet reference

问题

我有一个宏工作簿,用于向工作表添加公式(以及其他数据)。

我想将包含公式和数据的工作表转移到xlsx版本。

我尝试创建一个新的xlsx工作表,然后使用VBA将我想要的工作表复制/粘贴到新工作簿。不幸的是,新工作簿中的公式引用了原始工作簿。

如何在不引用原始工作的情况下,将数据和公式转移到新工作簿的最佳方法是什么?

在公式中的问题示例:

原始xlsm工作表中的公式:

=SUMIFS('raw-data'!S:S,'raw-data'!B:B,'price-compare'!B5,'raw-data'!M:M,'price-compare'!F5,'raw-data'!O:O,'price-compare'!J3)

新xlsx工作表中的公式:

=SUMIFS('[billing-compare.v.1.0.xlsm]raw-data'!S:S,'[billing-compare.v.1.0.xlsm]raw-data'!B:B,'price-compare'!B6,'[billing-compare.v.1.0.xlsm]raw-data'!M:M,'price-compare'!F6,'[billing-compare.v.1.0.xlsm]raw-data'!O:O,'price-compare'!J3)

正如您所看到的,复制的工作表中的公式包含对不同工作簿中原始工作表的引用。

我需要阻止引用回原始工作簿。

谢谢。

英文:

I have a macro workbook that adds formulas to a sheet (along with other data).

I want to transfer the sheet with the resulting formulas and data to xlsx version.

I tried creating a new xlsx sheet and copy / pasting the sheets I wanted to the new workbook using VBA. Unfortunately the formulas in the new workbook referenced the original workbook.

What is the best method to transfer the data and formulas to a new workbook, without having them reference the original work?

An example of the issue within the formulas:

Formula in original xlsm sheet:

=SUMIFS('raw-data'!S:S,'raw-data'!B:B,'price-compare'!B5,'raw-data'!M:M,'price-compare'!F5,'raw-data'!O:O,'price-compare'!J3)

Formula in new xlsx sheet

=SUMIFS('[billing-compare.v.1.0.xlsm]raw-data'!S:S,'[billing-compare.v.1.0.xlsm]raw-data'!B:B,'price-compare'!B6,'[billing-compare.v.1.0.xlsm]raw-data'!M:M,'price-compare'!F6,'[billing-compare.v.1.0.xlsm]raw-data'!O:O,'price-compare'!J3)

As you can see the formulas in the copied sheet contain reference to the original sheet in a different workbook.

I need prevent the reference back to the originating workbook.

Thanks.

答案1

得分: 1

以下是代码部分的翻译:

' 从一个工作簿复制公式到另一个工作簿的方法,不包含 `[` 的内容如下:

Sub CopyFormulas()
    
    Dim wkb As Workbook
    Set wkb = Workbooks.Open(ThisWorkbook.Path & "\" & "Source.xlsx")
    
    Dim wksName As String
    wksName = "Sheet1"
    
    Dim myFormulaCells As Range
    Set myFormulaCells = GetFormulaCells(wkb.Worksheets(wksName))
    
    If myFormulaCells Is Nothing Then
        Debug.Print "No formulas present in "; wkb.Name; "->"; wksName
    Else
        Dim myCell As Range
        For Each myCell In myFormulaCells.Cells
            Debug.Print myCell.Address
            Debug.Print myCell.Formula
            
            With ThisWorkbook.Worksheets(wksName)
                .Cells(myCell.Row, myCell.Column).Formula = myCell.Formula
            End With
        Next
    End If
    wkb.Close False
    
End Sub

' 这是 GetFormulaCells() 函数,如果存在公式,则返回具有公式的单元格:
Function GetFormulaCells(wks As Worksheet) As Range
    
    On Error GoTo FormulaCells_Error:
    
    Set GetFormulaCells = wks.Cells.SpecialCells(xlCellTypeFormulas, 23)
        
    On Error GoTo 0
    Exit Function
    
FormulaCells_Error:
    Err.Clear
    Set GetFormulaCells = Nothing
    
End Function

希望这对您有帮助。如果需要任何进一步的翻译,请告诉我。

英文:

A way to copy formulas from one workbook to another, without having that [ is something like this:

Sub CopyFormulas()
    
    Dim wkb As Workbook
    Set wkb = Workbooks.Open(ThisWorkbook.Path & "\" & "Source.xlsx")
    
    Dim wksName As String
    wksName = "Sheet1"
    
    Dim myFormulaCells As Range
    Set myFormulaCells = GetFormulaCells(wkb.Worksheets(wksName))
    
    If myFormulaCells Is Nothing Then
        Debug.Print "No formulas present in "; wkb.Name; "->"; wksName
    Else
        Dim myCell As Range
        For Each myCell In myFormulaCells.Cells
            Debug.Print myCell.Address
            Debug.Print myCell.Formula
            
            With ThisWorkbook.Worksheets(wksName)
                .Cells(myCell.Row, myCell.Column).Formula = myCell.Formula
            End With
        Next
    End If
    wkb.Close False
    
End Sub

This is the GetFormulaCells(), which returns the cells with the formula, if these exist:

Function GetFormulaCells(wks As Worksheet) As Range
    
    On Error GoTo FormulaCells_Error:
    
    Set GetFormulaCells = wks.Cells.SpecialCells(xlCellTypeFormulas, 23)
        
    On Error GoTo 0
    Exit Function
    
FormulaCells_Error:
    Err.Clear
    Set GetFormulaCells = Nothing
    
End Function

Assumptions:

  • both worksheets Target (the Excel with the VBA code) and Source.xlsx are in the same workbook;
  • Sheet1 is the only sheet, in which we are interested to migrate the formulas from and to. It exists in both workbooks;
  • You can delete the useless Debug.Print myCell.Address and others yourself;
  • In the Target worksheet, the formulas will be copied to the cell addresses that the formulas were having in the Source.Sheet1 worksheet;
  • The scenario when there are formulas referring a third workbook =SUM('[another_workbook.xlsx]'!A:A in the source workbook is not tested;

huangapple
  • 本文由 发表于 2023年4月10日 21:10:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977430.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定