简体   繁体   English

Excel-用单元格中的值替换公式中的单元格链接

[英]Excel - Replace cell link in formula with value in cell

I'm trying to figure clean up a spreadsheet - but I'm trying to avoid typing out a lot of hardcoded values that are referenced in formulas. 我正在尝试清理电子表格-但是我试图避免键入很多在公式中引用的硬编码值。

So right now, I have a function that takes a string 所以现在,我有一个需要一个字符串的函数

=FunctionThatWantsAString(A1,SomeOtherInputs) = FunctionThatWantsAString(A1,SomeOtherInputs)

In Cell A1 I have "SomeString" 在单元格A1中,我有“ SomeString”

Is there a way to easily replace the cell link "A1" in the formula with the string "SomeString"? 有没有一种方法可以轻松地将公式中的单元格链接“ A1”替换为字符串“ SomeString”?

Thanks 谢谢

The simple solution is to go to the Formulas ribbon > Defined Names > Name Manager > New [or simply select A1, and then click on the NameBox which says "A1" and type in "SomeString"] 简单的解决方案是转到“公式”功能区>“定义的名称”>“名称管理器”>“新建” [或直接选择A1,然后单击显示“ A1”的NameBox并键入“ SomeString”]]

This allows you to name a specified range. 这使您可以命名指定的范围。 You can then refer to that name either within an excel worksheet or within VBA. 然后,您可以在excel工作表中或VBA中引用该名称。

Edit for additional request If you don't want A1 to have to hold the text "SomeString", you can actually use the name manager to create a name which refers to a string. 编辑其他请求如果您不希望A1保留文本“ SomeString”,则实际上可以使用名称管理器来创建引用字符串的名称。 ie: you can have the Name SomeString be equal to "asdf". 即:您可以使名称SomeString等于“ asdf”。 Then, use ctrl+f to find and replace all instances of "A1," in your worksheet with "SomeString". 然后,使用ctrl + f查找工作表中所有“ A1”实例并将其替换为“ SomeString”。

Alternatively , just use ctrl+f to find and replace "A1" with ""asdf"", and have your results hardcoded directly in all cells. 或者,只需使用ctrl + f查找并用““ asdf”“替换” A1“,然后将结果直接硬编码在所有单元格中。 Probably not recommended to do that though, for maintenance purposes. 出于维护目的,可能不建议这样做。

If you are using a User Defined Function, you should check if that first parameter is a cell value, and if so, get the value. 如果使用的是用户定义函数,则应检查该第一个参数是否为单元格值,如果是,则获取该值。

Public Function test(st As Variant)

If TypeName(st) = "Range" Then

'get the value of the range or use the string passed in
'or if TypeName(st)="String"
End If

test = TypeName(st)

End Function

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

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