Excel VBA about total up all values in a range and write it at a desired cell

huiyin9218

Board Regular
Joined
Aug 7, 2018
Messages
53
Hi,

May i know how to write a code to sum up the numbers of each date in second column and write it at the side of each serial number (For example : location A and B)?
As you can see, serial number 1 has 2 dates and serial number 2 only have one date, how do i write a code to sum all the numbers between the serial numbers as the dates sometimes can go up to 5 dates per serial number?


Serial Number 1A
8 july 20181
4 july 20181
Serial Number 2B
8 july 20181

<tbody>
</tbody>

I would be so grateful for your help.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hi,

Do I understand correctly you want the cell after the date to show many times this specific date occurs within the serial number? It would be of great help if you were to give us a depiction of the results you wish to get.
 
Upvote 0
Hi,

Sorry for not stating it clearly.
I wish to sum up the number of "1" in each serial number, the sum will be written at two cells next to the serial number (the answer that i wish to get is shown with red color in the table)

Serial Number 12
8 july 20181
4 july 20181
Serial Number 21
8 July 20181
Serial Number 33
7 July 20181
2 July 20181
1 July 20181

<tbody>
</tbody>
 
Upvote 0
This is a VERY quick and dirty solution and could surely be done better, but it works.
I've pasted this in my Worksheet module, if used in another module make sure to get the references right:
Code:
Sub datecounter()
For Each cell In Range("B:B")
If cell = "" And cell.Offset(1, 0) = "" Then Exit For
If IsEmpty(cell.Value) Or cell.Value = "" Or cell.Row = Cells(Rows.Count, "B").End(xlUp).Row Then cell.Offset(0, 1).Value = "x"
Next cell
For Each cell In Range("C:C")
If cell.Row = Cells(Rows.Count, "B").End(xlUp).Row Then
cell.Value = ""
Exit For
End If
If cell.Value = "x" Then
Debug.Print cell.Address(0, 0)
nextx = Range("C:C").Find("x", Range(cell.Address(0, 0))).Offset(0, -1).Address(0, 0)
cell.Value = WorksheetFunction.Sum(Range(cell.Offset(0, -1).Address(0, 0) & ":" & nextx))
End If
Next cell
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,429
Messages
6,124,840
Members
449,193
Latest member
MikeVol

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