我在這裏搜索論壇,我似乎無法得到此代碼的工作。從Excel中寫入Excel中的命名單元格
我正試圖在Excel中打開工作簿,然後填充一些單元格(命名範圍)。我可以成功打開工作簿(工作簿中有一小部分VBA在打開時也運行,只是格式化的東西),但是當我開始輸入信息時,出現'運行時錯誤'438「對象不支持這個屬性或方法。「
從以前對其他類似問題的回答中,我已經完成了所有建議的方式,但是我似乎無法使其工作。
Option Compare Database
Option Explicit
Public Sub MaterialInput()
Dim xlapp As Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Dim RsClient As Recordset
Dim RsJobsite As Recordset
Dim db As Database
Dim ClientSTR As String
Dim JobsiteSTR As String
Dim customer As Variant
Set db = CurrentDb
JobsiteSTR = "SELECT T1Jobsites.JobsiteNickName FROM T1Jobsites WHERE T1Jobsites.JobsiteID = 1" ' & Form_LEM.TxtJobsiteID
Set RsJobsite = db.OpenRecordset(JobsiteSTR, dbOpenSnapshot, dbSeeChanges)
ClientSTR = "SELECT T1Companies.CompanyName " & _
"FROM T1Companies INNER JOIN T1Jobsites ON T1Companies.CompanyID = T1Jobsites.CompanyId " & _
"WHERE (((T1Jobsites.JobsiteID)=1))"
'ClientSTR = "SELECT T1Companies.CompanyName FROM T1Companies INNER JOIN T1Jobsites ON T1Companies.CompanyID = T1Jobsites.CompanyID" & _
" WHERE T1JobsitesID = 1" '& Form_LEM.TxtJobsiteID
Set RsClient = db.OpenRecordset(ClientSTR, dbOpenSnapshot, dbSeeChanges)
Set xlapp = CreateObject("excel.application")
Set wb = xlapp.Workbooks.Open("C:\Users\coc33713\Desktop\VISION - EXCEL FILES\VISIONCOUNT.xlsm")
Set ws = xlapp.Worksheets("CountSheet")
xlapp.Visible = True
'Tried this second after reading another forum
'the comments Recordset will be the actual values used, but I can't get the String "TEST" to work
wb.ws.Range("Client").Value = "TEST" 'RsClient!CompanyName
'Tried this way first
xlapp.ws.Range("'SiteName'").Value = "Test" 'RsJobsite!JobsiteNickName"
xlapp.ws.Range(Date).Value = "Test" 'Form_LEM.TxtDate
xlapp.ws.Range(ProjectName).Value = "Test" 'Form_LEM.TxtPlant
xlapp.ws.Range(ScaffoldID).Value = "Test" 'Form_LEM.cboScaffnum.Value
xlapp.ws.Range(ScaffoldNumber).Value = "Test" 'Form_LEM.cboScaffnum.Column(1)
Set xlapp = Nothing
Set wb = Nothing
Set ws = Nothing
Set RsClient = Nothing
Set RsJobsite = Nothing
Set db = Nothing
End Sub
As a Sidenote this is not a form it is just spreadsheet 謝謝大家!
FWIW(不是你的問題 - 這是斯科特的回答解決):請注意,'設置WS = xlapp.Worksheets( 「CountSheet」)'要'設置WS = wb.Worksheets( 「CountSheet」)' 。使用'xlapp.Worksheets(「CountSheet」)''實際上是'xlApp.ActiveWorkbook.Worksheets(「CountSheet」)'**可能**(可能是)'xlApp.Workbooks(「VISION - EXCEL FILES \ VISIONCOUNT。 xlsm「)。工作表(」CountSheet「)',但最好是正確地做,而不是讓它失去機會。 – YowE3K
@ YowE3K謝謝這個小小姐,是正確的答案謝謝!!!! –