簡體   English   中英

為什么在此MyCell范圍錯誤上出現類型不匹配?

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

我一直在這行上收到運行時錯誤:

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

子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

我沒有使用該名稱存在的文件/以該名稱打開的文件,我不知道是什么原因阻止了此文件的保存。 所有其他代碼段都按預期運行,它無法循環到下一位員工,因為該錯誤導致保存被抑制。

嘗試這個

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. 使用IsError處理錯誤或
  2. 轉到上面的代碼指向的單元格,並檢查是否存在任何公式錯誤。

如果單元格有公式錯誤,通常會得到該錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM