set excel cell value based on another cell

Posted on 2002-06-05
Last Modified: 2012-05-04
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


- 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

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


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


Author Comment

ID: 7057716

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


LVL 44

Accepted Solution

bruintje earned 150 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.



Author Comment

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

Expert Comment

ID: 7069003
great thanks for the grade

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Use Multiple Forms 4 49 2008 2 56
JSON Response and request in VB6 application 11 252
Create Files based on Cell Values in a Range in Excel 12 20
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now