Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,092
Default Cell formulas with VB

Can VB variables be used in a cell formula? For example I have columns of
data on one sheet and a list to compare to on another. I can put a formula
in a cell and copy down the comparison list to get what I want. This
comparison list remains constant and part of the workbook.
I can write this formula to a range of cells next to the comparison list
from VB:

Range("A2:A90").FormulaArray =
"=SUM((Dan!$E$1:$E$504="""")*(Dan!$B$1:$B$504=A2)) "

In this case a database named Dan.dbf was queried and brought in on a new
sheet which I named "Dan". Dan has 504 rows of data. This query routine
needs to work on any file queried, so I have assigned variables to the sheet
name and the rowcount
nwsht = "Dan"
rowcount = 504

How can I substitute these 2 variables into the above formula? I have played
with syntax variations of all I can think of.
Any advice will be greatly appreciated. Thanks, Mike


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,080
Default Cell formulas with VB

"Mike Fogleman" wrote in message
...
Can VB variables be used in a cell formula?


No.


--

Vasant



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default Cell formulas with VB

Hi Mike
try
Range("A2:A90").FormulaArray ="=SUM(('" & nwsht &"'!$E$1:$E$" &
rowcount & "="""")*('" & nwsht &"'!$B$1:$B$" & rowcount & "=A2))"

note the multiple apostrophes: ( ' " &
--
Regards
Frank Kabel
Frankfurt, Germany

Mike Fogleman wrote:
Can VB variables be used in a cell formula? For example I have
columns of data on one sheet and a list to compare to on another. I
can put a formula in a cell and copy down the comparison list to get
what I want. This comparison list remains constant and part of the
workbook.
I can write this formula to a range of cells next to the comparison
list from VB:

Range("A2:A90").FormulaArray =
"=SUM((Dan!$E$1:$E$504="""")*(Dan!$B$1:$B$504=A2)) "

In this case a database named Dan.dbf was queried and brought in on a
new sheet which I named "Dan". Dan has 504 rows of data. This query
routine needs to work on any file queried, so I have assigned
variables to the sheet name and the rowcount
nwsht = "Dan"
rowcount = 504

How can I substitute these 2 variables into the above formula? I have
played with syntax variations of all I can think of.
Any advice will be greatly appreciated. Thanks, Mike


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Cell formulas with VB

Yes



Dim i As Integer
Dim s As String

s = "Dan"
i = 504
Range("A2:A90").FormulaArray = "=SUM((" & s & "!$E$1:$E$" & i &
"="""")*(" & s & "!$B$1:$B$" & i & "=A2))"


Also your formula has a circular reference to A2


---
Message posted from http://www.ExcelForum.com/

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,080
Default Cell formulas with VB

Sorry, I misunderstood your question ... didn't scroll down far enough!

--

Vasant

"Mike Fogleman" wrote in message
...
Can VB variables be used in a cell formula? For example I have columns of
data on one sheet and a list to compare to on another. I can put a formula
in a cell and copy down the comparison list to get what I want. This
comparison list remains constant and part of the workbook.
I can write this formula to a range of cells next to the comparison list
from VB:

Range("A2:A90").FormulaArray =
"=SUM((Dan!$E$1:$E$504="""")*(Dan!$B$1:$B$504=A2)) "

In this case a database named Dan.dbf was queried and brought in on a new
sheet which I named "Dan". Dan has 504 rows of data. This query routine
needs to work on any file queried, so I have assigned variables to the

sheet
name and the rowcount
nwsht = "Dan"
rowcount = 504

How can I substitute these 2 variables into the above formula? I have

played
with syntax variations of all I can think of.
Any advice will be greatly appreciated. Thanks, Mike






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,092
Default Cell formulas with VB

Thanks for the responses, guys. Frank was dead on with this one. Mudracker
was as close as I had gotten, but the single quotes had eluded both of us.
The circular ref to A2: A90 was a brain fart in writing the example formula,
B2:B90 would be a more correct example.
Thanks again guys, another good answer for my PERSONAL.XLS.


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
Two formulas in on cell based on two numbers in another cell? Melody Excel Discussion (Misc queries) 5 March 19th 10 07:51 PM
Carrying formulas over to new cell when old cell is deleted: possi Karl Excel Worksheet Functions 0 June 24th 08 01:10 PM
How to trace a cell? Which cells use a special cell in formulas? SupperDuck Excel Discussion (Misc queries) 4 December 1st 06 04:17 PM
Copying formulas from cell to cell to cell to....... Tom Hardy Excel Discussion (Misc queries) 3 June 15th 06 03:29 PM
change cell value greater than another cell value using formulas Unsure? Excel Worksheet Functions 2 April 2nd 06 10:24 PM


All times are GMT +1. The time now is 08:40 PM.

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

About Us

"It's about Microsoft Excel"