2017-08-16 26 views
0

マクロをExcel上に配置して、タブをループし、タブをPDFとして特定のフォルダに保存します。Excel VBA:実行時エラー5 - 無効なプロシージャコールまたは引数

マクロは部分的に、それはいくつかのPDFを作成しますが、その後停止し、このエラーがスローされます動作します

Option Explicit 

Sub WorksheetLoop() 

Dim wsA As Worksheet 
Dim wbA As Workbook 
Dim strTime As String 
Dim strName As String 
Dim strPath As String 
Dim strFile As String 
Dim strPathFile As String 
Dim myFile As Variant 
Dim rng As Range 

' Prevents screen refreshing. 
Application.ScreenUpdating = False 

Set wbA = ActiveWorkbook 

strPath = wbA.Path 
strTime = Format(Now(), "yyyymmdd") 

'get active workbook folder, if saved 
strPath = wbA.Path 
If strPath = "" Then 
    strPath = Application.DefaultFilePath 
End If 
strPath = strPath & "\" 

' Begin the loop. 
For Each wsA In ActiveWorkbook.Worksheets 

    'replace spaces and periods in sheet name 
    strName = Replace(wsA.Name, " ", "") 
    strName = Replace(strName, ".", "_") 

    If strName = "Macro" Then 
     MsgBox "That's all folks! :)" 
     Exit Sub 
    End If 

    If strName = "TOUCHPOINTS" Then 
     strName = "Touchpoints by markets" 
    End If 

    If strName = "VIDEOHOURS" Then 
     strName = "Viewing Hours by markets" 
    End If 

    If strName = "TARGETS" Then 
     strName = "Shares by markets" 
    End If 

    If strName = "SHARESCHANNELS" Then 
     strName = "IGNORE ME" 
    End If 

    If strName = "TOP10PREMIERES" Then 
     strName = "Top 10 Premieres by markets" 
    End If 

    If strName = "SHARETREND" Then 
     strName = "Share trends last 13 months" 
    End If 

    If strName = "COMPETITION" Then 
     strName = "Share overview international media companies" 
    End If 

    If strName = "COMPETITIONSHARETREND" Then 
     strName = "Share trends factual competitors last 13 months" 
    End If 

    If strName = "PUT" Then 
     strName = "PUT level" 
    End If 

    If strName = "CHANNELRANKER" Then 
     strName = "Top 20 Channels by Market" 
    End If 

    'create default name for savng file 
    strFile = strName & "_" & strTime & ".pdf" 
    myFile = strPath & strFile 

    Debug.Print myFile 

    'export to PDF if a folder was selected 
    If myFile <> "False" Then 
     wsA.ExportAsFixedFormat _ 
       Type:=xlTypePDF, _ 
       Filename:=myFile, _ 
       Quality:=xlQualityStandard, _ 
       IncludeDocProperties:=True, _ 
       IgnorePrintAreas:=False, _ 
       OpenAfterPublish:=False 
    End If 

Next wsA 

' Enables screen refreshing. 
Application.ScreenUpdating = True 

End Sub 
+2

だからどこでエラーが発生しますか? :) – Vityata

+0

^^最後に 'Debug.Print myFile'が印刷したものは何ですか? – YowE3K

答えて

0

はおそらくエラーはここにある: If myFile <> "False" Then

ここ

Run Time Error 5 - Invalid Procedure Call or Argument 

は私のコードです

これは単純にこのようにする必要があります - >If myFile ThenまたはIf len(myFile) >8 Then


ただし、コードは正常に動作しています。ただのSelect Caseでそれを再構築しようとすると、それが道良く見えるとわずかに速い:

Select Case strName 
    Case "Macro" 
     MsgBox "That's all" 
     Exit Sub 
    Case "TOUCHPOINTS" 
     strName = "Touchpoints by markets" 
    Case Else 
     Debug.Print "I don't know -> "; strName 
End Select 

おそらくあなたは、エラーが見つかります。

+1

@ YowE3K - 3つのうち1つが正しいと思われます。D – Vityata

関連する問題