我需要帮助宏
Sub newbook()
Set NewBook = Workbooks.Add
Do
fName = Application.GetSaveAsFilename
Loop Until fName <> False
NewBook.SaveAs Filename:=fName 'showing yellow
此代码不适用于excel 2003和2007
首先,对于修复代码中的某些点很重要:
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
此代码对我来说完全正常。试试看!