ADO + VBA sheetname return value 添加额外的字符

ADO + VBA sheetname return value adds extra characters

我目前正在创建一个脚本。该脚本将需要使用 ADO 获取已关闭的 excel 工作簿的 sheet 名称。但是,sheet 有时会有多余的字符,例如单引号和美元符号。

结果显示:Job list Sheet --> 'Job List Sheet'$ or Joblist --> Joblist$

我知道这很容易被操纵,但有人能告诉我为什么吗?

这是我的代码:

Option Explicit
Private Const SCHEMA_TABLES As Integer = 20
Private Const OPEN_FORWARD_ONLY As Integer = 0
Private Const LOCK_READ_ONLY As Integer = 1
Private Const CMD_TEXT As Long = 1
Private Const PROVIDER As String = "Microsoft.ACE.OLEDB.12.0"
Private Const XL_PROP As String = """Excel 12.0;HDR=No"""
Private Const SHEETS_FIELD_NAME As String = "TABLE_NAME"

Private Sub GetJoblist_Click()
    Dim fPath As String
    Dim fname As String
    Dim oConn As Object
    Dim oRS As Object
    Dim connString As String
    Dim sql As String
    Dim found As Boolean
    Dim sheetField As String
    Dim i As Integer
    Dim key As String
    Dim job_used As String
    Dim sheetfieldvalue As String

    Dim SelectedFile As String
    Dim Source_flow As Range, target_flow As Range
    Dim TP_location As String, TP_filename As String, TP_formula As String, getcellvalue As String

    'Define the path and file name
    fPath = TextBox1.Value

    'Define key word
    key = "Job"

    'Late bind the ADO objects
    Set oConn = CreateObject("ADODB.Connection")
    Set oRS = CreateObject("ADODB.Recordset")

    'Open conection
    connString = "Provider=" & PROVIDER & ";" & _
                 "Data Source=" & fPath & ";" & _
                 "Extended Properties=" & XL_PROP & ";"

    oConn.Open connString

    'Search for the sheet name containing your key
    'in the tables (ie sheets) schema
    found = False
    oRS.Open oConn.OpenSchema(SCHEMA_TABLES)
    i = 1
    Sheets.Add.Name = "Job_list"

    Do While Not oRS.EOF
        sheetField = oRS.Fields(SHEETS_FIELD_NAME).Value ' why does this returns a value with ' and '$?
        Cells(i, 18) = sheetField
        If InStr(sheetField, key) > 0 Or InStr(sheetField, UCase(key)) > 0 Then
            found = True
            If Right(sheetField, 2) = "$'" Then
             sheetfieldvalue = Left(sheetField, Len(sheetField) - 2)
             sheetfieldvalue = Right(sheetfieldvalue, Len(sheetfieldvalue) - 1)
             Cells(i, 17) = sheetfieldvalue
            Else:
                sheetfieldvalue = Left(sheetField, Len(sheetField) - 1)
                Cells(i, 17) = sheetfieldvalue
            End If
            Exit Do
        End If
        oRS.MoveNext
        i = i + 1
    Loop
    oRS.Close

    Set oRS = Nothing
    oConn.Close
    Set oConn = Nothing
End Sub

因为那是 Excel ADO 的命名约定。

Table Naming Conventions

There are several ways you can reference a table (or range) in an Excel workbook: Use the sheet name followed by a dollar sign (for example, [Sheet1$] or [My Worksheet$]). A workbook table that is referenced in this manner consists of the entire used range of the worksheet.

oRS.Open "Select * from [Sheet1$]", oConn, adOpenStatic

阅读更多:https://support.microsoft.com/en-us/kb/278973