跳到主要內容

如何用單元格值保存Excel文件?

使用工作簿時,您是否曾考慮過根據單元格內容保存Excel文件? 例如,您在單元格A1中具有“銷售價格”,並且您想要將工作簿保存為名稱“銷售價格”。 當然,您可以在“另存為”對話框中鍵入名稱,然後將其保存。 但這將是您工作量低下的一種方法。 今天,我將討論解決它的快速技巧。

使用VBA代碼以特定的單元格值保存Excel文件


箭頭藍色右氣泡 使用VBA代碼以特定的單元格值保存Excel文件

以下VBA代碼可以幫助您保存具有指定單元格值的工作簿,請執行以下操作:

1。 按住 ALT + F11 鍵打開 Microsoft Visual Basic for Applications窗口.

2。 點擊 插入 > 模塊,然後將以下代碼粘貼到 模塊窗口.

VBA代碼:使用特定的單元格值保存Excel文件

Private Sub filename_cellvalue()
'Update 20141112
Dim Path As String
Dim filename As String
Path = "C:\Users\dt\Desktop\my information\"
filename = Range("A1")
ActiveWorkbook.SaveAs filename:=Path & filename & ".xls", FileFormat:=xlNormal
End Sub

3。 然後按 F5 鍵執行代碼,您的工作簿已被命名為單元格A1的單元格內容。 看截圖:

doc儲存單元格值1

筆記:

1.您可以通過將A1修改為上述代碼中的其他單元格引用來指定任何單元格值作為文件名。

2.您的活動工作簿將保存到以下文件夾中 C:\ Users \ dt \ Desktop \ my information \ (將位置粘貼到該位置時,必須保留最後一個斜杠),您可以根據需要更改路徑。


相關文章:

如何在Excel中使用日期和時間保存文件?

如何在Excel的標題欄或工具欄中顯示文件路徑?

最佳辦公生產力工具

🤖 Kutools 人工智慧助手:基於以下內容徹底改變數據分析: 智慧執行   |  生成代碼  |  建立自訂公式  |  分析數據並產生圖表  |  呼叫 Kutools 函數...
熱門特色: 尋找、突出顯示或識別重複項   |  刪除空白行   |  合併列或儲存格而不遺失數據   |   沒有公式的回合 ...
超級查詢: 多條件VLookup    多值VLookup  |   跨多個工作表的 VLookup   |   模糊查詢 ....
高級下拉列表: 快速建立下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
欄目經理: 新增特定數量的列  |  移動列  |  切換隱藏列的可見性狀態  |  比較範圍和列 ...
特色功能: 網格焦點   |  設計圖   |   大方程式酒吧    工作簿和工作表管理器   |  資源庫 (自動文字)   |  日期選擇器   |  合併工作表   |  加密/解密單元格    按清單發送電子郵件   |  超級濾鏡   |   特殊過濾器 (過濾粗體/斜體/刪除線...)...
前 15 個工具集12 文本 工具 (添加文本, 刪除字符,...)   |   50+ 圖表 類型 (甘特圖,...)   |   40+ 實用 公式 (根據生日計算年齡,...)   |   19 插入 工具 (插入二維碼, 從路徑插入圖片,...)   |   12 轉化 工具 (數字到單詞, 貨幣兌換,...)   |   7 合併與拆分 工具 (高級合併行, 分裂細胞,...)   |   ... 和更多

使用 Kutools for Excel 增強您的 Excel 技能,體驗前所未有的效率。 Kutools for Excel 提供了 300 多種進階功能來提高生產力並節省時間。  點擊此處獲取您最需要的功能...

產品描述


Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!
Comments (33)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Zdravím,

mám jednu prosbu ... došel jsem do bodu, kdy mám nastavené ukládání souboru excel u s názvem podle dané buňky, ale nedaří se mi pomocí makra aktualizaci, aby se to uložilo s aktuálním názvem.
Asi dělaám něco při nahrávání makra.
Musím tedy spustit ALT+F11 a následně F5.
Může mi někdo pomoci?
This comment was minimized by the moderator on the site
Hello, Petr,
If you want to auto save the workbook with the changed cell value, please apply the below code:
First, you should right click the sheet name contains the cell value you want to rename based on, and then click View Code to open the Microsoft Visual Basic for Applications window, then copy and paste the below code into the blank sheet code. see screenshot:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    Dim Path As String
    Dim filename As String
    
    On Error Resume Next
    If Target.Cells.Count > 1 Then Exit Sub
    If Target.Address = "$A$1" Then
        Application.DisplayAlerts = False
        Path = "C:\Users\AddinsVM001\Desktop\Folder-1\"
        filename = Target
        ActiveWorkbook.SaveAs filename:=Path & filename & ".xlsx"
        Application.DisplayAlerts = True
    End If
End Sub

