View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.misc
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default Summing random cells as you click on them

Maybe you could just ask the user to select the range in the macro and then
cycle through the selection:

Option Explicit
Sub testme()
Dim myRng As Range
Dim myCell As Range
Dim oWks As Worksheet
Dim oRow As Long

Set myRng = Nothing
On Error Resume Next
Set myRng = Application.InputBox(prompt:="Select a range", Type:=8)
On Error GoTo 0

If myRng Is Nothing Then
Exit Sub 'user hit cancel
End If

Set oWks = Workbooks.Add(1).Worksheets(1)

oRow = 0
For Each myCell In myRng.Cells
oRow = oRow + 1
oWks.Cells(oRow, "A").Value = myCell.Address(external:=True)
With oWks.Cells(oRow, "B")
.NumberFormat = myCell.NumberFormat
.Value = myCell.Value
End With
Next myCell

With oWks
.Cells(oRow + 1, "B").FormulaR1C1 = "=sum(r1c2:r[-1]c)"
.UsedRange.Columns.AutoFit
End With

End Sub


Ben in CA wrote:

I need to create a "program" with an Excel document that allows me to click a
button like "start adding" and then I can click on random cells (well, they
wouldn't actually be random, but not in any series of columns or rows), and
the contained values to be copied to successive cells in an empty column on
the side, that I can then have a Sum setup up on.

Basically, I have different pieces of equipment listed in an Excel
spreadsheet, along with various options. (Several hundred) I want to be able
to select which ones (even just by clicking on the price/value), and
automatically add them together - currently this is done on paper. (The costs
of the various options, etc. are scattered about, and aren't all in a
particular row or column.)

Once I'm done calculating it, probably I'd need to have a "Stop adding"
button that I could press and then it would stop calculating the value.

Preferably it only copies the actual number in the cells to the new
location, as many of the cells are themselves calculated by formulas.

Can anyone think of a way of doing this? I've been looking around for a way
to do this, but I haven't found anything that will do it yet.

Thanks in advance to anyone who solves this problem!


--

Dave Peterson