Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Newbie in deep..... Excel and VB

Hi everyone.
I'm an experienced programmer, just not experienced with Excel and VB. I have
to populate a spread sheet programmatically. On a button click, I am clearing
the worksheet, and then I compute a formula that goes into each cell. It
doesn't work!

This is what I'm doing (snipped somewhat)

Dim myFormula as string

....

myFormula = "=A1+B1"
cells(10, 10).formula = myFormula

..... that works.

So does.... cells(10,10).formula = "A1+B1"

Now if I need to compute the formula at runtime like this....
Let x=1
Let y=1
myFormula = "=A"+str(x)+"+B"+str(y)
and try cells(10, 10).formula = myFormula I get an error.

In each case the value of myFormula is "=A1+B1"

Anybody ever tried dynamically computing a formula and assigning it to a cell?

I've been working on XL for the past week, and have been very impressed with
the capabilities, but before I can become a true believer I need to know how to
do this...

Similarly, how can I dynamically compute a range? I would like to pass a
computed string to a statement like this:

range(mycomputedstring).formula = myFormula

Thanks in advance for your help.

Brad Vernon
Help!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Newbie in deep..... Excel and VB

Instead of using the Str function, use CStr to convert your variables x and
y to strings. Str puts a space in front of the string.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"BEVERNON" wrote in message
...
Hi everyone.
I'm an experienced programmer, just not experienced with Excel and VB. I

have
to populate a spread sheet programmatically. On a button click, I am

clearing
the worksheet, and then I compute a formula that goes into each cell. It
doesn't work!

This is what I'm doing (snipped somewhat)

Dim myFormula as string

...

myFormula = "=A1+B1"
cells(10, 10).formula = myFormula

.... that works.

So does.... cells(10,10).formula = "A1+B1"

Now if I need to compute the formula at runtime like this....
Let x=1
Let y=1
myFormula = "=A"+str(x)+"+B"+str(y)
and try cells(10, 10).formula = myFormula I get an error.

In each case the value of myFormula is "=A1+B1"

Anybody ever tried dynamically computing a formula and assigning it to a

cell?

I've been working on XL for the past week, and have been very impressed

with
the capabilities, but before I can become a true believer I need to know

how to
do this...

Similarly, how can I dynamically compute a range? I would like to pass a
computed string to a statement like this:

range(mycomputedstring).formula = myFormula

Thanks in advance for your help.

Brad Vernon
Help!



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 50
Default Newbie in deep..... Excel and VB

myFormula = "=A"+str(x)+"+B"+str(y)

Str() returns a string with a leading space if the number is positive.
Those leading spaces are screwin up the syntax of the Formula
property.
Try CStr() instead.



range(mycomputedstring).formula = myFormula


This should work fine. Just make sure mycomputedstring contains a
valid address enclosed in quotes. It could also contain the name of a
named range instead of
an address.


Regards,
Vic Eldridge
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Newbie in deep..... Excel and VB

If you are having a problem with spaces being left in or added simple use the TRIM function (can be used in VBA and Excel) to remove excess spaces.
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 81
Default Newbie in deep..... Excel and VB

Hi Brad

basicly this will do what you ask - i think!

Private Sub CommandButton1_Click()

Dim strFor As Variant
x = 1
y = 2

strFor = "=A" & x & "+b" & y

Sheets("sheet1").Cells(3, 1).Value = strFor

End Sub

You see that it's the same as your code, just with diffrent ways of
link the bite up! (the &)
However, i have ever done any thing like this, but i would wayger that
there is a better way to do it... row and colum formuals spring to
mind, but hey this get the job done.

NB. the cells(3,1) refers to row 3, col A, and so on,
toolsoptionsgenralR1C1 stlye

As for ranges

you can use somehting like

Private Sub CommandButton1_Click()

Dim MyRange As Range



With ActiveSheet
.Range(.Cells(2, 4), .Cells(4, 5)).Name = "tevor"
End With


Set MyRange = ActiveSheet.Range("tevor")

End Sub

agian there are lost of ways to define ranges and there are lots of
things you can do to change ranges,

any way hope this helps,
rosscoe


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Newbie in deep..... Excel and VB Solved! Thanks...

Thanks to those of you who pointed out the space.... I used a trim function and
it works perfectly. Thanks again!
Brad
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 32
Default Newbie in deep..... Excel and VB

I'm in the same position...I recommend "Excel 2002 Power Programming with VBA" by John Walkenbach, ISBN 0764547992.
Toby Erkson
Oregon, USA

On 08 Apr 2004 02:50:16 GMT, (BEVERNON) wrote:

Hi everyone.
I'm an experienced programmer, just not experienced with Excel and VB. ...

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Summing values, x rows deep, depending on a criterion klm[_2_] Excel Worksheet Functions 2 August 18th 08 02:02 PM
path too deep bothan25 Excel Discussion (Misc queries) 1 July 30th 08 11:44 PM
Why can't I have a cell more than 1 line deep? Conservative Officer New Users to Excel 2 July 18th 08 08:49 AM
3 deep nested if gets the same value on every cell. Janis Excel Discussion (Misc queries) 1 July 30th 07 09:10 PM
too deep formula line Light Excel Discussion (Misc queries) 5 March 17th 06 07:16 PM


All times are GMT +1. The time now is 11:00 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"