2

我有这段代码

    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,块就会失败,并且根本不会添加任何注释文本。

谢谢!

4

4 回答 4

5

您正在使用单元格的默认属性,

Debug.Print Cmp.Cells(i, QRXAll)

例如,这总是指单元格.Value属性。这.Value实际上是一种错误类型,Error 2042我认为您可以通过检查来避免

CLng(Cmp.Cells(i,QRXA11))

但这将导致2042而不是#N/A文本。

如果要获取字符串#N/A:尝试使用Cmp.Cells(i, QRXAll).Text which 依赖于单元格的.Text属性而不是其.Value.

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

于 2013-09-20T20:11:03.257 回答
1

免责声明:我做过一些 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.

编辑:感谢大卫指出.Text应该使用该属性

于 2013-09-20T20:11:14.053 回答
1

使用 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
于 2013-09-20T20:17:09.047 回答
0

如果出现错误,您可以使用IIf特定值:

& "Comment: " & IIf(IsError(Cmp.Cells(i, CommCol)),"",Cmp.Cells(i, CommCol)) & vbNewLine _
于 2013-09-20T20:15:54.063 回答