Solved

set excel cell value based on another cell

Posted on 2002-06-05
5
791 Views
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

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
Comment
Question by:shnmack
  • 3
  • 2
5 Comments
 
LVL 44

Expert Comment

by:bruintje
Comment Utility
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

by:shnmack
Comment Utility
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

by:
bruintje earned 150 total points
Comment Utility
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

by:shnmack
Comment Utility
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

by:bruintje
Comment Utility
great thanks for the grade
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

771 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

11 Experts available now in Live!

Get 1:1 Help Now