Solved

set excel cell value based on another cell

Posted on 2002-06-05
5
838 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 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.

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

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS Access Search and Replace Using VBA 6 105
Child Form in front 4 65
Create Files based on Cell Values in a Range in Excel 12 44
IF ELSE Statement in Excel Macro VBA 16 99
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…
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…

732 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