473,666 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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 6523
MikeTheBike
639 Recognized Expert Contributor
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 Recognized Expert Moderator Expert
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\us ersubfolder1\us ersubfolder2\ti mesheeteng.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,568 Recognized Expert Moderator MVP
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.Applicati on.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 Recognized Expert Expert
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
4615
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 failed. Access is denied." The only way I have found around this is exit VS.NET and restart it. When debugging, I have VS.NET launch Excel in order to load my shared AddIn. Any clues what is causing this? Is Excel not shutdown Many Thank Peter
1
6258
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 string. Only it doesnt work and all i get is "Delete method of Range class failed" whatever i do. Any suggestions?
3
10725
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 As Excel.Worksheet Set appExcel = New
2
4722
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: -2147467259 Method 'Visible' of object 'ComandBar' failed! At this point, I am stumped. Anyone have a clue why this is happening?
2
6060
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 database closes after answering the popup window (Microsoft Access has encounterd a problem and needs to close. .....) Does anybody has an idea why I'm having this problem?
1
5099
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 no experience at all in excel or the vb code to access excel. I found a few lines of code by searching on how to sort in vb for excel and the code with the new sort logic works fine the first time you run it, but run it twice and you get the titled...
4
15480
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 get the error: Runtime Error 1004: Method ‘Cells’ of Object ‘_Global’ failed It highlight this line: Range("A1:L1").Select sorry the code behind the button is long: . Dim stDocName As String stDocName = "QFinal4"
0
3424
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 data. This question is two part. One, I seem to be getting the title error at the starting line stating: oSheet.Range(rng).Select With Selection shp.Left = .Left shp.Top = .Top
4
5894
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". Design view of form is closed. Then same form is opened in normal view. Form is loaded. In the form load, values are passed into textboxes from global variables.Then values are passed into dynamically created textboxes from database. During this...
3
1812
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 user entered the data, i coded in vb to pass all the vaues to an excel sheet and to save it when i executed the vb application, as i designed, it asks for the data from the user
0
8444
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7386
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.