我需要有关“另存为”的帮助

问题描述 投票:0回答:1

我需要帮助

Sub newbook()
Set NewBook = Workbooks.Add 
Do 
    fName = Application.GetSaveAsFilename 
Loop Until fName <> False 
NewBook.SaveAs Filename:=fName 'showing yellow

此代码不适用于excel 2003和2007

excel vba save-as
1个回答
0
投票
不清楚您说什么:

首先,对于修复代码中的某些点很重要:

Option Explicit ' It is a good practice to use it always. This force your code to ' "ask" for every var to be declare before use it Sub newbook_1() ' Never use the same name for a var and a function/sub inside the same function/sub. That is why i change it 'as I told you Option Explicit requieres a declaration of the var before 'Here I use one line declaration. It is the same 'if you use this: ' Dim NewBook As Workbook ' Set NewBook = Workbooks.Add 'but just in one line Dim NewBook As Workbook: Set NewBook = Workbooks.Add Dim fName 'When you declare a variable, IF you don't define a var type, the var always will be Variant 'It is the same this: ' Dim fName As Variant Do fName = Application.GetSaveAsFilename 'Ask for the name and directory... Loop Until fName <> False 'Validate if the user put a name... 'Notice: when a new workbook is create and save it will set "Book#" as a default name 'when the dialog is called. Where the # is the following consecutive number NewBook.SaveAs Filename:=fName, FileFormat:=51 'Here! ' Save the file, with the name provide it for the user, 'whit the file format you want. End Sub

此代码对我来说完全正常。试试看!
© www.soinside.com 2019 - 2024. All rights reserved.