Excel VBA function 'REPLACE' turns text into a Date.

I have a column of filenames that I want to remove the file extension from so that I can compare them to another list.
Certain filenames appear like dates when the file extension is removed.
The 'Replace' function literally converts those names into dates.

VBA syntax:
    With Columns(TargetCol)     'Remove filename extensions
        .Replace What:=".pdf", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
    End With

Example filenames that convert to a date
     11-6285.pdf    results in   Nov-85    instead of   11-6285
     12-9958.pdf    results in   Dec-85    instead of   12-9958

Is there a way to remove the file extension without changing the remaining text to a Date datatype?
It doesn't matter if the column is formatted to 'Text' or not.
LVL 1
Jeff GeiselmanIT Tech AssocAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David SankovskySenior SysAdminCommented:
If the extensions are ALWAYS PDF, you can use the a simple inline command instead of going VBA script on it.
For example "=LEFT(A2,FIND(".pdf",A2,1)-1)"
Also, if it may have several types, but the filename certainly has no dots in it, you can simple search for "." as in "=LEFT(A2,FIND(".",A2,1)-1)"

See attached file for example.
Rgonzo1971Commented:
Hi,

pls try
With Columns("A:A")
    Set c = .Find(".pdf", LookAt:=xlPart, SearchOrder:=xlByRows)
    If Not c Is Nothing Then
        firstAddress = c.Address
        Do
            c.NumberFormat = "@"
            c.Value2 = Left(c.Value, Len(c.Value) - 4)
            Set c = .FindNext(c)
        Loop While Not c Is Nothing And c.Address <> firstAddress
    End If
End With

Open in new window

Regards

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jeff GeiselmanIT Tech AssocAuthor Commented:
Thanks, this should work.
It at least retains the correct format & datatype.

One minor issue:
'Loop While Not c Is Nothing And c.Address <> firstAddress'
causes the error 'Object variable or With Block Variable not set'    when 'c. is Nothing'.
May just need to trap for 'firstAddress' without referencing a null 'c'.
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

slubekCommented:
When you use General format, Excel tries to recognize cell values . Thats why it changes result of Replace into date.
Change your column format to String (Text) to prevent this.
Or maybe you should disable ""Extend data range formats and formulas." option (File>Options>Advanced)
Rgonzo1971Commented:
then try

With Columns("A:A")
    Set c = .Find(".pdf", LookAt:=xlPart)
    If Not c Is Nothing Then
        firstAddress = c.Address
        Do
            c.NumberFormat = "@"
            c.Value2 = Left(c.Value, Len(c.Value) - 4)
            Set c = .FindNext(c)
            On Error Resume Next
            bAddress = c.Address <> firstAddress
            On Error GoTo 0
        Loop While Not c Is Nothing And bAddress
    End If
End With

Open in new window

Regards
Jeff GeiselmanIT Tech AssocAuthor Commented:
slubek,,
In my Excel, it doesn't matter whether the column is formatted ahead of time to String(Text) or not, when the VB 'Replace' function executes, it actually changes the format to 'Date'.
I have even tried the TextToColumn 'Text' datatype with the same results.
aikimarkCommented:
I think you're going to need to a new column for a formula that removes the file extension.
=SUBSTITUTE(A1,".pdf","")

Open in new window

slubekCommented:
Sorry - my fault.
Replace works properly when your cell content begins with ', for example:
'11-6285.pdf
'12-9958.pdf
etc.
Add a column with concatenation formula, like: ="" & A1, copy it and paste "special as values". You will get column with values beginning with apostrophes. You can use your Replace on that column.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.