Excel VBA for Data Handling

KSKWin

New Member
Joined
May 7, 2023
Messages
11
Office Version
  1. 2019
Platform
  1. Windows
I have a excel with Col A is Date, Col B is Amount and Col C is Remark

Date column will have repeated date entry for different amount and different remarks.
Date column will not be Sequential fashion, Dates will be skipped.

I want a VBA code to achieve,

1) Sequential Date should be shown with Amount Zero and Blank Remark shown as comment in Amount Column, if there is no Date entry present in Col A
2) If multiple same date entries are present, then sum all the amount for the specific date and list the Remark for each amount against the same date as Excel Comment in Amount Col B

Attached the sample excel

Test Samples.xlsx
ABCDEFG
1
2This is Actual Data ColumnThis is I need
3
4DateAmountRemarkDateAmount
503-01-20249275.00Apple01-01-20240
605-01-20248673.00Mango02-01-20240
708-01-2024123.00Pine03-01-20249275
813-01-2024500.00Grape04-01-20240
924-01-2024195.00Jack05-01-20248673
1025-01-20245664.00Grape06-01-20240
1125-01-202412.00Grape07-01-20240
1226-01-2024124.00Grape08-01-2024123
1326-01-20241422.00Mango09-01-20240
1428-01-2024147.00Grape10-01-20240
1528-01-2024854.00Apple11-01-20240
1629-01-2024125.00Grape12-01-20240
1729-01-202486.00Pine13-01-2024500
1830-01-202421Pine14-01-20240
1930-01-202458Apple15-01-20240
2016-01-20240
2117-01-20240
2218-01-20240
2319-01-20240
2420-01-20240
2521-01-20240
2622-01-20240
2723-01-20240
2824-01-2024195
2925-01-20245676
3026-01-20241564
3127-01-20240
3228-01-20241001
3329-01-2024211
3430-01-202479
35
36
Sheet1
 

Attachments

  • Sample excel.jpg
    Sample excel.jpg
    91.6 KB · Views: 1
Last edited:

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Hello @KSKWin.
Try next code, perhaps I didn't understand the problem correctly.
VBA Code:
Option Explicit

Sub FillDatesAndPrices()
    Dim i           As Long
    Dim ws          As Worksheet
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    Application.ScreenUpdating = False

    With ws
        .Range("E4:F" & ws.Rows.Count).ClearContents
        .Range("E4").Value = "Date"
        .Range("F4").Value = "Amount"

        Dim priceDict As Object
        Set priceDict = CreateObject("Scripting.Dictionary")

        Dim lastRow As Long
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

        For i = 5 To lastRow

            If IsDate(.Cells(i, 1).Value) Then
                priceDict(Int(.Cells(i, 1).Value)) = .Cells(i, 2).Value
            End If

        Next i

        Dim yearFromCell As Integer
        yearFromCell = Year(.Cells(5, 1).Value)

        Dim dateToFind As Date
        dateToFind = DateSerial(yearFromCell, Month(Date), 1)
        i = 5

        Do While dateToFind <= DateSerial(yearFromCell, Month(Date) + 1, 0)
            .Cells(i, 5).Value = dateToFind

            If priceDict.Exists(Int(dateToFind)) Then
                .Cells(i, 6).Value = priceDict(Int(dateToFind))
            End If

            dateToFind = dateToFind + 1
            i = i + 1
        Loop

    End With

    Application.ScreenUpdating = True
End Sub
Good luck.
 
Upvote 0

Forum statistics

Threads
1,225,902
Messages
6,187,732
Members
453,436
Latest member
MEZHH

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