Lệnh lấy tên file trong exel và cấu trúc VBA lưu file

Đây là hàm trong exel và hàm VBA giúp bạn xử lý với tên file và lưu file một cách tự động.

Lệnh lấy tên file Exel đang hoạt động:

=MID(CELL("filename"),SEARCH("[",CELL("filename"))+1,
SEARCH("]",CELL("filename"))-SEARCH("[",CELL("filename"))-1)



Lệnh lấy đường dẫn file đang hoạt động:

=LEFT(CELL(“filename”),SEARCH(“[“,CELL(“filename”))-1)

Cấu trúc lệnh lưu file theo ô định sẵn:
VD: Tên file là : A1+B1+C1.xlsx

Sub Luu()
        ten = Application.Range("A1").Value & Range("B1").Value & Range("C1").Value
        tenfile = "D:\" & ten & ".xlsx"
        ActiveWorkbook.SaveAs Filename:= _
        tenfile, FileFormat:=xlOpenXMLWorkbook, _
        Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
        CreateBackup:=False
End Sub

Post Author: Anh bán hàng