473,322 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Method 'Cells' of Object' _Global' failed ACCESS to EXCEL with VBA

hi...
i have an access application in which i need to open an existing excel sheet, find a date (already in the sheet) and populate a row (with the date cell column) with either 1 or 0

this means i have to convert the datecell.column to the alphabet equivalent before i can set a cell to be populated.

I used a function, which i have used in excel before (as seen below)

Expand|Select|Wrap|Line Numbers
  1. Function Col_Letter(lngcol) As String
  2. Dim vArr
  3. vArr = Split(Cells(1, lngcol).Address(True, False), "$")
  4. Col_Letter = vArr(0)
  5. End Function
  6.  
when called, this runs fine on first run, gives an error (Method 'Cells' of Object' _Global' failed) the second run, runs fine the third run, and gives same error the fourth run...etc

i have tried more explicit methods of referring to the cell... seeing as i am working with excel from assess, but non has worked.
i tried


Expand|Select|Wrap|Line Numbers
  1. Function Col_Letter(lngcol) As String
  2. Dim vArr
  3. With Worksheets("August2013")
  4. vArr = Split(Cells(1, lngcol).Address(True, False), "$")
  5. Col_Letter = vArr(0)
  6. End With
  7. End Function
  8.  
Expand|Select|Wrap|Line Numbers
  1. Function Col_Letter(lngcol) As String
  2. Dim vArr
  3. With Worksheets(1)
  4. vArr = Split(Cells(1, lngcol).Address(True, False), "$")
  5. Col_Letter = vArr(0)
  6. End With
  7. End Function  
Expand|Select|Wrap|Line Numbers
  1. Function Col_Letter(lngcol) As String
  2. Dim vArr
  3. With Sheets(1)
  4. vArr = Split(Cells(1, lngcol).Address(True, False), "$")
  5. Col_Letter = vArr(0)
  6. End With
  7. End Function
  8.  
Expand|Select|Wrap|Line Numbers
  1. Function Col_Letter(lngcol) As String
  2. Dim vArr
  3. vArr = Split(Sheets(1).Cells(1, lngcol).Address(True, False), "$")
  4. Col_Letter = vArr(0)
  5. End Function
  6.  
(these act in a similar way to the first issue)


Expand|Select|Wrap|Line Numbers
  1. Function Col_Letter(lngcol) As String
  2. Dim vArr
  3. Dim app As New Excel.Application
  4. app.Visible = True
  5. Dim Book As Excel.Workbook
  6. Set Book = app.Workbooks.Add("D:\...\timesheeteng.xlsm")
  7. With Book.Worksheets("August2013")
  8. vArr = Split(Cells(1, lngcol).Address(True, False), "$")
  9. Col_Letter = vArr(0)
  10. End With
  11. End Function
  12.  
(this returns an object required error)

Any idea what i doing wrong?
Feb 11 '14 #1
4 6461
MikeTheBike
639 Expert 512MB
Hi

I cannot simulate you error, as this works
Expand|Select|Wrap|Line Numbers
  1. Function Col_Letter(lngcol) As String
  2.     Dim vArr() As String
  3.     vArr = Split(Cells(1, lngcol).Address(True, False), "$")
  4.     Col_Letter = vArr(0)
  5. End Function
  6.  
  7. Sub test()
  8.     Dim i As Integer
  9.     For i = 25 To 30
  10.         MsgBox Col_Letter(i)
  11.     Next i
  12. End Sub
However, I would like to know why you need to find out what the column letter is, as you do not need to know this to write info to that coulmn when you have the index already??


MTB
Feb 11 '14 #2
zmbd
5,501 Expert Mod 4TB
Expand|Select|Wrap|Line Numbers
  1. Set Book = app.Workbooks.Add("D:\...\timesheeteng.xlsm") 
Is malformed.
"\...\" should be the full path such as
"D:\username\usersubfolder1\usersubfolder2\timeshe eteng.xlsm"

Expand|Select|Wrap|Line Numbers
  1. app.Workbooks.Add
Normally, I would not use this method, change "add" to "open"

Also You should look at the following insights article on applicaition automation.


In fact I borrowed the code from there and another source for the following to transfer a query I use from time to time, you should be able to modify this to your application.

You can of course modify this for latebinding.

Expand|Select|Wrap|Line Numbers
  1. Sub zj_excel_query2sheet_2()
  2. '
  3. 'You must set a reference to the EXCEL Library for the following to work
  4. '
  5.     Dim xlApp As Excel.Application
  6.     Dim xlWB As Excel.Workbook
  7.     Dim xlWS As Excel.Worksheet
  8.     Dim acRng As Variant
  9.     Dim xlRow As Integer
  10.     Dim qry As QueryDef
  11.     Dim rst As Recordset
  12.     Set xlApp = New Excel.Application
  13.     Set xlWB = xlApp.Workbooks.Open("C:\Documents and Settings\All Users\Workbook1.xlsx")
  14.     Set xlWS = xlWB.Worksheets("BBData")
  15.  
  16.     'first empty cell in the worksheet.
  17.     ' or hardcode to example xlRow = 3 to always startin the third row...
  18.     xlRow = (xlWS.Columns("A").End(xlDown).row)
  19.  
  20.     Set qry = CurrentDb.QueryDefs("QueryA")
  21.     Set rst = qry.OpenRecordset
  22.  
  23.     Dim c As Integer
  24.     'column 1 = a
  25.     c = 1
  26.     xlRow = xlRow + 1
  27.  
  28.     Do Until rst.EOF
  29.         For Each acRng In rst.Fields
  30.             'starting in the first empty cell in the column indicated....
  31.             'index with the loop.
  32.             xlWS.Cells(xlRow, c).Formula = acRng
  33.             c = c + 1
  34.         Next acRng
  35.  
  36.         xlRow = xlRow + 1
  37.         c = 1
  38.         rst.MoveNext
  39.         If xlRow > 25 Then GoTo rq_Exit
  40.     Loop
  41.  
  42. rq_Exit:
  43.     rst.Close
  44.     Set rst = Nothing
  45.     Set xlWS = Nothing
  46.     xlWB.Close acSaveYes
  47.     Set xlWB = Nothing
  48.     xlApp.Quit
  49.     Set xlApp = Nothing
  50.     Exit Function
  51. End Sub
Feb 11 '14 #3
NeoPa
32,556 Expert Mod 16PB
The code is good, but it suffers from being run from Access. See Application Automation for how and why this happens. It's basically down to the fact that each application has a set of defaults. Thus, Cells in Excel refers automatically to Excel.Application.ActiveSheet.Cells. When referring from foreign applications this must be stated explicitly.

Your last posted attempt was nearer, but assuming the code up to line #7 works as expected you still have a problem in that you have used With ... on line #7 but when you reference Cells on line #8 you do so without the ".". It should say .Cells(... instead.
Feb 13 '14 #4
ADezii
8,834 Expert 8TB
Why not use the Range Object instead? The following Code will:
  1. Open an Excel Spreadsheet via Automation.
  2. Loop through every Cell in a specified Range.
  3. Check the Cell Value for a valid Date.
  4. If the Date is valid, print the Cell's Address along with the Column designator.
    Expand|Select|Wrap|Line Numbers
    1. Dim appExcel As New Excel.Application
    2. Dim wb As Excel.Workbook
    3. Dim ws As Excel.Worksheet
    4. Dim rng1 As Excel.Range
    5. Dim rng2 As Excel.Range
    6.  
    7. appExcel.Visible = False
    8.  
    9. Set wb = appExcel.Workbooks.Open("C:\Security\Test2.xlsm")
    10. Set ws = wb.Worksheets("Sheet1")
    11.  
    12. ws.Activate
    13.  
    14. Set rng1 = ws.Range("A1:CC100")
    15.  
    16. For Each rng2 In rng1
    17.   If IsDate(rng2.Value) Then
    18.     Debug.Print rng2.Address, Split(rng2.Address, "$")(1)
    19.   End If
    20. Next
    21.  
    22. appExcel.Quit
    23.  
  5. OUTPUT
    Expand|Select|Wrap|Line Numbers
    1. $D$1          D
    2. $D$2          D
    3. $D$3          D
    4. $CA$7         CA
    5. $B$12         B
    6. $AG$12        AG
    7. $AW$14        AW
    8. $H$24         H
    9. $K$28         K
    10. $U$44         U
    11. $E$48         E
    12. $AB$56        AB
    13. $BC$75        BC
    14. $O$78         O
    15. $L$90         L
    16. $T$90         T
    17. $AB$94        AB
    18. $X$100        X
    19.  
  6. The rest should be easy to figure out.
Feb 14 '14 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Peter | last post by:
Hello Thanks for reviewing my question. I am writing a shared Office AddIn that uses a couple of user controls on a form and from time to time I receive this error "COM Interop registration...
1
by: richilli | last post by:
Hi Any help on this would be appreciated cos its driving me insane. I have a function in VB.NET that takes in an excel range and tries to delete rows where the first column starts with a...
3
by: James Wong | last post by:
Dear all, I have an old VB6 application which can create and access Excel object. The basic definition statements are as follows: Dim appExcel As Object Dim wkb1 As Excel.Workbook Dim wks1...
2
by: Lauren Wilson | last post by:
Hi folks, I have an Access 2003 app that works perfectly on my computer. However, when we install it on OTHER computers that ALSO have access 2003 we get an error on startup: Error:...
2
by: Reginald Bal | last post by:
Hello, I created a main form with 2 subforms. In an attempt to move to the next record (or new record) on the main form I get the error " Method 'requery' of object '_Subform' failed". The...
1
by: Socko | last post by:
I'm trying to fix an sub routine in an VB module that basically reads in a MS database and writes it to an Excel Spread sheet. It works just fine except that the data isn't sorted correctly. I have...
4
by: ielamrani | last post by:
Hi, I am getting this error when I try to export to an excel sheet. When I click on a button to export the first time it's fine, I rename the exported excel sheet and I try to export it again and I...
0
by: CoreyReynolds | last post by:
Hey all, I have a piece of code that dumps a bunch of data into a spreadsheet. Also rearranges it into a pivot table and then graphs the pivot table as well so my boss can get a clear view of the...
4
by: fnemo | last post by:
I'm getting the error - Method 'Item' of object 'Forms' failed . Earlier this error was not occuring. In the below code, first textboxes are created dynamically in the form "display_result"....
3
by: Hema Suresh | last post by:
Hi all I created a database via VB and saved it in excel sheet I have 10 command buttons and 10 text box controls on the vb form and i coded in the way to get the data from the user once the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.