2017-06-15 74 views
0

我在这里搜索论坛,我似乎无法得到此代码的工作。从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 谢谢大家!

+0

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

+0

@ YowE3K谢谢这个小小姐,是正确的答案谢谢!!!! –

回答

2

使用

ws.Range("Client").Value = "Test" 

或者

Dim sName as String 
sName = "Client" 

ws.Range(sName).Value = "Test" 

原因是是,你有ws对象已经确定,所以没有必要再指派血统吧。实际上,试图这样做会破坏语法规则。

+0

谢谢我考虑到你的答案,但它确实是一个错过了宣言。谢谢 –

0

FWIW(不是你的问题 - 这是斯科特的回答解决):请注意, Set ws = xlapp.Worksheets("CountSheet") 应 Set ws = wb.Worksheets("CountSheet")

使用xlapp.Worksheets("CountSheet") 实际上是xlApp.ActiveWorkbook.Worksheets("CountSheet")这可能是(也许是)xlApp.Workbooks("VISION - EXCEL FILES\VISIONCOUNT.xlsm").Worksheets("CountSheet")但最好是做正确的,而不是把它留给机会。

谢谢你们!

0

这应该做你想做的。

Sub DAOFromExcelToAccess() 
' exports data from the active worksheet to a table in an Access database 
' this procedure must be edited before use 
Dim db As Database, rs As Recordset, r As Long 
    Set db = OpenDatabase("C:\FolderName\DataBaseName.mdb") 
    ' open the database 
    Set rs = db.OpenRecordset("TableName", dbOpenTable) 
    ' get all records in a table 
    r = 3 ' the start row in the worksheet 
    Do While Len(Range("A" & r).Formula) > 0 
    ' repeat until first empty cell in column A 
     With rs 
      .AddNew ' create a new record 
      ' add values to each field in the record 
      .Fields("FieldName1") = Range("NamedRange1").Value 
      .Fields("FieldName2") = Range("NamedRange2").Value 
      .Fields("FieldNameN") = Range("NamedRangeN").Value 
      ' add more fields if necessary... 
      .Update ' stores the new record 
     End With 
     r = r + 1 ' next row 
    Loop 
    rs.Close 
    Set rs = Nothing 
    db.Close 
    Set db = Nothing 
End Sub 
相关问题