miqbal

New Member
Joined
Mar 7, 2018
Messages
11
Hi Forum,

example:
a
b
c
d
e
1
2
5
6
7
8
sum (e5:e8)

<tbody>
</tbody>

suppose above is the data on sheet 1

I want to find out sum of last current region of column "E" starting from "5" to "8"

since I am new to VBA I tried below code but no success.


Sub findtotal()​

firstrow = Sheet1.Cells(Rows.Count, 5).End(xlUp).End(xlUp)​

lastrow = Sheet1.Cells(Rows.Count, 5).End(xlUp)​

MsgBox firstrow
MsgBox lastrow​

totalval = Application.WorksheetFunction.Sum(Sheet1.Range(firstrow, lastrow))​

MsgBox totalval​


End Sub​


"totalval" part is not working and I know the reason because syntax is not right.

please if someone help to resolve it and correct the code.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG09Mar58
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = Range("E:E").SpecialCells(xlCellTypeConstants)
MsgBox Application.Sum(Rng.Areas(Rng.Areas.Count))
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
The issues you had with your code is that you were returning the values from cells E5 and E8, not the row numbers (you didn't notice that because your row numbers and values matched).
Also, in building your range, you included the row numbers but not the column references.

To carry on with your method, here is how you could get it to work:
Code:
Sub findtotal()

Dim firstrow As Long, lastrow As Long
Dim myRange As Range

firstrow = Sheet1.Cells(Rows.Count, 5).End(xlUp).End(xlUp)[COLOR=#ff0000].Row[/COLOR]
lastrow = Sheet1.Cells(Rows.Count, 5).End(xlUp)[COLOR=#ff0000].Row[/COLOR]
Set myRange = Sheet1.[COLOR=#ff0000]Range(Cells(firstrow, 5), Cells(lastrow, 5))[/COLOR]
totalval = Application.WorksheetFunction.Sum(myRange)
MsgBox totalval

End Sub
 
Upvote 0
Alternately (to Joe4's posting), you could have approached it this way where you work with the found cell rather than their row numbers (note that I changed some of your variable names to make them more descriptive and that I also declared all my variables as to their data type)...
Code:
[table="width: 500"]
[tr]
	[td]Sub FindTotal()

  Dim FirstCell As Range, LastCell As Range, TotalVal As Double
  
  Set FirstCell = Sheet1.Cells(Rows.Count, 5).End(xlUp).End(xlUp)
  Set LastCell = Sheet1.Cells(Rows.Count, 5).End(xlUp)
  
  MsgBox FirstCell.Row
  MsgBox LastCell.Row
  
  TotalVal = Application.WorksheetFunction.SUM(Range(FirstCell, LastCell))
  
  MsgBox TotalVal

End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,729
Members
449,049
Latest member
MiguekHeka

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top