Solved

writing "=sum" function  form vb to excel

Posted on 2004-08-15
16
395 Views
Last Modified: 2010-05-02
trying to write =sum function from vb to sum some of the cells in excel
when i write


  .Cells(10,13).Formula = "=SUM(C13,E13,G13,I13,K13,M13,O13,Q13)"

the above statement writes the string as it is it writes =SUM(C13,E13,G13,I13,K13,M13,O13,Q13 in the specified cell (10,13)instead of calculating the value from the given cells. Can some one tell me how to do this please see that the cells are from alternate columns but in the same row.

thanks in advance. Its kind of urgent
Thanks agian
0
Comment
Question by:JuniorDeveloper2000
[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
  • 5
  • 3
  • 2
  • +4
16 Comments
 
LVL 12

Expert Comment

by:Gary Dewrell
ID: 11805829
Hi JuniorDeveloper2000,

See if this helps you out some.
http://class.et.byu.edu/ce270/vbaexcel_primer/excelfunctions.htm

God Bless
0
 
LVL 12

Expert Comment

by:Gary Dewrell
ID: 11805871
JuniorDeveloper2000,

I was able to do this with VB6

xlsheet.Application.cells(5, 1).Formula = "=sum(e1,e2,e3)"
0
 

Author Comment

by:JuniorDeveloper2000
ID: 11805876
No it didnot help i have to specify those particular cells specified above i cannot give a range of cells
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:Gary Dewrell
ID: 11805894
I was able to specify the specific cells. See my last post.
0
 

Author Comment

by:JuniorDeveloper2000
ID: 11805902
gdewrell,
i tried the same thing .cells(5, 1).Formula = "=sum(e10,e11)" but it still writes a string as>>>>>>>> =sum(e10,e11)i dont know why it does this
0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 11805908
Use Value ie

.Cells(10, 13).Value = "=SUM(C13,E13,G13,I13,K13,M13,O13,Q13)"
   
Cheers

Dave
0
 

Author Comment

by:JuniorDeveloper2000
ID: 11805945
brettdj,
still it just writes the string instead of calculating th values
0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 11806046
Can you please post the relevant code section for this

Thanks

Dave
0
 

Author Comment

by:JuniorDeveloper2000
ID: 11806125
For i = 0 To cnt - 1
        r = m_iCancelled + 2 + i  '//////// this would be the starting row
        str = "A" & r & ":" & "AC" & r
With m_oXLSheet
               .Cells.Range(str).Insert  ''''''''''''insert one row
               .Cells(r, 1) = strAry(i)   ''''''''''''insert value in col 1 stored in array
               strSum = "=SUM("       '''''''''''created a sum formula to sum
               For j = 1 To 12
                    c = j + j + 1           ''''''''''puts required column fileds in c which are alternate starting from 3 then 5 then 7....
                    .Cells(r, c) = 0        '''''''''''adds 0 to all the cells of the newly inserted row
                    If j = 12 Then         '''''''''''''removes the last comma which gets inserted in strSum
                        strSum = strSum & ch & r
                    Else
                        strSum = strSum & ch & r & ","
                    End If
               Next j
               strSum = strSum & ")"   '''''''''''this strSum does give me exactly the string which i want
              .Cells(r, 27) = strSum      '''''''but here is the problem this line writes the string instead of calculating
                                                   ''''''''i tried with .cells(r,27).formula = strSum and also with .cells(r,27).value = strSum
                                                   '''''''' still puts the string in cell(r,27) instead of calculating it
       End With
Next i

i spend a long time figuring it out can some 1 please help its urgernt
0
 
LVL 12

Expert Comment

by:BobLamberson
ID: 11806941
Hi JuniorDeveloper2000 ,

Your code is doing what you are telling it to - putting the formula in the cell.

Now you need to cause a calculation of that formula, like you would use F9 in Excel.

Try using the Calculate method on your worksheet object
        m_oXLSheet.UsedRange.Columns("A:C").Calculate
    change the columns to what ever columns you want.


Here is a copy of MS help on the subject;

  Calculate Method
       
  Calculates all open workbooks, a specific worksheet in a workbook, or a specified range of cells on a worksheet, as shown in the following table.

  To calculate Follow this example
  All open workbooks Application.Calculate (or just Calculate)
  A specific worksheet Worksheets(1).Calculate
  A specified range Worksheets(1).Rows(2).Calculate

  expression.Calculate

  expression   Optional for Application, required for Worksheet and Range. An expression that returns an object in the Applies To list.

  Example
  This example calculates the formulas in columns A, B, and C in the used range on Sheet1.

  Worksheets("Sheet1").UsedRange.Columns("A:C").Calculate



Bob
0
 
LVL 8

Expert Comment

by:SNilsson
ID: 11807317
For i = 0 To cnt - 1
r = m_iCancelled + 2 + i  
str = "A" & r & ":" & "AC" & r

With m_oXLSheet
    .Cells.Range(str).Insert  
    .Cells(r, 1) = strAry(i)  
    strSum = "=SUM("      
    For j = 1 To 12
       c = j + j + 1        
       .Cells(r, c) = 0      
       If j = 12 Then        
          strSum = strSum & ch & r
       Else
          strSum = strSum & ch & r & ","
       End If
    Next j
    strSum = strSum & ")"  
    .Cells(r, 27).formula = strSum        
End With
Next i
m_oXLSheet.calculate
0
 
LVL 35

Expert Comment

by:mvidas
ID: 11809491
Hi JD2000,

If you want the sum placed in the cell instead of the formula, you could try

.Cells(10, 13) = Excel.Application.WorksheetFunction.Sum([C13], [E13], [G13], [I13], [K13], [M13], [O13], [Q13])

 or if its not recognizing the [ ] shorthand, use:

.Cells(10, 13) = Excel.Application.WorksheetFunction.Sum(Range("C13"), Range("E13"), _
 Range("G13"), Range("I13"), Range("K13"), Range("M13"), Range("O13"), Range("Q13"))

Matt
0
 

Author Comment

by:JuniorDeveloper2000
ID: 11827446
it started working when i change the column format from text to numeric.. i know it is kind of silly.
.cells(row,col).values = "sum(a1,b1,c1)" worked when i change the column format to numeric.
Thanks all for your effort
0
 
LVL 1

Accepted Solution

by:
GhostMod earned 0 total points
ID: 12528671
PAQed with points refunded (500)

GhostMod
Community Support Moderator
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to debug this code 7 65
How to set the sa password in a vb6 code for sql connection 9 63
Problem to With line 4 75
Automatic Email Reminder 4 75
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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

726 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