VBA Code to auto date cells

data808

Active Member
Joined
Dec 3, 2010
Messages
353
Office Version
  1. 2019
Platform
  1. Windows
I have a merged C2 and D2 cell where I will type in the month "January" for example and another cell G2 that I will type in the year 2022. I will do this for every month. Is there a way based on which month and year I type into these cells, to auto populate 1/1/22 - 1/31/22 from cells A4 - A34? I realize that not every month has 31 days and so if these happens for February for example and goes to 2/31/22, that is ok. I am just trying to create a template and we will only be using the dates that we need to and ignore the dates and don't exist. Thanks.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Vba never likes using Merged Cells.
Why not just enter January in just one range like C2?
 
Upvote 0
Vba never likes using Merged Cells.
Why not just enter January in just one range like C2?
Sorry had to do it for cosmetics so the spacing looks better. I tried VBA just to get started but its not working:

Sub auto_date_cells()

If Range("C2:D2").Value = "1" Then
Range("A4").Value = "01/01/22"
End If


End Sub


I still haven't added the year G1 cell into the code yet but just wanted to see if this would work but it does nothing. Do you know why? I added the VBA to sheet1 and then I tried it in a module with no luck.
 
Upvote 0
Sorry had to do it for cosmetics so the spacing looks better. I tried VBA just to get started but its not working:

Sub auto_date_cells()

If Range("C2:D2").Value = "1" Then
Range("A4").Value = "01/01/22"
End If


End Sub


I still haven't added the year G1 cell into the code yet but just wanted to see if this would work but it does nothing. Do you know why? I added the VBA to sheet1 and then I tried it in a module with no luck.
I know users do this sometimes for cosmetic reasons, but Vba does not like this.

If you just enter 1 then why do you need two ranges.
 
Upvote 0
I know users do this sometimes for cosmetic reasons, but Vba does not like this.

If you just enter 1 then why do you need two ranges.
This was a test VBA. If you look at my original post I will be typing in the whole month name. I just wanted to see if I could get the VBA to somewhat work.
 
Upvote 0
This was a test VBA. If you look at my original post I will be typing in the whole month name. I just wanted to see if I could get the VBA to somewhat work.
You may get another answer from another poster here with an answer. But from my experience Vba does not like Merged Cells
 
Upvote 0
You may get another answer from another poster here with an answer. But from my experience Vba does not like Merged Cells
Thanks for the replies. I've definitely gotten VBA to work with merged cells so I know its possible. Just not sure why I can't get even my test VBA to work. I think you could even reference the merged cell with the starting from the first one so the code would look like this:

Sub auto_date_cells()

If Range("C2").Value = "1" Then
Range("A4").Value = "01/01/22"
End If


End Sub

I just basically got rid of the D2 so it went from "C2:D2" to just "C2". I have gotten similar codes to work like this before. Just don't know why this isn't working. Again thanks for trying to help. Hopefully someone on here knows how to get this done.
 
Upvote 0
Thanks for the replies. I've definitely gotten VBA to work with merged cells so I know its possible. Just not sure why I can't get even my test VBA to work. I think you could even reference the merged cell with the starting from the first one so the code would look like this:

Sub auto_date_cells()

If Range("C2").Value = "1" Then
Range("A4").Value = "01/01/22"
End If


End Sub

I just basically got rid of the D2 so it went from "C2:D2" to just "C2". I have gotten similar codes to work like this before. Just don't know why this isn't working. Again thanks for trying to help. Hopefully someone on here knows how to get this done.
Yes I was going to suggest formatting the two cells as Merged and then Center Text
But then we would use Just C2 for example in the script.
 
Upvote 0
Yes I was going to suggest formatting the two cells as Merged and then Center Text
But then we would use Just C2 for example in the script.
I just tried a similar code on a brand new spreadsheet with no merged cells and still nothing. Here is the code I tried:

Sub test()

If Range("A1").Value = 1 Then
Range("B1").Value = 5
End If

End Sub

Any ideas why this isn't working? So I type "1" into A1 cell and nothing happens with B1 cell.
 
Upvote 0
Any ideas why this isn't working? So I type "1" into A1 cell and nothing happens with B1 cell.
If you want it to work as you type then you need to put it to be a Worksheet_Change event/sub not a regular Sub
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,543
Members
449,089
Latest member
davidcom

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