Autofill Month

alsharif

New Member
Joined
Mar 14, 2021
Messages
14
Office Version
  1. 2019
Platform
  1. Windows
Hello Every one,


please help me with the proper VBA code for this:

My work sheet has two Columns A and B

I need to Auto fill Dates monthly starting from the last date in column B till the end of the last filled Cell in Column A.

the last cell on both columns needs to be dynamic since the data is increasing every day in the sheet .


Thank you In Advance
 

Attachments

  • Capture.PNG
    Capture.PNG
    97 KB · Views: 18

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hello Please try this. If works for you or not, please provide the feedback.

I have assumed one row in column b will have one date according to which other rows will be auto filled

VBA Code:
Sub date_month()

Dim lastrow_a As Byte
Dim lastrow_b As Integer
Dim b_day As Byte
Dim b_month As Byte
Dim b_year As Integer
Dim i As Integer

lastrow_a = Range("a1").End(xlDown).Row
lastrow_b = Range("b1").End(xlDown).Row

b_day = VBA.Day(Range("b" & lastrow_b))
b_month = VBA.Month(Range("b" & lastrow_b))
b_year = VBA.Year(Range("b" & lastrow_b))


For i = (lastrow_b + 1) To lastrow_a
    If b_month < 12 Then
            Range("b" & i).Value = VBA.Conversion.CDate(b_day & "-" & b_month + 1 & "-" & b_year)
            b_month = b_month + 1
    Else
            b_month = 1
            b_year = b_year + 1
            Range("b" & i).Value = VBA.Conversion.CDate(b_day & "-" & b_month & "-" & b_year)
    End If
    Next i



End Sub
 
Upvote 0
Hello Please try this. If works for you or not, please provide the feedback.

I have assumed one row in column b will have one date according to which other rows will be auto filled

VBA Code:
Sub date_month()

Dim lastrow_a As Byte
Dim lastrow_b As Integer
Dim b_day As Byte
Dim b_month As Byte
Dim b_year As Integer
Dim i As Integer

lastrow_a = Range("a1").End(xlDown).Row
lastrow_b = Range("b1").End(xlDown).Row

b_day = VBA.Day(Range("b" & lastrow_b))
b_month = VBA.Month(Range("b" & lastrow_b))
b_year = VBA.Year(Range("b" & lastrow_b))


For i = (lastrow_b + 1) To lastrow_a
    If b_month < 12 Then
            Range("b" & i).Value = VBA.Conversion.CDate(b_day & "-" & b_month + 1 & "-" & b_year)
            b_month = b_month + 1
    Else
            b_month = 1
            b_year = b_year + 1
            Range("b" & i).Value = VBA.Conversion.CDate(b_day & "-" & b_month & "-" & b_year)
    End If
    Next i



End Sub
thank you sir for your reply

the result i got form your code is so close but it fill the dates daily not monthly check the pictures
 

Attachments

  • the result i got.png
    the result i got.png
    53 KB · Views: 28
  • the result i need.png
    the result i need.png
    53.6 KB · Views: 20
Upvote 0
thank you sir for your reply

the result i got form your code is so close but it fill the dates daily not monthly check the pictures
The result you are getting from my code is actually your answer but there is some formatting problem, because in you are getting the dates as mm/dd/yyyy
 
Upvote 0
Hello please check once

VBA Code:
Sub date_month()

Dim lastrow_a As Byte
Dim lastrow_b As Integer
Dim b_day As Byte
Dim b_month As Byte
Dim b_year As Integer
Dim i As Integer

lastrow_a = Range("a1").End(xlDown).Row
lastrow_b = Range("b1").End(xlUp).Row

Range("b1:b" & lastrow_b) = VBA.Format(Range("b1:b" & lastrow_b), ddmmyyyy)
b_day = VBA.Day(Range("b" & lastrow_b))
b_month = VBA.Month(Range("b" & lastrow_b))
b_year = VBA.Year(Range("b" & lastrow_b))


For i = (lastrow_b + 1) To lastrow_a
    If b_month < 12 Then
            Range("b" & i).Value = VBA.Conversion.CDate(b_day & "-" & b_month + 1 & "-" & b_year)
            b_month = b_month + 1
    Else
            b_month = 1
            b_year = b_year + 1
            Range("b" & i).Value = VBA.Conversion.CDate(b_day & "-" & b_month & "-" & b_year)
    End If
    Next i



End Sub
 
Upvote 0
Solution
Wooow it really works fantastically.

thank you very much for your time and your effort.

you are the best :)
 
Upvote 0
Wooow it really works fantastically.

thank you very much for your time and your effort.

you are the best :)
Happy to help. Thank you for your feedback. Please mark the code which works for you as the answer. So that it help others too.
 
Upvote 0
Brother now while im working on the sheet using the code i found that the code affects the enitre B column from the start till the end, but i really need the code to ignore the date before the last date and do not change them , i mean that i need just to Autofill the the column only considering the last value and ignoring the values before because some times the sheet contains old dates before the last date and i do not want to change them, hope you get my point
thank you
 
Upvote 0
Brother now while im working on the sheet using the code i found that the code affects the enitre B column from the start till the end, but i really need the code to ignore the date before the last date and do not change them , i mean that i need just to Autofill the the column only considering the last value and ignoring the values before because some times the sheet contains old dates before the last date and i do not want to change them, hope you get my point
thank you
Is the solution working for you or you need the modification.

But actually as you require months to be increased. So in b column all the cells are behaving the same way right ?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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