我目前正在创建脚本。该脚本需要使用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