Extract last amounts in column within two dates

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
650
Office Version
  1. 2019
Hello,
I want to extract amounts is existed in column F within two dates
ورقة عمل Microsoft Excel جديد ‫‬.xlsx
ABCDEFGHIJK
1DATEINFODEBITCREDITBALANCENOTEITEMFROM DATETO DATEAMOUNT
201/11/2024SALES INVOICE NO BSJ200020002000
302/11/2024SALES INVOICE NO BSJ200130005000
403/11/2024SALES RETURNS INVOICE NO BSJ78930002000
503/11/2024VOUCHER NO VBGH67881500500
604/11/2024SALES INVOICE NO BSJ20031500015500
704/11/2024SALES INVOICE NO BSJ200484852398528,485.000
805/11/2024SALES INVOICE NO BSJ20051200035985
906/11/2024SALES INVOICE NO BSJ2006120004798524,000.000
ACOOUNT
Cell Formulas
RangeFormula
E2E2=C2-D2
E3,E5:E9E3=E2+C3-D3
E4E4=E3+CC104-D4



result
ورقة عمل Microsoft Excel جديد ‫‬.xlsx
ABCDEFGHIJK
1DATEINFODEBITCREDITBALANCENOTEITEMFROM DATETO DATEAMOUNT
201/11/2024SALES INVOICE NO BSJ200020002000101/11/202404/11/202428,485.000
302/11/2024SALES INVOICE NO BSJ200130005000205/11/202406/11/202424,000.000
403/11/2024SALES RETURNS INVOICE NO BSJ78930002000TOTAL52,485.000
503/11/2024VOUCHER NO VBGH67881500500
604/11/2024SALES INVOICE NO BSJ20031500015500
704/11/2024SALES INVOICE NO BSJ200484852398528,485.000
805/11/2024SALES INVOICE NO BSJ20051200035985
906/11/2024SALES INVOICE NO BSJ2006120004798524,000.000
ACOOUNT
Cell Formulas
RangeFormula
K4K4=SUM(K2:K3)
E2E2=C2-D2
E3,E5:E9E3=E2+C3-D3
E4E4=E3+CC104-D4

so should copy every amount is existed in column F with two dates and insert TOTAL row to sum amount column .and should delete data before brings report in range H:K
thanks
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Give this a try with a copy of your worksheet.

VBA Code:
Sub Extract_Amounts()
  Dim a As Variant, b As Variant
  Dim k As Long, i As Long
  
  Intersect(ActiveSheet.UsedRange.EntireRow, Columns("H:K")).Offset(1).Clear
  a = Range("A1").CurrentRegion.Value
  ReDim b(1 To UBound(a), 1 To 4)
  For i = 2 To UBound(a)
    If Len(a(i - 1, 6)) > 0 Then
      k = k + 1
      b(k, 1) = k: b(k, 2) = a(i, 1)
    End If
    If Len(a(i, 6)) > 0 Then
      b(k, 3) = a(i, 1): b(k, 4) = a(i, 6)
    End If
  Next i
  With Range("H2:K2")
    .Resize(k).Value = b
    With .Offset(k)
      .FormulaR1C1 = Array("TOTAL", "", "", "=sum(R2C:R[-1]C)")
      .Cells(1).Interior.Color = vbYellow
      .Cells(1).Font.Bold = True
    End With
  End With
End Sub
 
Upvote 0
Great!
just I need fixing the formatting .
I would add borders around cells and show number format like this "#,##0.00" in range H:K also show data in the center.
 
Upvote 0
just I need fixing the formatting .
Format H1:K1 manually as this does not get changed by the code then try this version

VBA Code:
Sub Extract_Amounts_v2()
  Dim a As Variant, b As Variant
  Dim k As Long, i As Long
  
  Intersect(ActiveSheet.UsedRange.EntireRow, Columns("H:K")).Offset(1).Clear
  a = Range("A1").CurrentRegion.Value
  ReDim b(1 To UBound(a), 1 To 4)
  For i = 2 To UBound(a)
    If Len(a(i - 1, 6)) > 0 Then
      k = k + 1
      b(k, 1) = k: b(k, 2) = a(i, 1)
    End If
    If Len(a(i, 6)) > 0 Then
      b(k, 3) = a(i, 1): b(k, 4) = a(i, 6)
    End If
  Next i
  With Range("H2:K2").Resize(k + 1)
    .Value = b
    .BorderAround xlContinuous
    .Borders(xlInsideVertical).LineStyle = xlContinuous
    .Borders(xlInsideHorizontal).LineStyle = xlContinuous
    With .Rows(k + 1)
      .FormulaR1C1 = Array("TOTAL", "", "", "=sum(R2C:R[-1]C)")
      .Cells(1).Interior.Color = vbYellow
      .Cells(1).Font.Bold = True
    End With
    .Columns(4).NumberFormat = "#,##0.00"
  End With
End Sub
 
Upvote 0
when I put the data in center manually the code will move it for the right !
Sorry, I missed that point you mentioned in post 3. :oops:

Rich (BB code):
Sub Extract_Amounts_v3()
  Dim a As Variant, b As Variant
  Dim k As Long, i As Long
  
  Intersect(ActiveSheet.UsedRange.EntireRow, Columns("H:K")).Offset(1).Clear
  a = Range("A1").CurrentRegion.Value
  ReDim b(1 To UBound(a), 1 To 4)
  For i = 2 To UBound(a)
    If Len(a(i - 1, 6)) > 0 Then
      k = k + 1
      b(k, 1) = k: b(k, 2) = a(i, 1)
    End If
    If Len(a(i, 6)) > 0 Then
      b(k, 3) = a(i, 1): b(k, 4) = a(i, 6)
    End If
  Next i
  With Range("H2:K2").Resize(k + 1)
    .Value = b
    .BorderAround xlContinuous
    .Borders(xlInsideVertical).LineStyle = xlContinuous
    .Borders(xlInsideHorizontal).LineStyle = xlContinuous
    .HorizontalAlignment = xlCenter
    With .Rows(k + 1)
      .FormulaR1C1 = Array("TOTAL", "", "", "=sum(R2C:R[-1]C)")
      .Cells(1).Interior.Color = vbYellow
      .Cells(1).Font.Bold = True
    End With
    .Columns(4).NumberFormat = "#,##0.00"
  End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,753
Messages
6,180,748
Members
452,996
Latest member
nelsonsix66

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