簡體   English   中英

從單元格獲取文件名並提示保存文件位置的宏

[英]Macro for getting the file name from the cell and prompt the location to save the file

我正在尋找一個宏,該宏在運行時將從單元格中獲取值並使用該名稱保存它,並提示輸入保存文件的路徑。 以下是我的嘗試:

Sub SelectFolder()
    Dim diaFolder As FileDialog

    Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
    diaFolder.AllowMultiSelect = False
    diaFolder.Show
    ActiveSheet.Copy
    ActiveWorkbook.SaveAs Filename:= _
    diaFolder & ActiveSheet.[d2] & ".xlsx", FileFormat:= _
    xlExcel8, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False


    Set diaFolder = Nothing
End Sub

在這種情況下,該值是從單元格中獲取的,並且提示您要求文件夾保存文件。 但是文件沒有存儲在文件夾中,而是存儲在單獨的文件夾中。 我猜這里需要進行一些小的更正。

另外,在保存時,它還警告我說97-2003文件類型可能存在兼容性問題。 我猜這行需要糾正一些問題:

diaFolder & ActiveSheet.[d2] & ".xlsx", FileFormat:= _
        xlExcel8, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False 

但不知道該更改什么。 我希望文件位於.xlsx中,並且還與所有設備兼容。

有人可以幫我做這兩個嗎?

這是一個工作代碼:

Sub SelectFolder()
    Dim diaFolder As FileDialog
    Dim fileName, filePath As String

    Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)

    diaFolder.AllowMultiSelect = False
    diaFolder.Show
    ActiveSheet.Copy

    fileName = ActiveSheet.Range("D2").Value

    'You need to use diaFolder.SelectedItems(1) and not just diaFolder
    filePath = diaFolder.SelectedItems(1) & "\" & fileName & ".xlsx"

    ActiveWorkbook.SaveAs fileName:= _
        filePath, FileFormat:=xlWorkbookNormal, Password:="", WriteResPassword:="" _
        , ReadOnlyRecommended:=False, CreateBackup:=False

    Set diaFolder = Nothing
End Sub

注意:
我已將FileFormat從Excel8更改為xlWorkbookNormal

您的代碼中的問題:
1. FileFormat Excel 8 97-2003 format in Excel 2007-2013, xls表示97-2003 format in Excel 2007-2013, xls並且您將其另存為.xlsx
2.您僅使用diaFolder而不是diaFolder.SelectedItems(1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM