VBA将具有给定名称的Excel文件保存到特定位置

时间:2018-11-26 13:39:05

标签: excel vba filenames excel-2016 savefiledialog

我最近询问有关将具有特定名称的excel文件保存到设置的位置的问题: Getting correct default save name and save directory with spaces in VBA

我想使用相同的过程,但方式有所不同。我尝试编辑代码以使其正常工作,但是执行代码时,我始终保持黑色文件名屏幕。

我要用例程保存的文件是一个模板,每4周更新一次新数据。这是一个只读文件,用作源模板,并且在更新数据后,必须将我保存在其他位置,以防止由于错误/不必要的修改而保存原始源文件。

刷新时,模板将打开一个文件,其中包含名为“ refresh_segment_template.xlsm”的刷新脚本。

模板中的代码为:

Option Explicit

Dim aantalrijen As Long

Const SheetSchaduwblad As String = "schaduwblad"
-------
Sub vernieuwalles()
Dim myTemplate As String: myTemplate = ActiveWorkbook.Name
Dim myTool As String: myTool = "refresh_segment_template.xlsm"

  Application.ScreenUpdating = False

  Workbooks.Open GetPath & myTool
  Application.Run myTool & "!vernieuwalles", myTemplate

  Call Windows(myTool).Close(False)

  Application.ScreenUpdating = True

End Sub
Private Function GetPath() As String
Dim myPosition As Integer
Dim myPath As String: myPath = ActiveWorkbook.Path

  myPosition = InStr(StrReverse(myPath), "\") - 1
  myPosition = Len(myPath) - myPosition

  GetPath = Mid(myPath, 1, myPosition - 1) & "\XLAM\"

End Function

refresh_segment_template 中的代码为:

Option Explicit

Dim aantalrijen As Long

Const SheetSchaduwblad As String = "schaduwblad"
------------------    
Sub vernieuwalles(mytemplate As String)

  Windows(mytemplate).Activate

  On Error GoTo Err_

  Application.StatusBar = "Bezig met vernieuwen"

  Application.ScreenUpdating = False
  Application.Calculation = xlCalculationManual

'  Call SheetOpschonen

  Call datawissen
  Call dataplaatsen
  Call kolomtitels
  Call toevoegen
  Call maaktabel
  Call refreshpivots

Exit_:
  Application.StatusBar = ""
  Application.ScreenUpdating = True
  Application.Calculation = xlCalculationAutomatic
  Exit Sub

Err_:
  Call MsgBox(Err.Number & vbCrLf & Err.Description)
  Resume Exit_

Application.Calculation = xlCalculationAutomatic

End Sub
    -------------
    Sub refreshpivots()
    Dim workbook_Name As Variant
    Dim location As String
    Dim filename As String

    filename = "M:\Commercie\Marktdata\IRi\Segment Ontwikkeling\" & ActiveWorkbook.Name

      ActiveWorkbook.RefreshAll

    workbook_Name = Application.GetSaveAsFilename(fileFilter:="Excel binary sheet (*.xlsb), *.xlsb", InitialFileName:="M:\Commercie\Marktdata\IRi\Segment Ontwikkeling\")

    If workbook_Name <> False Then

        ActiveWorkbook.SaveAs filename:=filename, WriteResPassword:="TM", FileFormat:=50

    End If

    End Sub

我现在想知道如何使最后一个脚本使用 template 的文件名并将其保存在脚本的给定位置(即M:\ Commercie \ Marktdata \ IRi \ Segment Ontwikkeling)。

当我执行上面的代码时,我得到一个“另存为”屏幕,但是没有给出文件名,只有设置的目录是正确的。

refresh_segment_template 是一个 .xlsm 文件。模板是一个 .xlsb 文件。

0 个答案:

没有答案