代码之家  ›  专栏  ›  技术社区  ›  J_Go

ADO+VBA sheetname返回值添加额外字符

  •  0
  • J_Go  · 技术社区  · 8 年前

    我目前正在创建脚本。该脚本需要使用ADO获取关闭的excel工作簿的工作表名称。然而,工作表中有时会有多余的字符,例如单引号和美元符号。

    结果显示:作业列表表--gt;'工作列表'$或工作列表-->求职者$

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

    这是我的代码:

    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
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   cyboashu    8 年前

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

    表命名惯例

    Excel工作簿:使用工作表名称后跟美元符号(用于 例如,[Sheet1$]或[My Worksheet$])。工作簿表 以这种方式引用的是

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