Excel 分配给范围时出错:参数数目错误

Excel 分配给范围时出错:参数数目错误,excel,vba,Excel,Vba,我需要循环浏览命名工作表,从每张工作表创建pdf并将其保存到文件夹中 到目前为止,我们已经创建了以下内容,并且它一直在说错误数量的参数 如果Range(“B16”)=“3页声明”,那么Range会停留在“3页”,然后它会说 “参数数目错误或属性分配无效” 使用Win7和Excel 2010 Sub CreatePDFs_Click() ' CreatePDF_Statements ' ' Create a PDF from the current sheet and save to folde

我需要循环浏览命名工作表,从每张工作表创建pdf并将其保存到文件夹中

到目前为止,我们已经创建了以下内容,并且它一直在说错误数量的参数

如果Range(“B16”)=“3页声明”,那么Range会停留在“3页”,然后它会说

“参数数目错误或属性分配无效”

使用Win7和Excel 2010

Sub CreatePDFs_Click()
' CreatePDF_Statements
'

' Create a PDF from the current sheet and save to folder

Dim DestFolder As String, PDFFile As String
Dim OpenPDFAfterCreating As Boolean, AlwaysOverwritePDF As Boolean, DisplayEmail As Boolean
Dim OverwritePDF As VbMsgBoxResult
Dim ws As Worksheet
Dim sheet_name As Range

'Loop
For Each sheet_name In Sheets("Info").Range("A:A")
If sheet_name.Value = "" Then
Exit For
Else
Sheets(sheet_name.Value).Select

ws.Activate

    OpenPDFAfterCreating = False    'Change this if you want to open the PDF after creating it : TRUE or FALSE
    AlwaysOverwritePDF = False      'Change this if you always want to overwrite a PDF that already exists :TRUE or FALSE

DestFolder = Sheets("Dashboard").Range("M4")


    'Customer Name stored in B8
    CustomerName = Left(ActiveSheet.Range("B8").Value, InStr(1, ActiveSheet.Range("B8").Value, " ") + 50)

    'Create new PDF file name including path and file extension
    PDFFile = DestFolder & Application.PathSeparator & CustomerName & ".pdf"

    'If the PDF already exists
    If Len(Dir(PDFFile)) > 0 Then
        If AlwaysOverwritePDF = False Then

            OverwritePDF = MsgBox(PDFFile & " already exists." & vbCrLf & vbCrLf & "Do you want to overwrite it?", vbYesNo + vbQuestion, "File Exists")

            On Error Resume Next
            'If you want to overwrite the file then delete the current one
            If OverwritePDF = vbYes Then
                 Kill PDFFile
                     Else

                MsgBox "OK then, if you don't overwrite the existing PDF, I can't continue." _
                    & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Exiting Macro"
                Exit Sub
            End If
        Else
            On Error Resume Next
            Kill PDFFile
        End If
        If Err.Number <> 0 Then

            MsgBox "Unable to delete existing file.  Please make sure the file is not open or write protected." _
                    & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Unable to Delete File"
            Exit Sub
        End If
    End If

    'Create the PDF
'0 Page

    If Range("B16") = "0   page Statement" Then

    Range("B2:I50").Select
    Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=OpenPDFAfterCreating

    End If

'1 Page

    If Range("B16") = "1   page Statement" Then

    Range("B2:I50").Select
    Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=OpenPDFAfterCreating

    End If

'2 Pages
    If Range("B16") = "2   page Statement" Then

    Range("B2:I50,K2:R50").Select
Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=OpenPDFAfterCreating

    End If

'3 Pages
    If Range("B16") = "3   page Statement" Then

    Range("B2:I50,K2:R50,T2:AA50").Select
Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=OpenPDFAfterCreating

    End If

'4 Pages
    If Range("B16") = "4   page Statement" Then

    Range("B2:I50,K2:R50,T2:AA50,AC2:AJ50").Select
    Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=OpenPDFAfterCreating

    End If

'5 Pages
    If Range("B16") = "5   page Statement" Then

    Range("B2:I50,K2:R50,T2:AA50,AC2:AJ50,AL2:AS50").Select
    Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=OpenPDFAfterCreating


    End If

          Cancel = True


        End If

Sheets("Dashboard").Select
Range("A1").Select

