我在这里搜索论坛,我似乎无法得到此代码的工作。从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谢谢这个小小姐,是正确的答案谢谢!!!! –