Sign in with Microsoft
Sign in or create an account.
Hello,
Select a different account.
You have multiple accounts
Choose the account you want to sign in with.

連結資料類型 最初于 2018 Microsoft 365 Excel 年 6 月發行,因此其他功能可能無法識別它們。 當您想要使用其他功能以條件式識別儲存格是否包含連結的資料類型時,可能尤其如此。 本文將說明一些您可以用於識別儲存格中連結資料類型的解決方法。

附註: 連結的資料類型僅適用于全球多重租使用者用戶端 (標準 Microsoft 365 帳戶) 。

公式

您隨時 都可以撰寫參照資料類型的公式。 不過,如果您想要使用 TEXT 函數來解壓縮具有連結資料類型的儲存格文字,您#VALUE! 錯誤。

解決方法是使用FIELDVALUE函數,並指定field_name欄位。 在下列範例中,如果儲存格 A1 包含股票資料類型,則公式會返回股票名稱。

=FIELDVALUE (A1,"Name")

不過,如果儲存格 A1 不包含連結的資料類型,則 FIELDVALUE 函數會#FIELD 錯誤!. 如果您想要評估儲存格是否包含連結的資料類型,您可以使用下列公式,使用 ISERROR 函數來測試 FIELDVALUE 函數是否會返回錯誤。

=if (ISERROR (FIELDVALUE (A2,"Name") ) ,"此儲存格沒有連結的資料類型","此儲存格具有連結的資料類型")

如果公式評估為錯誤,則它會返回文字「此儲存格沒有連結的資料類型」,否則會返回「此儲存格有連結的資料類型」。

如果您只想隱藏該#FIELD! 錯誤,您可以使用:

=IFERROR (FIELDVALUE (A1,"Name") ,")

如果發生錯誤,會返回空白儲存格。

條件式格式設定

您可以根據 儲存格是否有 連結的資料類型,以條件式格式化儲存格。 您首先會選取需要條件式格式的儲存格,然後前往 > 條件式格式>規則>使用公式...針對公式,您可以使用下列方法:

=NOT (ISERROR (FIELDVALUE (A1,"Name") ) )

儲存格 A1 是您想要評估範圍中的頂端儲存格。 然後,以您想要的格式進行。

在此範例中,如果儲存格 A1 包含 「Name」的有效功能變數名稱,則公式會返回 TRUE,而且會採用格式。 如果儲存格 A1 不包含連結的資料類型,則公式會返回 FALSE,而且不會採用任何格式。 如果您想要反白任何不含有效連結資料類型的儲存格,您可以移除 NOT。

VBA

有幾種 VBA (Visual Basic for Applications) ,您可以使用這些方法來識別儲存格或範圍是否包含連結的資料類型。 此第一個程式使用 HasRichDataType 屬性。 

這兩個程式都會提示您選取要評估的儲存格範圍,然後返回包含結果的郵件方塊。

Sub IsLinkedDataType()
    Dim c As Range
    Dim rng As Range
    Dim strResults As String
    
    Set rng = Application.InputBox("Select a range to check for linked data types", Type:=8)
    
    For Each c In rng
      '    Check if the HasRichDataType is TRUE or FALSE
        If c.HasRichDataType = True Then
        '   The cell holds a linked data type
            strResults = strResults & c.Text & " - Linked data type" & vbCrLf
        Else
            strResults = strResults & c.Text & " - Not a linked data type" & vbCrLf
        End If
    Next c

    MsgBox "Your range contains the following details" & vbCrLf & vbCrLf & strResults, vbInformation + vbOKOnly, "Results"
    
End Sub

下一個程式會使用 LinkedDataTypeState 屬性

Sub IsLinkedDataTypeState()
    Dim c As Range
    Dim rng As Range
    Dim strResults As String
    
    Set rng = Application.InputBox("Select a range to check for linked data types", Type:=8)
    
    For Each c In rng
   '    Check if the LinkedDataTypeState is 1 (TRUE) or 0 (FALSE)
        If c.LinkedDataTypeState = 1 Then
        '   The cell holds a linked data type
            strResults = strResults & c.Text & " - Linked data type" & vbCrLf
        Else
            strResults = strResults & c.Text & " - Not a linked data type" & vbCrLf
        End If
    Next c
    
   MsgBox "Your range contains the following details" & vbCrLf & vbCrLf & strResults, vbInformation + vbOKOnly, "Results"

End Sub

此最終程式碼段是使用者定義的函數 (UDF) ,而且您參照它就像任何其他公式Excel一樣。 只要輸入 =fn_IsLinkedDataType (A1) ,其中 A1 是您想要評估的儲存格。

Public Function fn_IsLinkedDataType(c As Range)
'   Function will return TRUE if a referenced cell contains a linked data type
    If c.HasRichDataType = True Then
      fn_IsLinkedDataType = "Linked data type"
    Else
        fn_IsLinkedDataType = "Not a linked data type"
    End If
End Function

若要使用這些範例,請按Alt+F11以開啟 Visual Basic 編輯器 (VBE) ,然後前往 [插入>模組,然後將程式碼貼到右側開啟的新視窗中。 當您完成時,您可以使用 Alt+Q返回Excel返回[ 若要執行前兩個範例的其中一個,請 前往開發人員>程式碼>宏>從清單中選取您想要執行的宏,然後選取執行

需要更多協助嗎?

您可以隨時詢問 Excel 技術社群中的專家,或在社群中取得支援。

Need more help?

Want more options?

探索訂閱權益、瀏覽訓練課程、瞭解如何保護您的裝置等等。

社群可協助您詢問並回答問題、提供意見反應,以及聆聽來自具有豐富知識的專家意見。

Was this information helpful?

How satisfied are you with the translation quality?
What affected your experience?
By pressing submit, your feedback will be used to improve Microsoft products and services. Your IT admin will be able to collect this data. Privacy Statement.

Thank you for your feedback!

×