Date in multiple formats

asdsparky

Board Regular
Joined
Oct 13, 2017
Messages
60
I have the following code in my workbook to populate the date in certain cells when the cells are selected. The formula works to populate both ranges. The issue I am running into is formatting the date differently for the two ranges. I need the first range (cell "E4") to format as "mmmm yyyy" which I have done and it works. The other range "G13:G400" needs to format as "mm/dd/yy". Any ideas? I have tried running each range as a separate string, but no luck.
Code:
With ActiveCell
            If Intersect(range("E4", "G13:G400"), .Cells) Is Nothing Then Exit Sub
           .Value = Date
           .NumberFormat = ("mmmm yyyy")
 End With
 
That did eliminate filling every cell in the selected row with the date. It still enters the date in the "F#" cell when the row is selected, rather than only when that particular cell is selected. I tried modifying slightly to this:
Code:
    Dim rng1 As Range
    Dim rng2 As Range
    Dim cell As Range
    
    Set rng1 = Intersect(Target, Range("F2"))
    If Not rng1 Is Nothing Then
        For Each cell In rng1
            With Activecell
                .Value = Date
                .NumberFormat = "mmmm yyyy"
            End With
        Next cell
    End If
    
    Set rng2 = Intersect(Target, Range("F9:F400"))
    If Not rng2 Is Nothing Then
        For Each cell In rng2
            With Activecell
                .Value = Date
                .NumberFormat = "mm/dd/yyyy"
            End With
        Next cell
    End If
It prevents the "F" cell from getting the date when I select the row or column but now it changes the value of the first cell in the selected row or column to the date.
 
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Then let's update the code to bail out if more than one cell is selected (i.e. selecting a whole row or column).
Because of that, we should no longer need to loop through the intersection (since it can only be one cell now).
Code:
    Dim rng1 As Range
    Dim rng2 As Range
    
[COLOR=#ff0000]    If Target.Count > 1 Then Exit Sub[/COLOR]

    Set rng1 = Intersect(Target, Range("F2"))
    If Not rng1 Is Nothing Then
        With Target
            .Value = Date
            .NumberFormat = "mmmm yyyy"
        End With
    End If
    
    Set rng2 = Intersect(Target, Range("F9:F400"))
    If Not rng2 Is Nothing Then
        With Target
            .Value = Date
            .NumberFormat = "mm/dd/yyyy"
        End With
    End If

Note: Try to get in the habit of using "Target" instead of "ActiveCell" in Worksheet_SelectionChange and Worksheet_Change event procedures!
"Target" is the cell that triggered the code to run, and doesn't change within the procedure.
"ActiveCell" could actually change within the procedure, depending on what your code does.
Well-written code will seldom ever use "ActiveCell".
 
Last edited:
Upvote 0
Good call. I thought I had another issue because this update took me back to square one, where the date would only work in the "F9:F400" range. I played with it until I found the culprit, which leads me to think that maybe my very first code would have worked had I noticed this in the first place (I will be checking that one). By limiting the target count to 1 I inadvertently canceled out the functionality for the "F2" range because "F2" is merged with "E2", causing the target count to exceed 1. I changed the code as follows and it seems to be working now:
Code:
    Dim rng1 As range
    Dim rng2 As range
    
    If Target.Count < 3 Then
    Set rng1 = Intersect(Target, range("E2"))
    If Not rng1 Is Nothing Then
        With Target
            .Value = Date
            .NumberFormat = "mmmm yyyy"
        End With
    End If
    
    Set rng2 = Intersect(Target, range("F9:F400"))
    If Not rng2 Is Nothing Then
        With Target
            .Value = Date
            .NumberFormat = "mm/dd"
        End With
    End If
    End If

Thanks so much for your help with this.
 
Upvote 0
I further modified it because now if I accidentally grab 2 cells, the date goes into both in the "F9:F400" range. I corrected this as shown below. I think we now have a final solution:beerchug:
Code:
    Dim rng1 As range
    Dim rng2 As range
    
    If Target.Count < 3 Then
    Set rng1 = Intersect(Target, range("E2"))
    If Not rng1 Is Nothing Then
        With Target
            .Value = Date
            .NumberFormat = "mmmm yyyy"
        End With
    End If
    End If
    
    If Target.Count = 1 Then
    Set rng2 = Intersect(Target, range("F9:F400"))
    If Not rng2 Is Nothing Then
        With Target
            .Value = Date
            .NumberFormat = "mm/dd"
        End With
    End If
    End If
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,198
Members
453,022
Latest member
RobertV1609

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