简体   繁体   English

为什么在此MyCell范围错误上出现类型不匹配?

[英]Why am I getting a type mismatch on this MyCell range error?

I keep getting a runtime error on this line: 我一直在这行上收到运行时错误:

                        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
                                Filename:=MgrPath & "2018 Mid-Year Comp Statement - " & SM.Range("C5").Value & ".pdf", _
                                Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
                                OpenAfterPublish:=False

Sub Statement_Autoprint() 子Statement_Autoprint()

        Dim MCST As Workbook: Set MCST = ActiveWorkbook
        Dim User As String: User = Environ$("Username")
        Dim SavePath As String: SavePath = "M:\comp_statements\"
        Dim CS As Worksheet: Set CS = MCST.Sheets("Control Sheet")

        Dim MgrPath As String, MyCell As Range, Printed As Integer, i As Integer, SM As Worksheet
        Printed = 0


    Call Disable

For i = 2 To CS.Range("B" & CS.Rows.Count).End(xlUp).Row
    If CS.Range("A" & i) <> "" & CS.Range("B" & i) <> "" Then
        Set SM = MCST.Sheets(CStr(CS.Range("A" & i)))
        SM.Calculate
        SM.Range("P1") = Format(CS.Range("B" & i), "000000000")

            For Each MyCell In SM.Range("N2:N70")
                If MyCell = "HIDE" Then
                    MyCell.EntireRow.Hidden = True
                ElseIf MyCell <> "HIDE" Then
                    MyCell.EntireRow.Hidden = False
                End If
            Next MyCell

        If Not Application.CalculationState = xlDone Then
            DoEvents
        End If

                MgrPath = "M:\Pittsburgh\GRP4\HR_PCorpComp\2018 Midyear\Reporting\Parsley\comp_statements\" & SM.Range("K5") & "\"


                If Dir(MgrPath, vbDirectory) <> "" Then
                    MkDir MgrPath
                End If

                        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
                                    Filename:=MgrPath & "2018 Mid-Year Comp Statement - " & SM.Range("C5").Value & ".pdf", _
                                    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
                                    OpenAfterPublish:=False

                    Printed = Printed + 1
                End If
            Next i

            CS.Activate

            Call Re_Enable


End Sub

I do not have any files that exist/are open under that name, I have no clue what could be preventing this from saving. 我没有使用该名称存在的文件/以该名称打开的文件,我不知道是什么原因阻止了此文件的保存。 All of the other bits of code do what they're supposed to, it just can't loop to the next employee because the save is being suppressed because of that error. 所有其他代码段都按预期运行,它无法循环到下一位员工,因为该错误导致保存被抑制。

Try this 尝试这个

For Each mycell In SM.Range("N2:N70")
    If IsError(mycell) Then
        Debug.Print mycell.Address
    Else
        mycell.EntireRow.Hidden = (mycell = "HIDE")
    End If
Next mycell
  1. Either handle the error using IsError or 使用IsError处理错误或
  2. Go to the cell which the above code points to and check if there are any formula errors. 转到上面的代码指向的单元格,并检查是否存在任何公式错误。

You usually get that error if the cell has formula errors. 如果单元格有公式错误,通常会得到该错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM