简体   繁体   中英

How to copy file name to a cell, remove extension but preserve formatting

I have an excel VBA script that I am using to consolidate a bunch of reports (roughly 100) into a single worksheet, for each worksheet that I am adding I would like the file name to appear in a column, however I need to remove the extension ".xlsx" which I can do no problem however when I remove the extension the formatting of the cell changes, for example a file might be named 001.xlsx I remove the.xlsx and excel drops the leading 00's. How do I go about remove the filename extension but preserve the cell formatting?

Here is the code that I am using to add the filenames:

            rngFile.Value = wbkSrc.Name
            rngFile.Replace What:=".xlsx", Replacement:="", LookAt:=xlPart

Any help is greatly appreciated, thank you.

Adding a single quote (') in the beginning of the cell text will make Excel treat the cell content as text, so "001" will remain as it is after removing the ".xlsx" part. cell.Value will be equal to the filename without the single quote.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM