Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

set excel cell value based on another cell

Posted on 2002-06-05
5
Medium Priority
?
929 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 44

Expert Comment

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

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

by:
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

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

by:bruintje
ID: 7069003
great thanks for the grade
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

650 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