Open save window in file path from a cell well also populateing filename from cell

依然范特西╮ 提交于 2020-01-05 16:52:13

问题


i have a workbook that i use as a template to make estimates that when i'm done filling out the template there is a macro that creates a new workbook and copies all the sheets of the template workbook to the new one and then removes all the formulas and info i don't want the customer to see.

Here's part of my code that creates the new workbook and copies all the sheets from the template to the new one and then cleans it up

Sub TestConvert()



'Disabling the following to speed up the vba code, must re-enable at end of code
ActiveSheet.DisplayPageBreaks = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayAlerts = False




'adds file name and path to all sheets
Dim WSfn As Worksheet
For Each WSfn In ThisWorkbook.Sheets

'Adds formula to show file path
WSfn.Range("A2") = "=LEFT(CELL(""filename"",RC),FIND(""["",CELL(""filename"",RC),1)-1)"

'Adds formula to show file name
WSfn.Range("A3") = "=MID(CELL(""filename""),FIND(""["",CELL(""filename""))+1,(FIND(""]"",CELL(""filename""))-FIND(""["",CELL(""Filename""))-16))"

WSfn.Calculate 'Calculate sheet

WSfn.Range("A2") = WSfn.Range("A2") 'this will remove the formula from the cell making it text only
WSfn.Range("A3") = WSfn.Range("A3") 'this will remove the formula from the cell making it text only


Next



'************************************************************************************************


'copies all the sheets of the open workbook to a new one

Dim thisWb As Workbook, wbTemp As Workbook
Dim ws As Worksheet


Set thisWb = ThisWorkbook
Set wbTemp = Workbooks.Add 'creates new workbook dimmed as WbTemp

On Error Resume Next 'if there is in error when deleting will not stop the macro from continuing...
'.. deletes the extra sheets 2 sheets if on an older versions of excel
For Each ws In wbTemp.Worksheets
    ws.Delete 'deletes all but one sheet in new workbook
Next
On Error GoTo -1 'clears the error handling and sets it to nothing which allows you to create another error trap.

'copys all the sheets from the original to the new workbook dimmed as wbTemp
For Each ws In thisWb.Sheets
    ws.Copy After:=wbTemp.Sheets(wbTemp.Worksheets.Count)
Next

wbTemp.Sheets(1).Delete 'deletes the the first sheet in the list in the new workbook which is a black sheet from creating a new workbook



'put vba code to be ran in new book here


'makes all formulas in new workbook values only
wbTemp.Sheets.Select 'selects all sheets in new workbook
Cells.Select 'selects all cell
Selection.Copy 'copies everything selected

Selection.PasteSpecial Paste:=xlPasteValues 'pastes as values only in selected cells

wbTemp.Application.CutCopyMode = False 'clears the clipbored




'removes all defind names from new workbook / submittal
Dim xName As Name
For Each xName In wbTemp.Names
xName.Delete
Next




'removes all dropdowns from new workbook / submittal
Dim DD As Worksheet
For Each DD In wbTemp.Worksheets
Cells.Select
DD.Cells.Validation.Delete
Range("A1").Select
Next


'removes all vba buttons from all sheets
Dim i As Integer
On Error Resume Next
For i = 1 To 1000
wbTemp.Sheets(i).Buttons.Delete
Next i




'All sheets scroll to top left and select "A1"
    Dim Sht As Worksheet

 '****************************
 'change A1 to suit your preference
Const TopLeft As String = "A1"
 '****************************

 'loop thru all the sheets in the workbook
For Each Sht In Worksheets

  'scroll:=True takes cell to the top-left of window
Application.Goto Sheet.Range(TopLeft), scroll:=True
Next



'Hides the following from all sheets
wbTemp.Sheets.Select 'selects all sheets in new workbook
ActiveWindow.DisplayGridlines = False
ActiveWindow.DisplayHeadings = False


'selects the first sheet in the list
Sheets(1).Select



ActiveSheet.DisplayPageBreaks = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayAlerts = True



'save vba code here

'works to only add the filename would like it to also open in file path from cell A2
Application.Dialogs(xlDialogSaveAs).Show Range("A3").Text & "- (Submittal) " & Format(Date, "mm-dd-yy") & "_" & Format(Time, "hhmm") & ".xlsx"



End Sub

im wanting to make it so when the save window opens it opens in the file path from cell A2 and populates the filename from cell A3

i can also send/post the full excel file if that helps any.


回答1:


The Application.GetSaveAsFilename method is a good choice for this. Pass the return value to a variant type var so you can test for Cancel or Close.

Dim sFN As Variant
With Worksheets("Sheet6")
    sFN = .Range("A1") & Chr(92) & .Range("A2") & Format(Date, "_mm-dd-yy")  '<~~ no extension yet
End With
With Application
    sFN = .GetSaveAsFilename(InitialFileName:=sFN, _
                             FileFilter:="Excel Workbook (*.xlsx), *.xlsx," & _
                                         "Macro Workbook (*.xlsm), *.xlsm," & _
                                         "Binary Workbook (*.xlsb), *.xlsb")
End With

Select Case sFN
    Case False
        'user clicked Cancel or Close (×)
        Debug.Print sFN
    Case Else
        With ThisWorkbook
            Select Case Right(sFN, 5)
                Case ".xlsx"
                    .SaveAs Filename:=sFN, FileFormat:=xlOpenXMLWorkbook
                Case ".xlsm"
                    .SaveAs Filename:=sFN, FileFormat:=xlOpenXMLWorkbookMacroEnabled
                Case ".xlsb"
                    .SaveAs Filename:=sFN, FileFormat:=xlExcel12
                Case Else
                    'there really shouldn't be a case else
            End Select
        End With
End Select

I've added a Select Case statement statement for a Workbook.SaveAs method to three msot common types of Excel workbooks.




回答2:


You can use the .InitialFileName property of the dialog.

Dim ws As Excel.Worksheet
Set ws = ActiveWorkbook.Sheets("Sheet1")

Dim oFileDialog As FileDialog
Set oFileDialog = Application.FileDialog(msoFileDialogSaveAs)

With oFileDialog
    .Title = "Save File"
    .ButtonName = "Ok"
    .InitialFileName = ws.Range("A2").Value & "\" & ws.Range("A3").Value
    .Show
End With

If you need to get back the name that it was saved as you can use .SelectedItems after .Show

MsgBox (oFileDialog.SelectedItems(1))

NOTE:
You probably want to do a quick verification that the directory in A2 exists before doing this. If it does not exist it will throw this into some users folder.

EDIT I'm not sure why yours isn't saving, could be excel version or some other variable in your code.

Since you have the path and name, do you really need the saveas dialog? You could just do

Workbooks.Add

'Then your code in your template that is modifying the active workbook

'Then save it without the dialog
ActiveWorkbook.SaveAs ws.Range("A2").Value & "\" & ws.Range("A3").Value
'OR  
ActiveWorkbook.SaveAs Filename:= ws.Range("A2").Value & "\" & ws.Range("A3").Value


来源:https://stackoverflow.com/questions/35434096/open-save-window-in-file-path-from-a-cell-well-also-populateing-filename-from-ce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!