Sub TotalPurchaseAmounts()
Application.Run "UnProtect"
Dim PurchTotalLoc As Variant
Dim userResponce As Range
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Dim rng4 As Range
Dim rng5 As Range
Dim rng6 As Range
Dim rng7 As Range
Dim rng8 As Range
Dim TargetSheet As Range
On Error Resume Next
Application.DisplayAlerts = False
' Would like to use Range Name "TargetSheet"...but does not work.......COMPILE ERROR - SUB OR FUNCTION NOT DEFINED
' Sheet(TargetSheet).Activate '*****THIS LINE CAUSES THE ERROR
Sheet1.Activate 'don't want to keep this line as it is static
Application.Run "UnProtect"
Set userResponce = Application.InputBox("Please select the cell where you want the Total to appear", _
Title:="Select the cell for your Total", Type:=8)
If userResponce Is Nothing Then Exit Sub
Let PurchTotalLoc = userResponce.Address
On Error Resume Next
'Ask the user for up to eight cells
Set rng1 = Application.InputBox("Click on first cell to be totalled", "First Cell", , , , , , 8)
If rng1 = "" Then GoTo ext:
Set rng2 = Application.InputBox("Click on second cell to be totalled", "Second Cell", , , , , , 8)
If rng2 = "" Then GoTo ext:
Set rng3 = Application.InputBox("Click on third cell to be totalled", "Third Cell", , , , , , 8)
If rng3 = "" Then GoTo ext:
Set rng4 = Application.InputBox("Click on fourth cell to be totalled", "Fourth Cell", , , , , , 8)
If rng4 = "" Then GoTo ext:
Set rng5 = Application.InputBox("Click on fifth cell to be totalled", "Fifth Cell", , , , , , 8)
If rng5 = "" Then GoTo ext:
Set rng6 = Application.InputBox("Click on sixth cell to be totalled", "Sixth Cell", , , , , , 8)
If rng6 = "" Then GoTo ext:
Set rng7 = Application.InputBox("Click on seventh cell to be totalled", "Seventh Cell", , , , , , 8)
If rng7 = "" Then GoTo ext:
Set rng8 = Application.InputBox("Click on eigth cell to be totalled", "Eighth Cell", , , , , , 8)
If rng8 = "" Then GoTo ext:
'Output to a specific user-selected cell (called "PurchTotalLoc") via InputBox
ext:
Range(PurchTotalLoc) = Application.WorksheetFunction.Sum(rng1, rng2, rng3, rng4, rng5, rng6, rng7, rng8)
'puts total in if one selects 8 amounts, but not if one enters less than 8 and hits "cancel"
Application.Run "Protect"
End Sub
Bookmarks