VBA: Preventing numbers automatically changing to dates ?

JumboCactuar

Well-known Member
Joined
Nov 16, 2016
Messages
785
Office Version
  1. 365
Platform
  1. Windows
Hi,
this only happens with numbers that look like a date:

I have data like this:
1 to 9
10 to 12
13 to 15
17 to 19

Now i need it to look like
1 - 9
10 - 12
13 - 15
17 - 19

Problem is with the first two, get converted to01-Sep and 10-Dec

this is what ive tried:
Code:
Range("A2:A1000").NumberFormat = "@"
frm1 = "to"
to1 = "-"
Columns("A").Replace what:=frm1, replacement:=to1, lookat:=xlPart, MatchCase:=False

any help appreciated
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
There might be a more clever VBA way, but this worked for me (still relatively new using VBA):

Code:
Sub FixDate()
ActiveSheet.Select
Dim LR As Long
LR = Cells(Rows.Count, "A").End(xlUp).Row
Range("B2:B" & LR).Formula = "=Left(A2, Find("" "", A2)) & "" - "" & Right(A2, Len(A2) - (Find(""to"", A2) + 2))"
Range("A:A").NumberFormat = ";;;@"
Range("A2:A" & LR) = Range("B2:B" & LR).Value
Range("B:B").EntireColumn.Delete
End Sub
 
Upvote 0
There might be a more clever VBA way, but this worked for me (still relatively new using VBA):

Code:
Sub FixDate()
ActiveSheet.Select
Dim LR As Long
LR = Cells(Rows.Count, "A").End(xlUp).Row
Range("B2:B" & LR).Formula = "=Left(A2, Find("" "", A2)) & "" - "" & Right(A2, Len(A2) - (Find(""to"", A2) + 2))"
Range("A:A").NumberFormat = ";;;@"
Range("A2:A" & LR) = Range("B2:B" & LR).Value
Range("B:B").EntireColumn.Delete
End Sub

Thankyou though this didn't work as expected and formatted some of my numbers as blank

I forgot to mention my original data is also mixed with correctly formatted data so that could be why

Example:
1 to 8
9
10 - 12
13 to 14
15
16-17

I need it to be:
1 - 8
9
10 - 12
13 - 14
15
16 - 17
 
Upvote 0
Another option, rather than using a dash -, you could use an En dash –
Code:
Range("A2:A1000").NumberFormat = "@"
frm1 = "to"
to1 = Chr(150)
Columns("A").Replace what:=frm1, replacement:=to1, lookat:=xlPart, MatchCase:=False
 
Upvote 0
Another option, rather than using a dash -, you could use an En dash –
Code:
Range("A2:A1000").NumberFormat = "@"
frm1 = "to"
to1 = Chr(150)
Columns("A").Replace what:=frm1, replacement:=to1, lookat:=xlPart, MatchCase:=False

Thankyou I will try this
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,436
Members
449,083
Latest member
Ava19

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