简体   繁体   中英

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)

In Cell A1 I have "SomeString"

Is there a way to easily replace the cell link "A1" in the formula with the string "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"]

This allows you to name a specified range. You can then refer to that name either within an excel worksheet or within 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. ie: you can have the Name SomeString be equal to "asdf". Then, use ctrl+f to find and replace all instances of "A1," in your worksheet with "SomeString".

Alternatively , just use ctrl+f to find and replace "A1" with ""asdf"", and have your results hardcoded directly in all cells. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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