2017-06-15 75 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 
相關問題