简体   繁体   中英

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()

        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
  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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