"Diane Frost" <dfr...@pcblawfirm.com> wrote in message
news:060401c2fed5$6c5b09f0$a001...@phx.gbl...
Sub Rollem()
Dim lSum As Long
With Application.WorksheetFunction
Range("B5").Value = .Sum(Range("B5:B7"))
End With
End Sub
"Diane Frost" <dfr...@pcblawfirm.com> wrote in message
news:060401c2fed5$6c5b09f0$a001...@phx.gbl...
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Sub RollC()
With Application.WorksheetFunction
Range("C8") = .Sum(Range("C5:C7"))
End With
Range("C5") = Range("C8")
End Sub
Sub RollD()
With Application.WorksheetFunction
Range("D8") = .Sum(Range("D5:D7"))
End With
Range("D5") = Range("D8")
End Sub
(You may notice "Dim lSum As Long" is missing. It is unneeded.)
BTW, these macros work on the worksheet that is active when they are run -
whatever it is - even if it is the "wrong" one. There are many ways to
specify which worksheet to act on. Stay tuned.
Bob
"Diane Frost" <dfr...@pcblawfirm.com> wrote in message
news:#GmLFo2$CHA....@TK2MSFTNGP11.phx.gbl...
I have named these procedures Roll, RollB, RollC, etc. because I am not very
creative. (Yeah, right. ;-)) You can rename them to whatever makes sense to
you, so long as they are unique in a workbook. It is best to be as explicit
as possible about which workbook and worksheet the macros work on, lest an
inadvertent action hose otherwise satisfactory work. I have provided several
examples of specifying worksheets. Pick a method that suits you and change
names as required.
Bob
'----------------------------------------------------------
Option Explicit
Private Sub Roll(rng As Range)
'Check the range size.
If rng.Cells.Count = 4 And _
(rng.Columns.Count = 1 Or rng.Rows.Count = 1) Then
With Application.WorksheetFunction
rng(rng.Count) = .Sum(Range(rng.Cells(1), rng.Cells(3)))
End With
rng.Cells(1) = rng(rng.Count)
Else
'Warn the user if range is wrong size.
MsgBox "Wrong range size (#" & CStr(rng.Cells.Count) & _
" !!). Range size must be 4 adjacent " & _
"cells in a row or a column.", , "U-Roll'em!"
End If
End Sub
Public Sub RollB()
Roll ThisWorkbook.Worksheets("Sheet1").Range("B5:B8")
'Works on the sheet named "Sheet1" in the workbook
'where this macro is stored - ThisWorkbook.
End Sub
Public Sub RollC()
Roll Workbooks("Book1.xls"). _
Worksheets("Sheet1").Range("C6:C9")
'Works on the specified range in
'Book1.xls, Sheet1.
'Must exist and be open.
End Sub
Public Sub RollD()
Roll ActiveSheet.Range("D7:D10")
'Works on the specified range in
'whatever workbook is active.
End Sub
Public Sub RollA()
Roll ActiveSheet.Cells
'Tests range checking of Roll().
End Sub
Public Sub RollButGoSlow()
'Throw up an "are you sure?" dialog.
Dim rng As Range
Set rng = ActiveSheet.Range("B8:E8")
Dim msg As String
msg = "Are you sure you want to do the range "
msg = msg & ActiveWorkbook.Name & ", "
msg = msg & ActiveSheet.Name
msg = msg & "(" & rng.Address & ")"
'Check answer and continue.
If (vbYes = _
MsgBox(msg, vbYesNo + vbDefaultButton2, _
"U-Roll'em!")) Then
Roll rng
'Works on the specified range in
'whatever worksheet is active.
End If
Set rng = Nothing
End Sub
"Diane Frost" <dfr...@pcblawfirm.com> wrote in message
news:#GmLFo2$CHA....@TK2MSFTNGP11.phx.gbl...