Not sure what code to use.

Jacko1996

New Member
Joined
May 29, 2015
Messages
21
Hey so I have a whole bunch of daily data for a weather station. What I want it to do is create a table made of monthly data for all the years using the daily data. I'm not sure what formula to use.

The table below shows kind what it looks like, however on a much larger scale

YearMonthDayRainfall amount (millimetres)
18931210
18931220
18931230
18931240
18931250
18931260
18931270
18931280
18931290
189312100
189312110
189312120
189312130
189312140
189312150
189312160
189312170
189312182
189312190
189312200
189312210
189312220
189312230
189312240
189312250
189312260
189312270
189312280
189312290
189312300
189312310
1894117.9
1894120
1894130
1894140
1894150
1894162.3
1894170
1894180
18941964.5
189411012.7
18941110
18941120
189411331.8
189411429.7
189411538.4
18941168.1
18941170
18941180
18941199.7
18941200
18941210
189412299.1
18941230
189412416.5
189412514.7
18941260.3
18941274.8
18941284.8
189412956.4

<colgroup><col><col><col><col></colgroup><tbody>
</tbody>

Below is what i want it to look like the values are made up but those would be the values of rainfall for the months.

YearJanFebMarAprMayJuneJulyAugSepNovOctDec
1983000000462012080
19842304366826130034234679

<tbody>
</tbody>
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
If your daily data in columns A:D on sheet 1 and your summary table starts in Cell A1 on Sheet 2:

Code:
Cell B2 =SUMIFS('Sheet1'$D:$D,'Sheet1'$A:$A,'Sheet2'$A2,'Sheet1'$B:$B,COLUMN()-1)

This formula can be copied across and down as needed.

Dan
 
Upvote 0
Jacko1996,

1. What version of Excel, and, Windows are you using?

2. Are you using a PC or a Mac?


Here is a macro solution for you to consider, based on your posted data, with your raw data sorted/grouped by column A, Year, and, column B, Month.

You can change the raw data worksheet name in the macro.

Sample raw data:


Excel 2007
ABCD
1YearMonthDayRainfall amount (millimetres)
218931210
318931220
418931230
518931240
618931250
718931260
818931270
918931280
1018931290
11189312100
12189312110
13189312120
14189312130
15189312140
16189312150
17189312160
18189312170
19189312182
20189312190
21189312200
22189312210
23189312220
24189312230
25189312240
26189312250
27189312260
28189312270
29189312280
30189312290
31189312300
32189312310
331894117.9
341894120
351894130
361894140
371894150
381894162.3
391894170
401894180
4118941964.5
42189411012.7
4318941110
4418941120
45189411331.8
46189411429.7
47189411538.4
4818941168.1
4918941170
5018941180
5118941199.7
5218941200
5318941210
54189412299.1
5518941230
56189412416.5
57189412514.7
5818941260.3
5918941274.8
6018941284.8
61189412956.4
62
Sheet1


After the macro in a new worksheet Rainfall:


Excel 2007
ABCDEFGHIJKLM
1YearJanFebMarAprMayJuneJulyAugSepOctNovDec
21893000000000002
31894401.700000000000
4
Rainfall


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below macro code, and, function
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub YearMonthRainfall()
' hiker95, 06/02/2015, ME858813
Dim w1 As Worksheet, wr As Worksheet
Dim o As Variant, j As Long, rng As Range, nlr As Long
Dim lr As Long, nrl As Long, r As Long, c As Long, n As Long, m As Long, msum As Double
Application.ScreenUpdating = False
Set w1 = Sheets("Sheet1")   '<-- you can change the sheet name here
With w1
  lr = .Cells(Rows.Count, 1).End(xlUp).Row
  With .Range("E2:E" & lr)
    .Formula = "=A2&B2"
    .Value = .Value
  End With
  Set rng = .Range("E2:E" & lr)
  nlr = CountUnique(rng)
  ReDim o(1 To nlr + 1, 1 To 13)
  j = j + 1: o(j, 1) = "Year"
  o(j, 2) = "Jan": o(j, 3) = "Feb": o(j, 4) = "Mar": o(j, 5) = "Apr"
  o(j, 6) = "May": o(j, 7) = "June": o(j, 8) = "July": o(j, 9) = "Aug"
  o(j, 10) = "Sep": o(j, 11) = "Oct": o(j, 12) = "Nov": o(j, 13) = "Dec"
  For r = 2 To lr
    n = Application.CountIf(.Columns(5), .Cells(r, 5).Value)
    j = j + 1: o(j, 1) = .Cells(r, 1).Value
    For c = 2 To UBound(o, 2)
      o(j, c) = 0
    Next c
    m = .Cells(r, 2) + 1
    msum = Evaluate("=Sum(D" & r & ":D" & r + n - 1 & ")")
    o(j, m) = msum
    r = r + n - 1
  Next r
  rng.ClearContents
