Extracting data from excel table if it is between two given dates and then adding the numbers using VBA code

ashishscores

New Member
Joined
Dec 25, 2019
Messages
1
Office Version
  1. 2010
  2. 2007
Platform
  1. Windows
I have a table in excel having eight columns which includes year, date of deposit and total fees. Now, if date of deposit is between two given dates and it pertains to year 2019-20 then I am trying to extract value from corresponding fee column. I am trying to get total fees deposited between two given dates for a particular year. But i am not getting desired result. Kindly help.
I am using If and else statement and For each loop.

VBA Code:
Sub Macro12()

Dim Cell As Range

Dim Cell2 As Range

For Each Cell In Range("D2:D25")
  
  
' If (Cell.Value >= DateValue("1 / 5 / 2019")) And (Cell.Value <= DateValue("30 / 4 / 2020")) Then
If (Cell.Value >= 43586) And (Cell.Value <= 43951) Then


    For Each Cell2 In Range("C2:C25")
    
        If Cell2.Value = "2020-21" Then

        Range("I3").Value = Cell2.Value
        Range("J3").Value = Range("J3").Value + Cell2.Offset(0, 5).Value
          
          
        Else
    
        Range("I4").Value = "other years"
        Range("J4").Value = Range("J4").Value + Cell2.Offset(0, 5).Value
       
                        
      End If
    Next Cell2
 
' ElseIf (Cell.Value >= DateValue("1 / 5 / 2021")) And (Cell.Value <= DateValue("30 / 4 / 2022")) Then

ElseIf (Cell.Value >= 43599) And (Cell.Value <= 43599) Then


Range("M3").Value = "step 1 clear"

End If

Next Cell

End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi, next time please try prepare a data example using add-in specifically for this, it can be found here XL2BB
Note: If would necessary, pay attention to this post XL2BB 2 Squares

Data example ?
Book1
ABCDEFGH
1Start date01/12/2019Total
2End date30/03/2020
3
4yeardate_deposittotal_feesdate_depositamountcumulative
5201801/10/201850
6201912/06/201923
7201920/12/201958.6
8202013/03/202047
9202008/05/202059
10202006/07/2020894
11201806/08/20182589
12201605/04/201635
13202004/01/202021
14202025/02/2020580
15
Sheet1


And after run code ?
Book1
ABCDEFGH
1Start date01/12/2019Total706.60
2End date30/03/2020
3
4yeardate_deposittotal_feesdate_depositamountcumulative
5201801/10/20185020/12/201958.60 58.6
6201912/06/20192313/03/202047.00 105.6
7201920/12/201958.604/01/202021.00 126.6
8202013/03/20204725/02/2020580.00 706.6
9202008/05/202059
10202006/07/2020894
11201806/08/20182589
12201605/04/201635
13202004/01/202021
14202025/02/2020580
15
Sheet1


Code ?
VBA Code:
Option Explicit
'by Hernan Torres (Mikel ERP)
'December 25, 2019
'Refer to Sheet1
'Question by ashishscores
'topic: https://www.mrexcel.com/board/threads/extracting-data-from-excel-table-if-it-is-between-two-given-dates-and-then-adding-the-numbers-using-vba-code.1118804/

Sub extraer_segun_fecha()
Dim b()
Dim i As Long, j As Long
Dim sDay As Double, eDay As Double
Dim total As Double

Application.ScreenUpdating = False
sDay = CDbl(Sheet3.Range("B1"))
eDay = CDbl(Sheet3.Range("B2"))
b = Sheets("Sheet1").Range("A5:C" & Range("B" & Rows.Count).End(xlUp).Row).Value2

j = 4 'for start write in Range("E5")
For i = 1 To UBound(b)
Debug.Print CDbl(b(i, 2))
If (b(i, 2) >= sDay And b(i, 2) <= eDay) Then
j = j + 1
Cells(j, 5) = b(i, 2)
Cells(j, 6) = b(i, 3)
Cells(j, 7) = total + b(i, 3)
total = total + b(i, 3)
End If
Next i
Application.ScreenUpdating = True
Range("F1").Value = total
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,240
Messages
6,170,951
Members
452,368
Latest member
jayp2104

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