473,382 Members | 1,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Problem with .Formula = in Excel VBA

Excel VBA subroutine:
The Cell coordinates are determined by a couple of loops which give an x & y location for the cell in the form
Cells(x , y).
I want to calculate and enter into this cell the result of a Formula.
Basically Cell(x , y) = Cell(a , b) minus Cell(s , t)
Here's the code that's giving me the problem...
Expand|Select|Wrap|Line Numbers
  1.  
  2. cells(x , y).Formula = "=sum(cells(a , b)- cells(s , t))"
  3.  
  4. 'An alternative that I tried was directly accessing the worksheet, but it still didn't work
  5.  
  6. Cells(x , y).Formula = "=sum(Main!H20-Main!H22)
  7.  
  8.  
Nov 22 '13 #1
6 2322
Killer42
8,435 Expert 8TB
You haven't actually told us what problem you're having with this code.

I'LL take a stab at it, though. Where you've said cells(a , b) it's likely you intended to use the values of a and b in there.

I'm thinking something like...
Expand|Select|Wrap|Line Numbers
  1. Cells(x , y).Formula = "=sum(cells(" & a & " , " & b & ")- cells(" & s & " , " & t & "))"
  2.  
Um... something else here doesn't really make sense. The Sum function is used to add up a number of values. Not sure why you're using it, since you're only working with a single (calcaulted) value. As an alternative, you might try this...
Expand|Select|Wrap|Line Numbers
  1. Cells(x , y).Formula = "=cells(" & a & " , " & b & ") - cells(" & s & " , " & t & ")"
  2.  
Nov 23 '13 #2
Killer42
8,435 Expert 8TB
Oh, and a general tip. You'll probably find more VBA expertise in the Microsoft Access / VBA forum.
Nov 23 '13 #3
Hi Killer42,
I tried to simplify the problem, but I guess I should have described the problem I having in more detail.