End Sub
Sub-createPDF_-Click()
'创建PDF_语句
'
'从当前工作表创建PDF并保存到文件夹
将文件夹设置为字符串,将PDFFile设置为字符串
Dim OpenPDFaftCreating为布尔值,AlwaysOverwritePDF为布尔值,DisplayEmail为布尔值
Dim将EPDF覆盖为VbMsgBoxResult
将ws设置为工作表
尺寸表名称作为范围
'循环
对于每张图纸,图纸中的名称(“信息”)范围(“A:A”)
如果工作表_name.Value=”“,则
退出
其他的
工作表(工作表名称.值)。选择
ws.Activate
OpenPDFAfterCreating=False“如果要在创建PDF后打开它,请更改此选项:TRUE或False
AlwaysOverwritePDF=False“如果您始终希望覆盖已存在的PDF,请更改此选项:TRUE或False
DestFolder=工作表(“仪表板”).范围(“M4”)
'存储在B8中的客户名称
CustomerName=Left(ActiveSheet.Range(“B8”).值,InStr(1,ActiveSheet.Range(“B8”).值,”)+50)
'创建新的PDF文件名,包括路径和文件扩展名
PDFFile=DestFolder&Application.PathSeparator&CustomerName&“.pdf”
'如果PDF已经存在
如果Len(Dir(PDFFile))>0,则
如果AlwaysOverwritePDF=False,则
OverwritePDF=MsgBox(PDFFile&“已存在”。&vbCrLf&vbCrLf&“是否要覆盖它?”,vbYesNo+vbQuestion,“文件存在”)
出错时继续下一步
'如果要覆盖该文件,请删除当前文件
如果覆盖EPDF=vbYes,则
杀死Pdfile
其他的
MsgBox“好的,如果你不覆盖现有的PDF,我就不能继续。”_
&vbCrLf&vbCrLf&“按“确定”退出此宏。”,vbCritical,“退出宏”
出口接头
如果结束
其他的
出错时继续下一步
杀死Pdfile
如果结束
如果错误号为0,则
MsgBox“无法删除现有文件。请确保该文件未打开或未受写保护。”_
&vbCrLf&vbCrLf&“按“确定”退出此宏。”,vbCritical,“无法删除文件”
出口接头
如果结束
如果结束
'创建PDF
“0页
如果范围(“B16”)=“0页语句”,则
范围(“B2:I50”)。选择
Selection.ExportAsFixedFormat类型:=xlTypePDF,文件名:=PDFFile,质量:=xlQualityStandard,IncludeDocProperties:=True,忽略打印区域_
:=False,OpenAfterPublish:=OpenPDFAfterCreating
如果结束
“1页
如果范围(“B16”)=“1页声明”,则
范围(“B2:I50”)。选择
Selection.ExportAsFixedFormat类型:=xlTypePDF,文件名:=PDFFile,质量:=xlQualityStandard,IncludeDocProperties:=True,忽略打印区域_
:=False,OpenAfterPublish:=OpenPDFAfterCreating
如果结束
“2页
如果范围(“B16”)=“2页声明”,则
范围(“B2:I50,K2:R50”)。选择
Selection.ExportAsFixedFormat类型:=xlTypePDF,文件名:=PDFFile,质量:=xlQualityStandard,IncludeDocProperties:=True,忽略打印区域_
:=False,OpenAfterPublish:=OpenPDFAfterCreating
如果结束
“3页
如果范围(“B16”)=“3页声明”,则
范围(“B2:I50,K2:R50,T2:AA50”)。选择
Selection.ExportAsFixedFormat类型:=xlTypePDF,文件名:=PDFFile,质量:=xlQualityStandard,IncludeDocProperties:=True,忽略打印区域_
:=False,OpenAfterPublish:=OpenPDFAfterCreating
如果结束
“4页
如果范围(“B16”)=“4页声明”,则
范围(“B2:I50,K2:R50,T2:AA50,AC2:AJ50”)。选择
Selection.ExportAsFixedFormat类型:=xlTypePDF,文件名:=PDFFile,质量:=xlQualityStandard,IncludeDocProperties:=True,忽略打印区域_
:=False,OpenAfterPublish:=OpenPDFAfterCreating
如果结束
“5页
如果范围(“B16”)=“5页声明”,则
范围(“B2:I50,K2:R50,T2:AA50,AC2:AJ50,AL2:AS50”)。选择
Selection.ExportAsFixedFormat类型:=xlTypePDF,文件名:=PDFFile,质量:=xlQualityStandard,IncludeDocProperties:=True,忽略打印区域_
:=False,OpenAfterPublish:=OpenPDFAfterCreating
如果结束
取消=真
如果结束
工作表(“仪表板”)。选择
范围(“A1”)。选择
端接头

提取错误消息

“参数数目错误或属性分配无效”

。。。问题是,您试图将3个以上的参数传递给
范围
,这最多需要2个参数。您希望在单个范围参照中组合每个不同的区域

改变

Range("B2:I50", "K2:R50")
...
Range("B2:I50", "K2:R50", "T2:AA50")
...
Range("B2:I50", "K2:R50", "T2:AA50", "AC2:AJ50")
...
Range("B2:I50", "K2:R50", "T2:AA50", "AC2:AJ50", "AL2:AS50")


这是很多代码。你能把它缩小到只有相关的部分和抛出错误的行吗?如果Range(“B16”)=“3页语句”然后Range,它会停留在“3页”上-然后它会说“错误的参数数或无效的属性赋值”谢谢你的输入,我已经做了更正,但作为一名新手,我在结尾处收到一条新的错误消息,上面写着“没有下一个”,请您进一步建议您可能缺少结尾。如果没有,您的问题我已移动:ws.Activate FROM UNDER:DestFolder=Sheets(“仪表板”)。范围(“M4”)到UNDER:Sheets(sheet_name.Value)。请选择并删除最后一个:以结束单击此链接:-并用您的代码编辑您的原始问题,谢谢。
Range("B2:I50,K2:R50")
...
Range("B2:I50,K2:R50,T2:AA50")
...
Range("B2:I50,K2:R50,T2:AA50,AC2:AJ50")
...
Range("B2:I50,K2:R50,T2:AA50,AC2:AJ50,AL2:AS50")