Doing the "impossible" again
Question from the web: Excel formula that will paste a specific value in a different cell?
https://superuser.com/a/1926549/1142601
The question:
….So basically I want to write a formula that will check a condition, and if that condition is met, then I want to paste a specific line of text in a different cell. I should note that I do not want the formula to exist in the cell I want to paste a value into. So for example, if I want to paste a value into B5, I don't want the formula to be in cell B5...
Comments there of this sort as usual:
…..it is impossible for a formula in one cell to modify another cell. – ….
I have seen this question asked a lot. Often it comes form people less familiar with things like VBA.
It’s usually a bad idea to say, something can’t be done, which is usually the answer to the question there, as is in some of the comments there. It can be done, and very easily. (I am not necessarily saying that it should be done. But an incorrect blanket statement that it can’t be done is a bad idea, JIMVHO)
My contribution is a simple way, using a formula to change values in another cell, ( I will use it to paste something in B5, as in the original request example). No formula will appear in cell B5.
I wanted to answer close to what is asked for in the Thread title, and in the sin of the original poster knowledge level, as I know that thread has been caught a few times by others in the same "newbie" vain.
(The solution I offer here does mean that you need to know some very basic VBA coding - VBA coding is some coding that is "behind" Microsoft Office, but exists already there for you freely available in all Microsoft Office versions from the last 30 years or so. (Using Microsoft Office without the VBA you already have, can be compared with having some all purpose vehicle parked outside your door, that can do just about everything that any vehicle can and much more like flying to Mars, submerging to the centre of the earth, digging out a trench to make a river to save a Forest for younger generations etc., etc., but you just use it once a week for the same short journey to go to the local shops)
If you have no ideas about VBA then for the short term it is not a problem as I will give you a solution example in the uploaded file.
You need to use the Excel formula / function Boolox, which I wrote.
This is that VBA coding “behind” the formula / function Boolox that I wrote.
Code:
' https://www.excelfox.com/forum/showthread.php/2831-UDF-that-can-change-values-in-cells-other-than-the-cell-in-which-the-UDF-is-used?p=27592&viewfull=1#post27592 https://superuser.com/a/1926549/1142601
Option Explicit
' First main "UDF" Procedure. In a spreadsheet cell we use "it" something like this: =Boolox(A2)
Public Function Boolox(ByVal Rng As Range) As String
Rem 1 Unconventional use of the general thing we might call a UDF
Worksheets("RBobJeff").Evaluate Name:="=AnuverProc(" & Rng.Address & ")" ' The Evaluate(" ") string, that is to say the Name:=" " bit in this case, uses Excel spreadsheet syntax, not VBA syntax, and we can get that spreadsheet A1 type address syntax from the .Address stuff in VBA
Rem 2 The conventional use of a UDF to return a value in the cell that it is in, in a similar way to how a formula or in built function does
If Rng = "Boolox" Then
Let Boolox = "you got Boolox" ' This sets/ defines the String returned from the function, so if used in the spreadsheet, this is what comes in the cell where you type the formula/ function
Else
Let Boolox = "you got no Boolox" ' This sets/ defines the String returned from the function, so if used in the spreadsheet, this is what comes in the cell where you type the formula/ function
End If
End Function
' Second Procedure. Debateable whether or not it is part of the "UDF"
Sub AnuverProc(Rng As Range)
If Rng = "Boolox" Then
Let Worksheets("RBobJeff").Range("B5") = "you got Boolox"
Else
Let Worksheets("RBobJeff").Range("B5") = "you got no Boolox"
End If
End Sub
You need all the coding, and one typical convenient place to keep it is in the workbook where you are wanting to use the “it”. “It” being the formula and necessarily the coding which it won’t work without
To keep it simple, the coding needs to go in what we call a normal code module.
Although not always necessarily necessary, for the purposes of the demo, you need to confine yourself to using the worksheet named "RBobJeff" in the uploaded file, ( Or name a worksheet in another workbook "RBobJeff", and do the demonstration in that )
Now, in the first cell, A1, I have written Boolox, ( or if you are using your own file, then write Boolox in the first cell, A1 )
https://i.postimg.cc/ydTyjn3n/Boolox-in-first-cell.jpg
https://i.postimg.cc/mkqf6HdZ/Boolox-in-first-cell.jpg
Now you can do the demo
If you write in any other cell ( other than B5 – as per the original request if a condition is met something will be pasted there in B5 ) this formula,
=Boolox(A1)
, then the line of text, you got Boolox , will be written in B5, and nothing else – no formula or anything other than that text will be in B5
If you use the formula in a similar way, but with anything other than A1 as the cell, such as using
=Boolox(A12)
, then a similar thing happens except the text says you got no Boolox
For examples
For example, after writing in cell C4 =Boolox(A2) , you should see
https://i.postimg.cc/1RYQzkyN/you-got-no-boolox.jpg
https://i.postimg.cc/1RYQzkyN/you-got-no-boolox.jpg
For example, after writing in cell C8 =Boolox(A1) , you should see
https://i.postimg.cc/05HsNT9K/you-got-boolox.jpg
https://i.postimg.cc/05HsNT9K/you-got-boolox.jpg
_.___-
As a further experiment, remove the text in the first cell, and the text in B5 will change
https://i.postimg.cc/d3XPSyWV/Boolox-gone.jpg
https://i.postimg.cc/d3XPSyWV/Boolox-gone.jpg
In those experiment examples two basic things happen,
_(i) what appears in C4 or C8 is as anyone with basic Excel and Excel VBA would expect. It reflects the conventional use of a "UDF" .- There is a formulas / function on a cell, and by default setting you will see the result in the cell, ( and the formula can be seen in the formula bar)
_(ii) what happens in B5 is the "impossible", which is what the original request asked for …. formula ( I use as example that in C4 or C8 ) , that will check a condition, and if that condition is met, then I want to paste a specific line of text in a different cell. I should note that I do not want the formula to exist in the cell I want to paste a value into. So for example, if I want to paste a value into B5, I don't want the formula to be in cell B5
Coding description
There are two main coding sections in the first procedure, ( Function Boolox( ___ ) , corresponding to the main two things that happen, Depending on your point of view
, the first procedure may be regarded as the main "User Defined Function", UDF
or
, in everyday language you might refer to the entire coding as the UDF which is the Boolox.
_(i) Rem 2
In this particular example coding, The same text is also is put in the cell you type the formula in, but if you do not want that, then just remove the second section in the first procedure, Rem 2.
Rem 2 in the first procedure is the conventional way to use this type of coding.. Such conventional coding can be called just some VBA coding, or a Function procedure in VBA. It can be used generally as coding, and it can be set off (run, Called) in several ways. But if it is intended to be set off/ called from a formula in a spreadsheet, then usually it goes by the name of a UDF (User Defined Function). That name makes sense if you think about it, a user, me in this case, has made/ defined a function / formula to use in a spreadsheet, rather than the existing built in ones. The procedure / Function, Boolox, and the associated formula / function used in the spreadsheet would loosely go by the name of UDF if you were talking about it in everyday conservations, like you might say, “Hey man, wow, check out Alan’s UDF, it’s the Boolox”
_(ii) Rem 1
It is usually said that a UDF cannot change values in a different cell. Well, I guess we can debate that until the Cows come home, and/or debate whether I am strictly doing that in the section Rem 1
Rem 1 is the bit that does something alone the lines of what was asked for. Or depending on your pint of view, it may set in motion the second procedure and that does something alone the lines of what was asked for.
In simple layman terms it sets off a second procedure, which you probably would not call a UDF. But someone might in general all day talking say it is part of the UDF since it gets the job done from the user made/defined formula/ function in the spreadsheet.
I suspect nobody really knows what is going on here, but it is generally thought that one reason it works is because Excel has lost it’s orientation a bit on where the second procedure is or has come from.. Maybe that helps us debate if the UDF is changing the other cells or some lost soul set off in the Excel world.
‘StackOverflowUDFChangeOtherCells.xls’ https://app.box.com/s/knpm51iolgr1pu3ek2j96rju8aifu4ow