I am using the SUM as I am indeed trying to compute the arithmetic sum of a column of values (normally on a sheet I'd use =sum(a1:a8) for instance.

Also I'm trying to subtract just 2 values from locations on the sheet. I agree the =cells would have been more correct in that instance.

The Error:
I get "Expected End of Statement" in the VBA code and the first comma (after the =sum) is highlighted in both the .Formulas below.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  Cells(x + y, "D").Formula = "=sum(cells("&x+1&" , "D") - cells("&x+y+2&" , "D"))"
  3.  
  4. Also the same error msg in the following column sum
  5.  
  6.  Cells(x + z, "D").Formula = "=sum(cells("&x+1&" , "D") : cells("&x+y+2&" , "D"))"
  7.  
  8. The idea is to subtract the last entry from the first as well as summing all the entries in the column - hence the two .Formulas
  9.  
  10.  
Nov 23 '13 #4
Hi - I've fixed the VBA compilation error that I was getting in the above post ....... however the formula still doesn't work as it gives a worksheet error of #NAME? which means it doesn't like the word cells in the formula.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  Cells(x + y + 4, "D").Formula = "=sum(cells(x+1 , 4) : cells(x+y+1 , 4))"
  3.  
  4.  
The problem is I don't know how else to refer to the cells as their location on the worksheet changes.
Nov 23 '13 #5
Killer42
8,435 Expert 8TB
I don't think Cells(x + y + 4, "D") is correct to address a cell in VB code. For the two coordinates you're providing a numeric expression and a string.

It seems as though you are still having a problem incorporating variables in your expressions. If I remove the quotes around the "D" in this latest line of code, leaving this...
Expand|Select|Wrap|Line Numbers
  1. Cells(x + y + 4, D).Formula = "=sum(cells(x+1 , 4) : cells(x+y+1 , 4))"
...then this means you are placing this exact formula in a cell: "=sum(cells(x+1 , 4) : cells(x+y+1 , 4))". Would that work if you typed it directly into the cell? I think it's more likely that you intended to take the values of x and y, and insert them into the formula. For example, if we assume the values of x and y were 8 and 12 respectively, would you be expecting to see this formula placed in the cell?
Expand|Select|Wrap|Line Numbers
  1. =sum(cells(9 , 4) : cells(21 , 4))
If so, then the code would be more like this:
Expand|Select|Wrap|Line Numbers
  1. Cells(x + y + 4, D).Formula = "=sum(cells(" & x+1 & ", 4) : cells(" & x + y + 1 & " , 4))"
Sorry if I'm misunderstanding the issue here.
Dec 18 '13 #6
MikeTheBike
639 Expert 512MB
Hi

I am unclear, are wanting to enter a formula to calculate the result or calculate the result and enter the value? So below I have posted code to do both. Also I have illustrated relative and absolute addresss formular. Both formula example Use R1C1 notation, with an alternative using the cell addresses.
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.     Dim x As Integer
  3.     Dim y As Integer
  4.     Dim a As Integer
  5.     Dim b As Integer
  6.     Dim s As Integer
  7.     Dim t As Integer
  8.  
  9. Sub InsertValue()
  10.     x = 2
  11.     y = 2
  12.     a = 3
  13.     b = 3
  14.     s = 4
  15.     t = 4
  16.  
  17.     Cells(x, y) = Cells(a, b) - Cells(s, t)
  18.  
  19. End Sub
  20.  
  21. Sub InsertFormula_Rel()
  22.     x = 2
  23.     y = 2
  24.     a = 3
  25.     b = 3
  26.     s = 4
  27.     t = 4
  28.  
  29.     Cells(x, y).FormulaR1C1 = "=R[" & a - x & "]C[" & b - y & "]-R[" & s - x & "]C[" & t - y & "]"
  30.     'Cells(x, y).Formula = "=" & Replace(Cells(a, b).Address, "$", "") & "-" & Replace(Cells(s, t).Address, "$", "")
  31. End Sub
  32.  
  33. Sub InsertFormula_Abs()
  34.     x = 2
  35.     y = 2
  36.     a = 3
  37.     b = 3
  38.     s = 4
  39.     t = 4
  40.  
  41.     Cells(x, y).FormulaR1C1 = "=R" & a & "C" & b & "-R" & s & "C" & t
  42.     'Cells(x, y).Formula = "=" & Cells(a, b).Address & "-" & Cells(s, t).Address
  43. End Sub
I don't know if this any use to you but maybe the info my be usefull at some point in the future!

MTB
Dec 18 '13 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Michael Jordan | last post by:
I'm hoping that someone here can give me some insight into a problem I'm running into with Python, pywin32 and Excel. All-in-all using Python and pywin32 is great but I've run into a strange...
0
by: Sathya | last post by:
Hi Am using an ASP.NET website. i have an excel template stored in a folder in the server. the excel template already has some data inside. i need to add some more data to the excel sheet...
0
by: Adrian Belen via .NET 247 | last post by:
hi, I have a problem when I try to open an excel document in a AxWebBrowser Component in VB.NET. The problem appear in this situation: Before to all the Excel application is opened from the...
4
by: Ivan | last post by:
Hi All, I have tried to automate excel in vb.net and i found a problem that i can't find the solution in anywhere... i hope someone can help me in this group.... the problem is i try using...
6
by: Darrell Wesley | last post by:
A VB2003 application upgraded to VB2005 that builds an Excel spreadsheet. Everything appears to work correctly except that the Excel object does not go away it is still in the Process list in task...
0
by: Ron | last post by:
Hey Guys, I have an online system which use Excel 2003 to generate a report. the system has been developed on .NET 1.1 platform and the operating system are either windows XP or windows 2003. ...
15
by: =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?= | last post by:
Hi All, We are in the process of Upgrade Excel 2003 (Office 2003) to Excel 2007 (Office 2007) for one of web application. This web application is using Excel (Pivot Table) reports. With Excel...
3
chandru8
by: chandru8 | last post by:
Hi to all I had a problem in excel file.., I am generating a new excel file through vb6.0 and i can access data from that excel. if user opens another excel file and closes my excel file...
4
by: John Brock | last post by:
I have a .NET application that, among other things, creates Excel workbooks, and I have run into a very strange problem involving formulas on one worksheet that reference values on another...
1
by: raul15791 | last post by:
Hi, I'm new to C#. I'm writing a program that open a new excel file, write into it and lastly close the file. But the program is that there will be a orphaned process named EXCEL.EXE left on the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.