End With
If Not Evaluate("ISREF(Rainfall!A1)") Then Worksheets.Add(After:=w1).Name = "Rainfall"
Set wr = Worksheets("Rainfall")
With wr
  .UsedRange.Clear
  .Cells(1, 1).Resize(UBound(o, 1), UBound(o, 2)) = o
  .Columns("A:M").AutoFit
  .Activate
End With
End Sub
Function CountUnique(ByVal rng As Range) As Long
' Juan Pablo González, MrExcel MVP, 05/09/2003
' http://www.mrexcel.com/forum/excel-questions/48385-need-count-unique-items-column-visual-basic-applications.html
Dim St As String
Set rng = Intersect(rng, rng.Parent.UsedRange)
St = "'" & rng.Parent.Name & "'!" & rng.Address(False, False)
CountUnique = Evaluate("SUM(IF(LEN(" & St & "),1/COUNTIF(" & St & "," & St & ")))")
End Function

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the YearMonthRainfall macro.
 
Upvote 0
Here is another macro that you can consider...
Code:
Sub YearMonthRainfallToo()
  Dim r As Long, LastRow As Long, Data As Variant, Result As Variant
  LastRow = Sheets("[B][COLOR="#0000FF"]Sheet1[/COLOR][/B]").Cells(Rows.Count, "A").End(xlUp).Row
  Data = Sheets("[B][COLOR="#0000FF"]Sheet1[/COLOR][/B]").Range("A2:D" & LastRow)
  ReDim Result(Data(1, 1) To Data(UBound(Data), 1), 0 To 12)
  For r = 1 To UBound(Data) - LBound(Data) + 1
    If Result(Data(r, 1), 0) = "" Then Result(Data(r, 1), 0) = Data(r, 1)
    Result(Data(r, 1), Data(r, 2)) = Result(Data(r, 1), Data(r, 2)) + Data(r, 4)
  Next
  With Sheets("[B][COLOR="#0000FF"]Rainfall[/COLOR][/B]")
    .Cells.Clear
    .Range("A1:M1") = Array("Year", "Jan", "Feb", "Mar", "Apr", "May", _
                      "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
    .Range("A2").Resize(UBound(Result) - LBound(Result) + 1, 13) = Result
    On Error Resume Next
    .Range("A2").Resize(UBound(Result) - LBound(Result) + 1, 13).SpecialCells(xlBlanks) = 0
    On Error GoTo 0
  End With
End Sub
 
Last edited:
Upvote 0
What about a pivot table? This is precisely the sort of thing pivot tables were designed for. Months as column headers, years as the row headers, the rainfall is the value - sum / average / whatever you want. So long as your data is clean and contiguous it shouldn't be a problem.
 
Upvote 0
Jacko1996,

Here is another macro solution for you to consider, based on your posted data, with your raw data sorted/grouped by column A, Year, and, column B, Month.

You can change the raw data worksheet name in the macro.

Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below macro code, and, function
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub YearMonthRainfall_V2()
' hiker95, 06/03/2015, ME858813
Dim w1 As Worksheet, wr As Worksheet
Dim o As Variant, j As Long, rng As Range, nlr As Long
Dim lr As Long, nrl As Long, r As Long, n As Long, m As Long, msum As Double
Application.ScreenUpdating = False
Set w1 = Sheets("Sheet1")   '<-- you can change the sheet name here
With w1
  .Activate
  lr = .Cells(Rows.Count, 1).End(xlUp).Row
  With .Range("E2:E" & lr): .Formula = "=A2&B2": .Value = .Value: End With
  Set rng = .Range("E2:E" & lr)
  nlr = CountUnique_V2(rng)
  ReDim o(1 To nlr, 1 To 13)
  For r = 2 To lr
    n = Application.CountIf(.Columns(5), .Cells(r, 5).Value)
    j = j + 1: o(j, 1) = .Cells(r, 1).Value: m = .Cells(r, 2) + 1
    msum = Evaluate("=Sum(D" & r & ":D" & r + n - 1 & ")"): o(j, m) = msum
    r = r + n - 1
  Next r
  rng.ClearContents
End With
If Not Evaluate("ISREF(Rainfall!A1)") Then Worksheets.Add(After:=w1).Name = "Rainfall"
Set wr = Sheets("Rainfall")
With wr
  .UsedRange.Clear
  .Cells(1, 1).Resize(, 13).Value = Array("Year", "Jan", "Feb", "Mar", "Apr", "May", _
                                  "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec")
  .Cells(2, 1).Resize(UBound(o, 1), UBound(o, 2)) = o
  On Error Resume Next
  .Range("B2").Resize(UBound(o, 1), UBound(o, 2)).SpecialCells(xlBlanks) = 0
  On Error GoTo 0
  .Columns("A:M").AutoFit
  .Activate
End With
End Sub
Function CountUnique_V2(ByVal rng As Range) As Long
' Juan Pablo González, MrExcel MVP, 05/09/2003
' http://www.mrexcel.com/forum/excel-questions/48385-need-count-unique-items-column-visual-basic-applications.html
Dim St As String
Set rng = Intersect(rng, rng.Parent.UsedRange)
St = "'" & rng.Parent.Name & "'!" & rng.Address(False, False)
CountUnique_V2 = Evaluate("SUM(IF(LEN(" & St & "),1/COUNTIF(" & St & "," & St & ")))")
End Function

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the YearMonthRainfall_V2 macro.
 
Upvote 0
Here is another macro that you can consider...
Code:
Sub YearMonthRainfallToo()
  Dim r As Long, LastRow As Long, Data As Variant, Result As Variant
  LastRow = Sheets("[B][COLOR="#0000FF"]Sheet1[/COLOR][/B]").Cells(Rows.Count, "A").End(xlUp).Row
  Data = Sheets("[B][COLOR="#0000FF"]Sheet1[/COLOR][/B]").Range("A2:D" & LastRow)
  ReDim Result(Data(1, 1) To Data(UBound(Data), 1), 0 To 12)
  For r = 1 To UBound(Data) - LBound(Data) + 1
    If Result(Data(r, 1), 0) = "" Then Result(Data(r, 1), 0) = Data(r, 1)
    Result(Data(r, 1), Data(r, 2)) = Result(Data(r, 1), Data(r, 2)) + Data(r, 4)
  Next
  With Sheets("[B][COLOR="#0000FF"]Rainfall[/COLOR][/B]")
    .Cells.Clear
    .Range("A1:M1") = Array("Year", "Jan", "Feb", "Mar", "Apr", "May", _
                      "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
    .Range("A2").Resize(UBound(Result) - LBound(Result) + 1, 13) = Result
    On Error Resume Next
    .Range("A2").Resize(UBound(Result) - LBound(Result) + 1, 13).SpecialCells(xlBlanks) = 0
    On Error GoTo 0
  End With
End Sub
I forgot to mention that the above code (which I posted earlier), as written, requires that your data be sorted by the Year column (sorting by the other columns is not required for it to work). That seemed like a logical assumption to make; however, if your data is not sorted that way (let's say additional data for an earlier year came in from another source and you just stuck it at the bottom of the list), then a small change to one line of code will make my macro work with non-sorted data...

Code:
Sub YearMonthRainfallToo()
  Dim R As Long, LastRow As Long, Data As Variant, Result As Variant
  LastRow = Sheets("[COLOR="#0000FF"][B]Sheet1[/B][/COLOR]").Cells(Rows.Count, "A").End(xlUp).Row
  Data = Sheets("[COLOR="#0000FF"][B]Sheet1[/B][/COLOR]").Range("A2:D" & LastRow)
  ReDim Result(Application.Min(Sheets("[COLOR="#0000FF"][B]Sheet1[/B][/COLOR]").Columns("A")) To _
        Application.Max(Sheets("[COLOR="#0000FF"][B]Sheet1[/B][/COLOR]").Columns("A")), 0 To 12)
  For R = 1 To UBound(Data) - LBound(Data) + 1
    If Result(Data(R, 1), 0) = "" Then Result(Data(R, 1), 0) = Data(R, 1)
    Result(Data(R, 1), Data(R, 2)) = Result(Data(R, 1), Data(R, 2)) + Data(R, 4)
  Next
  With Sheets("[COLOR="#0000FF"][B]Rainfall[/B][/COLOR]")
    .Cells.Clear
    .Range("A1:M1") = Array("Year", "Jan", "Feb", "Mar", "Apr", "May", _
                      "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
    .Range("A2").Resize(UBound(Result) - LBound(Result) + 1, 13) = Result
    On Error Resume Next
    .Range("A2").Resize(UBound(Result) - LBound(Result) + 1, 13).SpecialCells(xlBlanks) = 0
    On Error GoTo 0
  End With
End Sub

In addition, this code, and the code I posted earlier, also requires that every year be represented with at least one value. That also seemed like a logical assumption to make; however, it that is not the case and some years have no data, then you will need to use this modification to the above code instead...

Code:
Sub YearMonthRainfallToo()
  Dim R As Long, LastRow As Long, Data As Variant, Result As Variant
  LastRow = Sheets("[COLOR="#0000FF"][B]Sheet1[/B][/COLOR]").Cells(Rows.Count, "A").End(xlUp).Row
  Data = Sheets("[COLOR="#0000FF"][B]Sheet1[/B][/COLOR]").Range("A2:D" & LastRow)
  ReDim Result(Application.Min(Sheets("[B][COLOR="#0000FF"]Sheet1[/COLOR][/B]").Columns("A")) To _
        Application.Max(Sheets("[COLOR="#0000FF"][B]Sheet1[/B][/COLOR]").Columns("A")), 0 To 12)
  For R = 1 To UBound(Data) - LBound(Data) + 1
    If Result(Data(R, 1), 0) = "" Then Result(Data(R, 1), 0) = Data(R, 1)
    Result(Data(R, 1), Data(R, 2)) = Result(Data(R, 1), Data(R, 2)) + Data(R, 4)
  Next
  With Sheets("[B][COLOR="#0000FF"]Rainfall[/COLOR][/B]")
    .Cells.Clear
    .Range("A1:M1") = Array("Year", "Jan", "Feb", "Mar", "Apr", "May", _
                      "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
    .Range("A2").Resize(UBound(Result) - LBound(Result) + 1, 13) = Result
    On Error Resume Next
    With .Range("B2").Resize(UBound(Result) - LBound(Result) + 1)
      .Resize(, 13).SpecialCells(xlBlanks) = 0
      .Offset(, -1).SpecialCells(xlBlanks).EntireRow.Delete
    End With
    On Error GoTo 0
  End With
End Sub
 
Last edited:
Upvote 0
So i used your macro and it worked, thanks so much, but i have formulas on the same page, but whenever i bring it in it deletes it.
Monthly DataJanFebMarAprMayJuneJulyAugSepOctNovDec
0-100683172129455780879388695641
101-200401504428453819191721354441
201-3001502519211613136017821
301-40072101418451100568
401-500313511311110023
501-60025647412000001
601-7008130010000003
701-8006222000000000
801-9006131000100000
901-10001010000000000
1001-11001010000000000
1101-12000000000000000
1201-13003201000000000
1301-14000000000000000

<colgroup><col width="64" span="14" style="width:48pt"> </colgroup><tbody>
</tbody>

It deletes this when i bring it in. How do i stop it doing that.
 
Upvote 0
Dim r As Long, LastRow As Long, Data As Variant, Result As Variant
LastRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
Data = Sheets("Sheet1").Range("A2:D" & LastRow)
ReDim Result(Data(1, 1) To Data(UBound(Data), 1), 0 To 12)
For r = 1 To UBound(Data) - LBound(Data) + 1


If Result(Data(r, 1), 0) = "" Then Result(Data(r, 1), 0) = Data(r, 1)
Result(Data(r, 1), Data(r, 2)) = Result(Data(r, 1), Data(r, 2)) + Data(r, 4)

Next
With Sheets("Rainfall")
.Cells.Clear
.Range("A1:M1") = Array("Year", "Jan", "Feb", "Mar", "Apr", "May", _
"Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
.Range("A2").Resize(UBound(Result) - LBound(Result) + 1, 13) = Result
On Error Resume Next
.Range("A2").Resize(UBound(Result) - LBound(Result) + 1, 13).SpecialCells(xlBlanks) = 0
On Error GoTo 0

End With
End Sub

Are you able to tell me what the stuff highlighted in red mean. I want to comment and write this data myself, but i want to know what it means.

Thankyou
 
Upvote 0
Jacko1996,

When you respond to your helper(s), please use their site ID/username/handle.

This will keep thread clutter to a minimum, and, make the discussion easier to follow.

Have you even tried either of my two macro solutions?
 
Upvote 0

Forum statistics

Threads
1,214,421
Messages
6,119,392
Members
448,891
Latest member
tpierce

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