简体   繁体   English

如何将文件名复制到单元格,删除扩展名但保留格式

[英]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.我有一个 excel VBA 脚本,用于将一堆报告(大约 100 个)合并到一个工作表中,对于我添加的每个工作表,我希望文件名出现在一列中,但是我需要删除扩展名“.xlsx”我没问题,但是当我删除扩展名时,单元格的格式会发生变化,例如,一个文件可能被命名为 001.xlsx 我删除了 .xlsx 并且 excel 删除了前导 00。 How do I go about remove the filename extension but preserve the cell formatting? go 如何删除文件扩展名但保留单元格格式?

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.在单元格文本的开头加上单引号(')会使 Excel 将单元格内容视为文本,因此“001”在删除“.xlsx”部分后将保持原样。 cell.Value will be equal to the filename without the single quote. cell.Value 将等于没有单引号的文件名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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