[英]How to Add Specific Cell in Excel document using VBA
This is my scenario, I have a path that contains all excel files. 这是我的情况,我有一个包含所有Excel文件的路径。 Now, I want to implement a solution that will insert the current "Filename" in specific cell for that file.
现在,我想实现一个解决方案,将当前的“文件名”插入该文件的特定单元格中。
For Example: 例如:
sample.xls
Sample2.xls
Sample3.xls
Inside that sample.xls, I want to inset the filename "sample" without the extension on cell "E7" and "E8" then save this file. 在该sample.xls中,我要插入文件名“ sample”,但在单元格“ E7”和“ E8”上不具有扩展名,然后保存该文件。
I currently have this formula to get the filename without the extension: 我目前有这个公式来获取不带扩展名的文件名:
=MID(CELL("filename",A1),SEARCH("[",CELL("filename",A1))+1,SEARCH(".",CELL("filename",A1))-1-SEARCH("[",CELL("filename",A1)))
Here are a few codes that might help you on the way: 以下是一些可能对您有所帮助的代码:
For the last row in a workbook: 对于工作簿的最后一行:
LastRow = ActiveWorkbook.ActiveSheet.Cells(ActiveWorkbook.ActiveSheet.Rows.Count, "a").End(xlUp).Row
ActiveWorkbook can be replaced by "Workbooks(NameofWorkbook)". ActiveWorkbook可以替换为“ Workbooks(NameofWorkbook)”。
ActiveWorksheet can be replaced by "Sheets(NameofSheet)". ActiveWorksheet可以替换为“ Sheets(NameofSheet)”。
For adding the paths of the excel files to column a: 要将excel文件的路径添加到列a:
Sub LoopThroughFiles()
Dim MyObj As Object, MySource As Object, file As Variant
file = Dir("c:\testfolder\")
While (file <> "")
ActiveWorkbook.ActiveWorksheet.Cells((LastRow+1), 1).Value = file
Wend
file = Dir
End Sub
Not too sure this works, you'll have to test it. 不太确定是否可行,您必须对其进行测试。
****EDIT_1**** ****编辑_1 ****
For the file extensions the following might be useful (found on another question): 对于文件扩展名,以下内容可能有用(在另一个问题上找到):
FileExtStr = "." & LCase(Right(wb.Name, Len(wb.Name) - InStrRev(wb.Name, ".", , 1)))
wb.name can later be replaced by the cells holding the path (by WB.WS.Cells(x, y).Value) wb.name以后可以用保存路径的单元格替换(通过WB.WS.Cells(x,y).Value)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.