2016-09-21 14 views
1

私はMicrosoft VBAの新機能です。Excelマクロを修正するのに困っています。ワークシートを保存するExcelマクロ

このマクロの目的は、ボタンを押すと自動的にアクティブなワークシートをファイルに保存するが、機能していないため、その理由がわからないということです。

私には正しいようです。

Sub Save() 
' 
' Save Macro 
' 
Sheets("My_sheet").Select 
    ChDir "C:\my_file" 
    ActiveWorkbook.SaveAs Filename:=Range("B6"), FileFormat:=xlOpenXMLWorkbookMacroEnabled, _ 
     Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _ 
     CreateBackup:=False 
    Sheets("My_sheet").Select 
' 
End Sub 
+0

何が問題なのですか?間違いはありますか? 'Range(" B6 ")とは何ですか? – Comintern

+0

"シート(" My_sheet ")を強調表示するエラーが表示されます。このマクロの一部を選択して、ワークシートを保存できません。 'Range(" B6 ")'は名前が書かれたセルです。この名前は、保存されたファイルの名前である必要があります。 – Rods2292

+2

「My_sheet」という名前のシートがあるとしますか?エラーは何ですか? – Comintern

答えて

0

を参照してください。 保存する前に確認することができます:

Sub SaveMe() 
Dim filename As String 
'check if directory exist 
If Dir("C:\my_file", vbDirectory) = "" Then 
    'if not ask if it should be created and continued 
    rspCreate = MsgBox("Directory doesn't exist, do you wish to create it and continue?", vbYesNo) 
    If rspCreate = vbYes Then 
     'create dir and carry on 
     MkDir "C:\my_file" 
    ElseIf rspCreate = vbNo Then 
     'no selected, stop execution 
     Exit Sub 
    End If 
End If 

filename = Range("B6") 
Sheets("My_sheet").Select 
ChDir "C:\my_file" 
'check if file name is valid 
If FileNameValid(filename) Then 
ActiveWorkbook.SaveAs filename:=Range("B6"), FileFormat:=xlOpenXMLWorkbookMacroEnabled, _ 
    Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _ 
    CreateBackup:=False 
Else 
    MsgBox "Invalid file name, file not saved" 
End If 
Sheets("My_sheet").Select 
End Sub 


'check if vali file name is used in cell 
Function FileNameValid(sFileName As String) As Boolean 
Dim notAllowed As Variant 
Dim i As Long 
Dim result As Boolean 
'list of forbidden characters 
notAllowed = Array("/", "\", ":", "*", "?", "< ", ">", "|", """") 
'Initial result = OK 
result = True 
For i = LBound(notAllowed) To UBound(notAllowed) 
    If InStr(1, sFileName, notAllowed(i)) > 0 Then 
    'forbidden character used 
     result = False 
     Exit Function 
    End If 
Next i 
FileNameValid = result 
End Function 
-1

あなたが唯一のシートに新しいブックとして"My_sheet"を保存しようとする場合、これを試してみてください。

Option Explicit 
Sub Save() 
    Dim Sht As Worksheet 

    Set Sht = ActiveWorkbook.Sheets("My_sheet") ' update sheet name 

    ' Copy the sheet to a new workbook 
    Sht.Copy 
    ''' !Note: run time error if folder doesn't exist 
    ChDir "C:\my_file" 

    ''' !Note: run time error if "B6" contains invalid file name or is empty 
    ' Save the copied sheet 
    ActiveWorkbook.SaveAs FileName:=Sht.Range("B6"), _ 
          FileFormat:=xlOpenXMLWorkbookMacroEnabled, _ 
          Password:="", _ 
          WriteResPassword:="", _ 
          ReadOnlyRecommended:=False, _ 
          CreateBackup:=False 


End Sub 

また、限り、シート名が "My_Sheet" として設定されているように、フォルダが存在するとファイル名が正しいか正常に動作するようですHow to avoid using Select in Excel VBA macros

For File Format See Examples

+0

Sht.Copyの目的は何ですか?クリップボードにあるものは何でも爆発します。 – Comintern

関連する問題