https://www.extendoffice.com/images/stories/comments/comment-skyyang/save-file-with-cell-value.png
After pasting the code, when you change the A1 cell value, a new excel file with this name will be saved into the specific location.

Note: please change the cell value and file path to oyur need.

Please try it, if you have any other problem, please comment here.
This comment was minimized by the moderator on the site
Hi

I want the file "Offerte JCDecaux_" to be automaticly re-named when saving.
This by combining the file name & the result of formula (=TEXT(O9; "aammjj" ) & "." & O10 & "_" & A5) in cell O11 of worksheet OFFERTE.
The end result would for exemple need to be "Offerte JCDecaux_2205004.1_LEENTJE".

How do I do that?
This comment was minimized by the moderator on the site
Hi 
its very helpful for me 
i would like take the filename from an other workbook can anyone show me how to do this 
This comment was minimized by the moderator on the site
I have updated this to work with Excel 2019 and to create a PDF:

Sub FileNameAsCellContent()

Dim userPath As String
Dim FileName1 As String
Dim FileName2 As String

userPath = Environ("UserProfile")

'Edit the save location here with first folder location after C:\Users\[user]\ or leave to save to Desktop
Const LOCATION As String = "\Desktop\"

'Select which cells to use in filename here
Application.DisplayAlerts = False
FileName1 = Range("F5")
FileName2 = Range("B6")

With ActiveSheet

'Edit filename format here or keep as default (default text - text.pdf)
PDFFullName = userPath & LOCATION & FileName1 & " - " & FileName2 & ".pdf"

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=PDFFullName, Quality:=xlQualityStandard, OpenAfterPublish:=False
Application.DisplayAlerts = True

End With
End Sub
This comment was minimized by the moderator on the site
Hi, I’m wondering if anyone could help me. I have this pasted into my workbook but it returns a file name of FALSE.xlsm in the Documents folder rather than the specified path.
I had to tweak it to make this work, prior to tweaking it I was getting a Run-time error ‘1004’ Excel cannot access the file.
This is the amended code:
Sub filename_cellvalue()Dim Path As StringDim filename As StringPath = “C:\Users\DC\Documents\Planners\”Filename = Range (“K2”)ActiveWorkbook.SaveAs Filename = (Path & Filename & “.xlsm”)End Sub
Using Excel 2016
Any help would be greatly appreciated.
Thanks in advance
This comment was minimized by the moderator on the site
can anyone please help with a solution, i've managed to get the output correctly if the cell is not blank



Private Sub Saveas()

Dim path As String

Dim filename1 As String



path = "C:\User\Compwork\"



filename1 = path & Range("G4") & " " & "-" & " " & Range("G6") & " " & "-" & " " & Range("H7") & " " & "VTError"

Application.DisplayAlerts = False

ActiveWorkbook.Saveas filename1, FileFormat:=52, CreateBackup:=False

ThisWorkbook.Worksheets("report").ExportAsFixedFormat Type:=x1typePDF, Filename:=filename1

Application.DisplayAlerts = True

ActiveWorkbook.Close



End Sub







However, my problem is I'd like to save a filename without leaving a space if the cell value is NULL for instance, if G6 value is blank then i like to the output "G4-H7 VTError".

Any there a way to approach this?





Thanks.
This comment was minimized by the moderator on the site
can anyone please help with a solution, i've managed to get the output correctly if the cell is not blank

Private Sub Saveas()

Dim path As String

Dim filename1 As String



path = "C:\User\Compwork\"



filename1 = path & Range("G4") & " " & "-" & " " & Range("G6") & " " & "-" & " " & Range("H7") & " " & "VTError"

Application.DisplayAlerts = False

ActiveWorkbook.Saveas filename1, FileFormat:=52, CreateBackup:=False

ThisWorkbook.Worksheets("report").ExportAsFixedFormat Type:=x1typePDF, Filename:=filename1

Application.DisplayAlerts = True

ActiveWorkbook.Close



End Sub



However, my problem is I'd like to save a filename without leaving a space if the cell value is NULL for instance, if G6 value is blank then i like to the output "G4-H7 VTError".
Any there a way to approach this?


Thanks.
This comment was minimized by the moderator on the site
I have a control button for saving as PDF with a specific name to a specific location already working, but I want to modify the code as the filename is the name of the active sheet plus the value of a specific cell from another sheet. How do I do that? This is what I have for saving as PDF:


Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
filename:="\\U:\Documents\My_IT_Stuff\T&A\PP11", _
OpenAfterPublish:=False
Application.ScreenUpdating = True
End Sub

Thank you!
This comment was minimized by the moderator on the site
Hi. I am very excited it could work but for me in it is saying "BASIC runtime error. Sob-procedure or function procedure not defined." and arrow indicating line 6. What should I do?
This comment was minimized by the moderator on the site
Se a célula de referencia estiver em uma outra planilha já aberta? Como faço?
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations