Computing.Net > Forums > Office Software > Excel - Need help taking cell data

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Excel - Need help taking cell data

Reply to Message Icon

Name: tjjking
Date: July 30, 2008 at 02:09:21 Pacific
OS: Windows XP
CPU/Ram: 1.8GHz, 1024MB RAM
Comment:

I have cells that contain information that changes with every change to another cell, and instead of using "=A1" and linking the data straight to that cell I want it to contain the answer to the formula.

Example:

Say "A1" contains "=INT(RAND()*((9+1)-2)+2)"
(Which is a random number from 2~9)
After putting that in it would generate a random number, but this particular number is not needed. Because the next change I make to get that random number would first change it's number, and then take it.

I don't want to use "=A1" as said before, because it would just copy the same number as well as when it changes.
I just want it to take the number and not change, while A1 still continues to generate a random number from 2~9.

Note:

Creating a circular reference works, but it has 2 problems.
1) It creates a pop-up (which can probably be removed through a macro -- which is what most of this is going to be done in anyways)
2) It requires 2 cells PLUS the one containing the changing information.



Sponsored Link
Ads by Google

Response Number 1
Name: DerbyDad03
Date: July 30, 2008 at 08:23:35 Pacific
Reply:

Why not generate the random number in VBA and then place it in A1 - or wherever you really need it?

The value would only change when the macro was run, over which you would have more control.


Sub RandNum()
Dim LRandomNumber As Integer
Randomize
LRandomNumber = Int((9 - 2 + 1) * Rnd + 2)
Range("A1") = LRandomNumber
End Sub

Perhaps use the code above in a Change event so the macro would run automatically when you make a change that would cause to you want a new random number.


Private Sub Worksheet_Change(ByVal Target As Range)
Dim LRandomNumber As Integer
If Target.Address = "$D$1" Then
Randomize
LRandomNumber = Int((9 - 2 + 1) * Rnd + 2)
Range("A1") = LRandomNumber
End If
End Sub


0

Response Number 2
Name: tjjking
Date: July 30, 2008 at 12:22:00 Pacific
Reply:

hmm, that looks like a good idea, but,
what if the changing number was dependent on other cells

ie:

=INT(RAND()*((B1+1)-A1)+A1)

could it be done with variables in VBA which represent those cells?
if so, how could I code that, because I don't know much about VBA


0

Response Number 3
Name: DerbyDad03
Date: July 30, 2008 at 14:08:57 Pacific
Reply:

"could it be done with variables in VBA which represent those cells?"

Yes

"if so, how could I code that, because I don't know much about VBA"

In your first post you said: "1) It creates a pop-up (which can probably be removed through a macro -- which is what most of this is going to be done in anyways)"

How are you going to do "most of this" in a macro if you don't know much about VBA?

In any case, yes, you can use variables in VBA to represent the ranges:


Private Sub Worksheet_Change(ByVal Target As Range)
Dim LRandomNumber As Integer
If Target.Address = "$D$1" Then
RndVar1 = Range("A1")
RndVar1 = Range("B1")
Randomize
LRandomNumber = Int((RndVar2 - RndVar1 + 1) * Rnd + RndVar1)
Range("A2") = LRandomNumber
End If
End Sub


0

Response Number 4
Name: tjjking
Date: July 30, 2008 at 14:29:51 Pacific
Reply:

I just usually record macros. :P


0

Response Number 5
Name: DerbyDad03
Date: July 30, 2008 at 19:36:15 Pacific
Reply:

Recording macros is a great way to learn how to write code, but typically results in bloated and inefficient code.

Once you record a macro, use F8 in the VBA editor to Single Step thorugh the code to see what it is doing. Once you see how the code flows, you should be able to eliminate alot of the extra stuff the macro recorder puts in.

For example, if you record the selection of D13 and enter 43 in the cell, you'll get this:

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 07/30/2008 by Me
'

'
Range("D13").Select
ActiveCell.FormulaR1C1 = "43"
End Sub

That can be reduced to:

Sub Macro1()
Range("D13")= 43
End Sub

You also can't record Loops or If-Then's or any of the other things that will make your VBA code a lot more efficient.


0

Related Posts

See More



Response Number 6
Name: tjjking
Date: July 30, 2008 at 21:00:09 Pacific
Reply:

then you should teach me some of these wondrous things :P


0

Response Number 7
Name: DerbyDad03
Date: July 31, 2008 at 05:28:50 Pacific
Reply:

I have been teaching you.

Read the questions, copy my code, single step through it with F8 and see what it does.

That's how I learned VBA.


0

Response Number 8
Name: tjjking
Date: July 31, 2008 at 13:22:10 Pacific
Reply:

well thanks for your help :D


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Office Software Forum Home


Sponsored links

Ads by Google


Results for: Excel - Need help taking cell data

Excel Formula, need help! www.computing.net/answers/office/excel-formula-need-help/6717.html

Excel Macro help. www.computing.net/answers/office/excel-macro-help/8054.html

Prof Excel Formula Help Needed www.computing.net/answers/office/prof-excel-formula-help-needed/4035.html