簡體   English   中英

#N / A的Excel VBA類型不匹配

[英]Excel VBA Type Mismatch for #N/A

我有這段代碼

    With Data.Cells(rowMatch, GWECol)
        .Value = Cmp.Cells(i, GWENetPr)
        .AddComment
        .Comment.Text Text:=UCase(Environ("UserName")) & ":" & vbNewLine _
            & "Comment: " & Cmp.Cells(i, CommCol) & vbNewLine _
            & "Transaction: " & Cmp.Cells(i, QRTran) & vbNewLine _
            & "QR Pr: " & Cmp.Cells(i, QRPr) & vbNewLine _
            & "QR WD: " & Cmp.Cells(i, QRWD) & vbNewLine _
            & "QR WD All: " & Cmp.Cells(i, QRWDA) & vbNewLine _
            & "QR XPr: " & Cmp.Cells(i, QRXPr) & vbNewLine _
            & "QR XAll: " & Cmp.Cells(i, QRXAll) & vbNewLine _
            & "GWE Pr: " & Cmp.Cells(i, GWEPr) & vbNewLine _
            & "GWE All: " & Cmp.Cells(i, GWEAll) & vbNewLine _
            & "GWE XPr: " & Cmp.Cells(i, GWEXPr) & vbNewLine _
            & "GWE XAll: " & Cmp.Cells(i, GWEXAll)
        .Comment.Shape.TextFrame.AutoSize = True
    End With

Cmp.Cells(i,X)指向可能具有#N / A錯誤(失敗的VLOOKUP)的單元格。

是否可以將代碼僅以#N / A作為字符串或將其保留為空? 現在,只要引用的單元格之一為#N / A,該塊將失敗,並且根本不會添加任何注釋文本。

謝謝!

你正在使用單元格的默認屬性,

Debug.Print Cmp.Cells(i, QRXAll)

例如,這總是指單元格.Value屬性。 .Value實際上是一個錯誤類型, Error 2042 ,我認為你可以通過檢查避免

CLng(Cmp.Cells(i,QRXA11))

但這將導致2042而不是2042 #N/A文本。

如果你想得到字符串Cmp.Cells(i, QRXAll).Text #N/A :嘗試使用Cmp.Cells(i, QRXAll).Text ,它依賴於單元格的.Text屬性而不是它的.Value

Debug.Print Cmp.Cells(i, QRXAll).Text

免責聲明:我已經做了一些VBA編程,但是我不會稱自己是專家。

這可能過於簡單,但您可以將每個值分配給變量,然后將變量分配給注釋。 如果任何一個值為N / A,則至少其余值仍將分配給注釋。 我推薦這種解決方案,因為它確保單個錯誤不會破壞整個操作。

Dim vComment As String
Dim vTransaction As String
Dim vQRPr As String
Dim vQRWD As String
' Etc.

vComment = Cmp.Cells(i, CommCol).Text
vTransaction = Cmp.Cells(i, QRTran).Text
vQRPr = Cmp.Cells(i, QRPr).Text
vQRWD = Cmp.Cells(i, QRWD).Text
' Etc.

.Comment.Text Text:=UCase(Environ("UserName")) & ":" & vbNewLine _
        & "Comment: " & vComment & vbNewLine _
        & "Transaction: " & vTransaction & vbNewLine _
        & "QR Pr: " & vQRPr & vbNewLine _
        & "QR WD: " & vQRWD & vbNewLine
        ' Etc.

編輯:感謝David指出應該使用.Text屬性

使用IsError檢查單元格是否具有#N / A

 if IsError(Cmp.Cells(i, GWENetPr)) then
      'give it a valid value
else
      'use the value int he cell
end if

'start with statement

With Data.Cells(rowMatch, GWECol)
    If IsError(Cmp.Cells(i, GWENetPr)) Then
        .Value = "" 'or #N/A
    Else
        .Value = Cmp.Cells(i, GWENetPr)
    End If

    .AddComment
    .Comment.Text Text:=UCase(Environ("UserName")) & ":" & vbNewLine _
        & "Comment: " & Cmp.Cells(i, CommCol) & vbNewLine _
        & "Transaction: " & Cmp.Cells(i, QRTran) & vbNewLine _
        & "QR Pr: " & Cmp.Cells(i, QRPr) & vbNewLine _
        & "QR WD: " & Cmp.Cells(i, QRWD) & vbNewLine _
        & "QR WD All: " & Cmp.Cells(i, QRWDA) & vbNewLine _
        & "QR XPr: " & Cmp.Cells(i, QRXPr) & vbNewLine _
        & "QR XAll: " & Cmp.Cells(i, QRXAll) & vbNewLine _
        & "GWE Pr: " & Cmp.Cells(i, GWEPr) & vbNewLine _
        & "GWE All: " & Cmp.Cells(i, GWEAll) & vbNewLine _
        & "GWE XPr: " & Cmp.Cells(i, GWEXPr) & vbNewLine _
        & "GWE XAll: " & Cmp.Cells(i, GWEXAll)
    .Comment.Shape.TextFrame.AutoSize = True
End With

如果出現錯誤,您可以使用IIf使用特定值:

& "Comment: " & IIf(IsError(Cmp.Cells(i, CommCol)),"",Cmp.Cells(i, CommCol)) & vbNewLine _

暫無
暫無

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

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