连接时如何将变体 dim 传递给字符串?

How do you pass a variant dim to a string when concatenating?

我正在尝试导出具有特定名称的文件(其中名称的不同部分是变量)。到目前为止,前两个字符串没有引起问题。当我执行索引匹配函数并获得变体数据类型值时,问题就出现了。我无法将变体类型转换为字符串以供稍后在代码中使用。以下片段:

Sub Export_Imgs()

    Dim tempSht as Range
    Dim dataSht as Range
    Dim matCell as Range
    Dim jobNum as Range
    Dim buildDate As String
    Dim buildID As String
    Dim imgFile As String
    Dim matID As Variant
    Dim imgMap As String
    Dim imgPDF As String
    Dim imgJPG As String

    Set tempSht = ThisWorkbook.Sheets("Template")
    Set dataSht = ThisWorkbook.Sheets("Machines & Material")
    Set matCell = tempSht.Range("$E")
    Set jobNum = tempSht.Range("$E")

    'Define file name variables
    buildDate = Format(Date, "YYYY-MM-DD")
    buildID = Left(jobNum, InStr(jobNum, ".") - 1)
    matID = WorksheetFunction.Index((dataSht.ListObjects("AMmat").ListColumns(4).DataBodyRange), _
        WorksheetFunction.Match(matCell, dataSht.ListObjects("AMmat").ListColumns(1).DataBodyRange, 0))

        imgMap = "C:\Users\Example\Desktop\"
        imgJPG = buildDate & "_" & buildID & "_" & CStr(matID) & ".jpg"
        imgFile = imgMap & imgJPG

End Sub

我在靠近代码底部的 imgJPG 行中收到 "type mismatch" 错误。我觉得好像缺少一些非常简单的东西。任何帮助将不胜感激。

我不会在这里使用 Index,而是使用类似下面的方法:

With dataSht.ListObjects("AMmat")
    Dim rw
    rw = Application.Match(matCell.Value, .ListColumns(1).DataBodyRange, 0)

    If Not IsError(rw) Then
        matId = .ListColumns(4).DataBodyRange.Cells(rw).Value
    End If   
End With