The revolutionary project management tool is here! Plan visually with a single glance and make sure your projects get done.

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

when i write

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

the above statement writes the string as it is it writes =SUM(C13,E13,G13,I13,K13,M

thanks in advance. Its kind of urgent

Thanks agian

See if this helps you out some.

http://class.et.byu.edu/ce270/vbaexcel_primer/excelfunctions.htm

God Bless

I was able to do this with VB6

xlsheet.Application.cells(

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

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

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.Colum

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).Calc

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").UsedR

Bob

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

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

.Cells(10, 13) = Excel.Application.Workshee

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

.Cells(10, 13) = Excel.Application.Workshee

Range("G13"), Range("I13"), Range("K13"), Range("M13"), Range("O13"), Range("Q13"))

Matt

All Courses

From novice to tech pro — start learning today.

GhostMod

Community Support Moderator