简体   繁体   English

强制 Excel 公式引用当前工作簿

[英]Force Excel Formula to Reference Current Workbook

I have a simple formula reference that I use in my workbook, however it gets complicated when I use another function that instantly opens my default worksheet and copies it over to my active workbook.我在我的工作簿中使用了一个简单的公式参考,但是当我使用另一个可以立即打开我的默认工作表并将其复制到我的活动工作簿的函数时,它会变得复杂。

The problem is that the cells in this workbook reference another sheet in my default workbook.问题是此工作簿中的单元格引用了我的默认工作簿中的另一个工作表。 The sheet in that and all the other workbooks I am working on has the same name.该工作表和我正在处理的所有其他工作簿具有相同的名称。 It's "Form"是“表格”

When I use my code to copy the sheet over, the cell automatically changes it's reference to include the previous workbook.当我使用我的代码复制工作表时,单元格会自动更改它的引用以包含以前的工作簿。

I want the formula to ALWAYS USE THE CURRENT WORKBOOK.我希望公式始终使用当前的工作簿。

Here is what I use这是我使用的

=Form!B6

Here is what I end up getting when i drop the sheet这是我放下床单时最终得到的结果

="filepath"Form!B6

Here is a way to copy a formula from one workbook to another with no changes :这是一种无需更改即可将公式从一个工作簿复制到另一个工作簿的方法:

Sub ytrewq()
    Dim s As String

    s = Workbooks("book2.xlsm").Sheets("Sheet1").Range("G8").Formula
    Workbooks("temp.xlsm").Sheets("Sheet1").Range("H1").Formula = s
End Sub

The trick is to use INDIRECT().诀窍是使用 INDIRECT()。 For example:例如:

=INDIRECT("Form!B6")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM