Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What code gives SUM of user-selected cell range on a sheet?

Status
Not open for further replies.

robocorp

Technical User
Feb 14, 2001
2
CA
I want to code the click event of a command button to sum a range of currency formatted cells that I select in a worksheet, and then display the result in a label or text box with the same format. What do I need? Any help is greatly appreciated, thanks!!

 
I solved this myself, although the original idea has changed somewhat, it works for me. I used an Input box to insert a range of cells into my code. Now if I want to total a column of figures I select the cell where I want my total to be displayed and click the command button. The Input Box asks for the range I want to total, once I enter the range and click ok the total of the range of cells is displayed in the cell I selected. Works great!
 
Do u have the code for Summing a range of values in an excel column in VB.
I am trying to get the 'Sum' in excel to work.

here's a part of my code.
Dim CCExcelApp:
Set ExcelApp = CreateObject("Excel.application")
With ExcelApp
.workbooks.open "filename.xls"
row = 7
For i = 1 To 20
.range("A" & CStr(row)).Select
.activecell.formular1c1 = i
.range("B" & CStr row)).Select
.activecell.formular1c1 = i
row = row + 1

Next
.range("A" & CStr(row)).Select
.selection.entirerow.Insert
.range("A" & CStr(row)).Select
.activecell.formular1c1 = "Total"
.range("B" & CStr(row)).Select
.activecell.formular1c1 = "=SUM(R[-9]B:R[-7]B)"
.range("A1").Select
.Visible = True
End With
Set ExcelApp = Nothing



I am gettin an runtime error and the code stops at the 'sum' line
 
Do u have the code for Summing a range of values in an excel column in VB.
I am trying to get the 'Sum' in excel to work.

here's a part of my code.
Dim CCExcelApp
Set ExcelApp = CreateObject("Excel.application")
With ExcelApp
.workbooks.open "filename.xls"
row = 7
For i = 1 To 20
.range("A" & CStr(row)).Select
.activecell.formular1c1 = i
.range("B" & CStr row)).Select
.activecell.formular1c1 = i
row = row + 1

Next
.range("A" & CStr(row)).Select
.selection.entirerow.Insert
.range("A" & CStr(row)).Select
.activecell.formular1c1 = "Total"
.range("B" & CStr(row)).Select
.activecell.formular1c1 = "=SUM(R[-9]B:R[-7]B)"
.range("A1").Select
.Visible = True
End With
Set ExcelApp = Nothing



I am gettin an runtime error and the code stops at the 'sum' line.
Any help is highly appreciated.
 
robocorp:

put in button click event (if still interested):

Dim oCell As Range
Dim nSum As Long

For Each oCell In Selection.Cells
nSum = nSum + oCell.Value
Next oCell

Set oCell = Nothing

MsgBox nSum


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top