Solved

# set excel cell value based on another cell

Posted on 2002-06-05
Medium Priority
961 Views
I have what seams to be simple

I wish to set  A1 to the value of B1 if  C1 = “TEXT”

I need to be able to use this within other functions such as if() from another cell

EG

- A1 - B1 - C1 - D1
-    - 22 - A  - =IF(C1=”A”,A1= value of B1,blank)
-    - 23 - B  - =IF(C1=”A”,A1= value of B1,blank)
-    - 24 - A  - =IF(C1=”A”,A1= value of B1,blank)
-    - 25 -    - =IF(C1=”A”,A1= value of B1,blank)

should result in

- A1 - B1 - C1 - D1
- 22 - 22 - A  -
-    - 23 - B  -
- 24 - 24 - A  -
-    - 25 -

sorry for the formating. I have used '-' for cell delimiters. there are no negative numbers

thanks
0
Question by:shnmack
• 3
• 2

LVL 44

Expert Comment

ID: 7056035
Hi Shnmack,

in cell A1 you can put the formula as you already made

=IF(C1="A",B1,"")

and then copy this down in column a for as many cells you need

HAGD:O)Bruintje
0

Author Comment

ID: 7057716
bruintje

cell A1 already contains values that otherwise must stay as they are.

i am looking to write a function to minipulate the apropriate cells - returning True (if sucsessful) otherwise False

thanks

0

LVL 44

Accepted Solution

bruintje earned 600 total points
ID: 7064122
Hi shnmack, i meant you can put that formula in Cell A this can be cell A2 and down

but if Cell A contains prefilled data then you need an outside sub or function to do this for you

-open the VB Editor with ALT +F11
-then insert a new module
-paste the code

option explicit

Public Sub test()
if ActiveCell.Column = 4 then
ActiveCell.Value = "False"
If Range("C" & ActiveCell.Row) = "A" Then
Range("A" & ActiveCell.Row) = Range("B" & ActiveCell.Row)
ActiveCell.Value = "True"
End If
end if
End Sub

will do that exactly
-it checks the current cell if it's in D column
-then check cell C on the row
-if that is an A then
-fill the cell A with the contents of Cell B
-and return a true in the cell D

this sub can be run from the menu
-tool | macro | run | test

this works for every cell D but you've to run it for every row

-if you want to go this way then i'll make a procedure that does the entire column for you in one go

third there is some very good info on this from Chip Pearson

-------------------
Any VBA code (sub or function) can alter any cell value or anything else, UNLESS that code is called, directly or indirectly, from a worksheet cell. In this case, NO code can alter anything.

Think of it like this:  When any VBA code is called by a worksheet cell, a 'switch' called Do_Not_Allow_Changes is turned 'on'.  When that switch is 'on', nothing may be changed.  When VBA code is called from a macro or an event procedure, the switch remains 'off', so any code can change anything.

In your example, the Sub Temel does not allow changing of values because the entire sequence of VBA code was started from a worksheet cell. If you called the function Ters from a Sub procedure, which you execute from the Macros menu item, it would allow changed.  The fact that the entire chain of VBA code was started from a worksheet cell is what prevents changes to cells.

The reason for this is fairly simple. The most fundamental thing that a spreadsheet does is keep track of what cells are dependent on what other cells. In other words, it tracks which cells need to be calculated before which other cells. It must do this in order to calculate the worksheet in the proper order.  Excel cannot predict what your VBA code will do. If it allowed VBA to change other cell's values, the order of calculation would become invalid. Cells which are dependent on another cell may be calculated first, rather than later, and the results would be incorrect.

For example, suppose A1 contained yesterday's date, and A2 contained the formula =IF(A1=TODAY(),TRUE,FALSE) .  Now suppose A3 contained the function =MyFunction() where

Function MyFunction()
Range("A1").Value = Now
End Function

When Excel calculates A2, the formula =IF(A1=TODAY(),TRUE,FALSE) would return FALSE, because A1 is yesterday, not today.  Then it calculates A3, which puts today's date in A1.  Excel doesn't (and can't) know that A3
changed the value of A1, and so now A1 would have today's date, but A2 would remain FALSE, which is clearly incorrect. When the entire sheet is calculated, A1 would have today's date, but A2 would say that it doesn't.

Therefore, Excel prevents this.  When any VBA code is called from a worksheet cell, you cannot change any values. All you can do is return a value to the caller.

You can *always* change cell values from VBA code, even from a function. For example, suppose you have the following code.

Sub AAA()
Dim Res As Double
Res = Something(Range("C1").Value)
End Sub

Function Something(X As Double) As Double
Range("C2").Value = X * 2
Something = X * 3
End Function

If you call AAA from the macros menu (or an event procedure or a form), the function Something can certainly change the value of C2.  That is not a problem, because the entire chain of VBA code was *not* started from a
worksheet cell. However, if you entered =Something(123) in some cell, you will get a #VALUE error, because Excel forbids code started by a worksheet cell from changing anything.

----------------

HAGD:O)Bruintje
0

Author Comment

ID: 7068779
Thanks Bruintje,
i was able to use your code imediatly
and your info from Chip Pearson has also proved invaluable
0

LVL 44

Expert Comment

ID: 7069003
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
###### Suggested Courses
Course of the Month8 days, 11 hours left to enroll