Board logo

標題: 請教VBA SQL語法內的工作表,可否用變數代替? [打印本頁]

作者: newsoft    時間: 2018-5-28 08:46     標題: 請教VBA SQL語法內的工作表,可否用變數代替?

Set myCon = CreateObject("ADODB.Connection")
    myCon.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & ThisWorkbook.FullName
    Sql = "SELECT * FROM [His$]"

His 是工作表名稱, His 能否用變數代替?
感謝指教...
作者: GBKEE    時間: 2018-6-19 08:37

回復 1# newsoft
  1. Option Explicit
  2. Sub Ex()
  3.     Dim myCon As Object, Sql As String, Sh_Name As String
  4.     Set myCon = CreateObject("ADODB.Connection")
  5.     myCon.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & ThisWorkbook.FullName
  6.     Sh_Name = ThisWorkbook.Sheets(1).Name  '
  7.     Sql = "SELECT * FROM [" & Sh_Name & "$]"

  8. End Sub
複製代碼





歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)