Worksheet Change Event

sachavez

Active Member
Joined
May 22, 2009
Messages
459
Excel 2010

Need some VBA assistance with a worksheet change event.

I need the code to reformat the contents in range G12:G29.

I'm pasting the data in from a main frame screen, which produces a date format of 620 (June 20, 2012). So, I need the code to reformat 620 to 6/20, etc.

Thanks, in advance!

Steve
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Question..

Take the number 111
Is that January 11, or November 1 ??

You'll need some method to differentiate that.
 
Upvote 0
That contradicts your original post, where June (single digit month) 20th is 620, not 0620

Is it actually consistantly a 4 digit value, DDMM

January 1st would be 0101
 
Upvote 0
The mainframe format is MMDD. Today's data is showing as 0620 (I originally left out the zero in my original post; when pasted into Excel, the leading zero doesn't show).

I hope this helps.

Thanks!
 
Upvote 0
In any case, this will work if the data is a consistent 4 digit value MMDD

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range, MyRange As Range
Set MyRange = Intersect(Target, Range("G12:G29"))
If MyRange Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each c In MyRange
    If Len(c) > 0 Then
        c = Left(c, 2) & "/" & Right(c, 2)
    End If
Next c
Application.EnableEvents = True
End Sub
 
Upvote 0
Thank you for the code.

The format is off a bit; it is converting 0620 to 62/20.


Steve

<TBODY>
</TBODY><COLGROUP><COL></COLGROUP>
 
Upvote 0
Then the value in the cell is NOT actually 0620, it's 620 Formatted as 0620

Try changing
c = Left(c, 2) & "/" & Right(c, 2)
to
c = Left(c.Text, 2) & "/" & Right(c.Text, 2)
 
Upvote 0
Ok, that worked. Now, when I past the data in a second time, I the result is 1/ep. this is very strange. Does the code need to format the range as a date?
 
Upvote 0

Forum statistics

Threads
1,203,257
Messages
6,054,408
Members
444,723
Latest member
Gagan sree ram

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