473,445 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Need Access VBA similar to the given Excel VBA

126 100+
Hi, the following Excel VBA code is used to select 5 rows by double clicking the cell in the first column corresponding to that row. The code is working fine in excel.

But, I need Access VBA code, which opens the excel file and performs the same function.

Can anyone suggest me how to do this? Thanks in advance!

Expand|Select|Wrap|Line Numbers
  1. Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
  2. Dim ColRange As String, PoliciesCount As Integer, LastUsedRow As Long
  3. If Target.Count > 1 Then Exit Sub
  4.  
  5. If WorksheetFunction.CountA(Cells) > 0 Then
  6. LastUsedRow = Cells.Find(What:="*", After:=[A1], _
  7.         SearchOrder:=xlByRows, _
  8.         SearchDirection:=xlPrevious).Row
  9. End If
  10.  
  11. ColRange = "A2:A" & LastUsedRow
  12. If Intersect(Target, Range(ColRange)) Is Nothing Then Exit Sub
  13. Target.Font.Name = "marlett"
  14.  
  15. If Target.Value <> "a" Then
  16. PoliciesCount = Columns(1).SpecialCells(xlCellTypeConstants, 23).Cells.Count
  17.     If PoliciesCount > 5 Then
  18.         MsgBox "You cannot select more than 5 rows", vbOKOnly Or vbCritical, "Warning!"
  19.         Exit Sub
  20.     End If
  21.     Target.Value = "a"
  22.     Cancel = True
  23.     Exit Sub
  24. End If
  25.  
  26. If Target.Value = "a" Then
  27.     Target.ClearContents
  28.     Cancel = True
  29.     Exit Sub
  30. End If
  31. End Sub
Jul 18 '08
76 7207
puppydogbuddy
1,923 Expert 1GB
Ok, got it. Add the Do Loop as shown:

Do until Target.Value = "a"
'Worksheet_SelectionChange Target
Worksheet_Activate
Loop
Jul 24 '08 #51
JFKJr
126 100+
Ok, got it. Add the Do Loop as shown:

Do until Target.Value = "a"
'Worksheet_SelectionChange Target
Worksheet_Activate
Loop
I replaced the code as above and it is giving me same "Object Required error" at line #15:

Expand|Select|Wrap|Line Numbers
  1. Sub RunMacro()
  2. Dim xlApp As Excel.Application
  3. Dim xlWB As Excel.Workbook
  4. Set xlApp = New Excel.Application
  5.  
  6. With xlApp
  7. .Visible = True
  8. .Interactive = True
  9. Set xlWB = .Workbooks.Open("C:\ExtractPolicyList2.xls", , False)
  10. xlWB.Worksheets(1).Activate
  11. xlWB.ActiveSheet.Range("a5").Select
  12. MsgBox "" & ActiveCell
  13. End With
  14.  
  15. Do Until Target.Value = "a"
  16. 'Worksheet_SelectionChange Target
  17. Worksheet_Activate
  18. Loop
  19.  
  20. Exit_Routine:
  21. Set xlApp = Nothing
  22. Set xlWB = Nothing
  23. Exit Sub
  24. End Sub
Jul 24 '08 #52
puppydogbuddy
1,923 Expert 1GB
Try changing : Do Until Target.Value = "a"

to: Do Until Target = "a"

or to: Do Until xlWB.Target.Value = "a"

or to: Do Until ActiveCell.Value = "a"
Jul 24 '08 #53
JFKJr
126 100+
Try changing : Do Until Target.Value = "a"

to: Do Until Target = "a"

or to: Do Until xlWB.Target.Value = "a"

or to: Do Until ActiveCell.Value = "a"
I replaced the "Do Until" loop with the following code
Expand|Select|Wrap|Line Numbers
  1. Do Until Target = "a"
  2. Worksheet_SelectionChange Target
  3. Loop
=> generates "Object Required" error at line #1
Expand|Select|Wrap|Line Numbers
  1. Do Until xlWB.Target.Value = "a"
  2. Worksheet_SelectionChange Target
  3. Loop
=> generates "Run-time error '438': Object doesn't support this property or method" error at line#1
Expand|Select|Wrap|Line Numbers
  1. Do Until ActiveCell.Value = "a"
  2. Worksheet_SelectionChange Target
  3. Loop
=> generates "Object Required" error at line #2
Expand|Select|Wrap|Line Numbers
  1. Do Until ActiveCell.Value = "a"
  2. Worksheet_SelectionChange ActiveCell
  3. Loop
=>calls "Worksheet_SelectionChange" event only one time when I run "RunMacro()" proc and coming out of the loop.
Jul 25 '08 #54
puppydogbuddy
1,923 Expert 1GB
Try:

Do Until xlWB.Worksheets(1).Target.Value ="a"
Worksheet_SelectionChange Target
Loop
Jul 25 '08 #55
JFKJr
126 100+
Try:

Do Until xlWB.Worksheets(1).Target.Value ="a"
Worksheet_SelectionChange Target
Loop
The above code is generating "Run-time error '438': Object doesn't support this property or method" error at line#1.
Jul 25 '08 #56
puppydogbuddy
1,923 Expert 1GB
#$%^&*$@ (just kidding!)

try changing this:
Do Until xlWB.Worksheets(1).Target.Value ="a"


To:
Do Until xlWB.Worksheets(1).ActiveCell.Value ="a"
Jul 25 '08 #57
JFKJr
126 100+
#$%^&*$@ (just kidding!)

try changing this:
Do Until xlWB.Worksheets(1).Target.Value ="a"


To:
Do Until xlWB.Worksheets(1).ActiveCell.Value ="a"
It is generating same "Run-time error '438': Object doesn't support this property or method" error.

I have searched internet a loooooooooot but unable to find anything on "handling excel events in access VBA" :(. And, now I am doubting that does anyone ever faced this problem before or I am the only person in the earth who is having this kind of ugly problem :(
Jul 25 '08 #58
puppydogbuddy
1,923 Expert 1GB
It is generating same "Run-time error '438': Object doesn't support this property or method" error.

I have searched internet a loooooooooot but unable to find anything on handling excel events in access VBA :(. And, now I am doubting that does anyone faced this problem before or I am the only person in the earth who is having this kind of ugly problem :(
I have used excel vba before and never had this problem, but I did not have a click on cell type of app either. Were you able to run the complete tutorial app that I sent you.(Kaleidescope) ? That would be a good test.

If the code below does not work, its time to ask Mr. Excel!!!

http://www.mrexcel.com/archive/VBA/index.html

Do Until ActiveWorksheet.Cells.Range(Target) = "a"

or

Do Until ActiveWorksheet.Cells(ActiveCell.Value) = "a"
Jul 25 '08 #59
JFKJr
126 100+
I have used excel vba before and never had this problem, but I did not have a click on cell type of app either. Were you able to run the complete tutorial app that I sent you.(Kaleidescope) ? That would be a good test.

If the code below does not work, its time to ask Mr. Excel!!!

http://www.mrexcel.com/archive/VBA/index.html

Do Until ActiveWorksheet.Cells.Range(Target) = "a"

or

Do Until ActiveWorksheet.Cells(ActiveCell.Value) = "a"
I am not sure what is Kaleidescope means, but yes I was able to run the complete code in the tutorial you sent -
http://pubs.logicalexpressions.com/P...cle.asp?ID=407

As I said before, with the code in the tutorial I was able to run the events in excel VBA whenever the action corresponding to it happens. But in contrary to excel, the access VBA is unable to run events whenever the action corresponding to it happens.

And regarding the above code, it is giving again the same "Object Required" error for both cases.

Anyways, thanks for all your patience, time and help :) Thank You very much.

I will try to contact Mr. Excel and see how it goes...

Thanks a lot!
Jul 25 '08 #60
puppydogbuddy
1,923 Expert 1GB
I am not giving up yet....I am going to research it on my own and get back to you if I find anything. The different shapes and colors generated by the tutorial create a Kaleidescope.
Jul 25 '08 #61
JFKJr
126 100+
I am not giving up yet....I am going to research it on my own and get back to you if I find anything. The different shapes and colors generated by the tutorial create a Kaleidescope.
Yes, I have even tested Kaleidoscope in the tutorial and thanks a lot for all the help you did and for continuing your research on the problem.

I will keep in touch with the post and will update the status if any progress!

Thanks!
Jul 25 '08 #62
puppydogbuddy
1,923 Expert 1GB
I think this may be part of the answer...the SelectionChange event is not activated when the change occurs with vba code, only by selection. So try changing the event to the worksheetChange event instead.

Change this:
Expand|Select|Wrap|Line Numbers
  1. Do Until ActiveCell.Value = "a"
  2. Worksheet_SelectionChange Target
  3. Loop
to this:
Expand|Select|Wrap|Line Numbers
  1. Do Until ActiveCell.Value = "a"
  2. Worksheet_Change Target
  3. Loop
Don't for to change the name of the Worksheet_SelectionChange sub to Worksheet_Change as well.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2.         Application.EnableEvents = False
  3.         Worksheet_BeforeDoubleClick Target.Value           
  4.         Target.Value = Target.Value + 1                       'may need to comment this line out         
  5.         Application.EnableEvents = True
  6. End Sub

Hope this helps.....let me know.
Jul 26 '08 #63
JFKJr
126 100+
I think this may be part of the answer...the SelectionChange event is not activated when the change occurs with vba code, only by selection. So try changing the event to the worksheetChange event instead.

Change this:
Expand|Select|Wrap|Line Numbers
  1. Do Until ActiveCell.Value = "a"
  2. Worksheet_SelectionChange Target
  3. Loop
to this:
Expand|Select|Wrap|Line Numbers
  1. Do Until ActiveCell.Value = "a"
  2. Worksheet_Change Target
  3. Loop
Don't for to change the name of the Worksheet_SelectionChange sub to Worksheet_Change as well.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2.         Application.EnableEvents = False
  3.         Worksheet_BeforeDoubleClick Target.Value           
  4.         Target.Value = Target.Value + 1                       'may need to comment this line out         
  5.         Application.EnableEvents = True
  6. End Sub

Hope this helps.....let me know.
Hello puppydogbuddy, sorry for the late response..

The following is the code with the above changes and it is generating "Object Required" error at line #14.
Expand|Select|Wrap|Line Numbers
  1. Sub RunMacro()
  2. Dim xlApp As Excel.Application
  3. Dim xlWB As Excel.Workbook
  4. Set xlApp = New Excel.Application
  5.  
  6. With xlApp
  7. .Visible = True
  8. .Interactive = True
  9. Set xlWB = .Workbooks.Open("C:\ExtractPolicyList2.xls", , False)
  10. xlWB.Worksheets(1).Activate
  11. xlWB.ActiveSheet.Range("a5").Select
  12. MsgBox "" & ActiveCell
  13. Do Until ActiveCell.Value = "a"
  14. Worksheet_Change Target
  15. Loop
  16. End With
  17. Exit_Routine:
  18. Set xlApp = Nothing
  19. Set xlWB = Nothing
  20. Exit Sub
  21. End Sub
Expand|Select|Wrap|Line Numbers
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2. Application.EnableEvents = False
  3. Worksheet_BeforeDoubleClick Target, False
  4. 'Target.Value = Target.Value + 1   'may need to comment this lineout
  5. Application.EnableEvents = True
  6. End Sub
So, I changed the above 14th line to the following line and it is giving "Method or Data Member not found" error at line #2 in "Worksheet_Change" proc.
Expand|Select|Wrap|Line Numbers
  1. Worksheet_Change ActiveCell
I also tried the following code by changing a little bit and the code is selecting the "A5" cell and coming out of the loop. The code is not automatically calling "Worksheet_Change" proc, which in turn calls "Worksheet_BeforeDoubleClick " proc, whenever I double click a cell.

Expand|Select|Wrap|Line Numbers
  1. Sub RunMacro()
  2. Dim xlApp As Excel.Application
  3. Dim xlWB As Excel.Workbook
  4. Set xlApp = New Excel.Application
  5.  
  6. With xlApp
  7. .Visible = True
  8. .Interactive = True
  9. Set xlWB = .Workbooks.Open("C:\Users\Harika\Documents\ExtractPolicyList2.xls", , False)
  10. xlWB.Worksheets(1).Activate
  11. xlWB.ActiveSheet.Range("a5").Select
  12. MsgBox "" & ActiveCell
  13. Do Until ActiveCell.Value = "a"
  14. xlWB.Application.EnableEvents = False
  15. Worksheet_Change ActiveCell
  16. xlWB.Application.EnableEvents = True
  17. Loop
  18. End With
  19. Exit_Routine:
  20. Set xlApp = Nothing
  21. Set xlWB = Nothing
  22. Exit Sub
  23. End Sub
Expand|Select|Wrap|Line Numbers
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2. Worksheet_BeforeDoubleClick Target, False
  3. 'Target.Value = Target.Value + 1   'may need to comment this line out
  4. End Sub
FYI, I submitted a post in Mr. Excel and somebody gave me a suggestion to include "Do Events".
Please kindly have a look at the post:
http://www.mrexcel.com/forum/showthread.php?p=1636778
Jul 26 '08 #64
Stewart Ross
2,545 Expert Mod 2GB
Hi again guys; sorry you are having such a struggle with this one. If we could make an award for dogged persistence you would both win it! Thanks PDog for all your assistance with this post (a thread that has become an epic!) and JFKjr for your patience despite what must now be great frustration.

In my opinion it might be better to rethink what it is you want to achieve for your users, then implement the effects (column width changes and so on) directly in the Access VBA code instead of trying to call Excel event handlers. It is no problem to change column widths, fonts, and so on in code from Access.

The Mr Excel reply points up the use of Do Events - but this can only be part of the answer. For Access to respond to Excel events you will need to declare the Excel object using the WithEvents qualifier so that Access can 'listen' for the events raised by Excel. I think you would still need to replicate the event code in Access for the worksheet change event for instance. I haven't used Excel events myself; I have however devised bespoke event handlers in Access for class modules I developed. I found that setting the applications up to send and listen to events was more challenging than actually responding to the events themselves, which is beautifully simple once the event sender and handlers are set up and listening to each other...

Your object errors that have arisen again and again result as previously mentioned from lack of qualification of the application object. The event errors seem to me to be occurring because the application is not yet set to listen to the events.

An extract from the Excel help file is listed below.

Personally, I would step back and consider why you need to respond to worksheet events at all; if you simply need to format a range of cells dynamically why not do so directly from Access??

-Stewart
Using Events with the Application Object

Before you can use events with the Application object, you must create a new class module and declare an object of type Application with events. For example, assume that a new class module is created and called EventClassModule. The new class module contains the following code.

Public WithEvents App As Application

After the new object has been declared with events, it appears in the Object drop-down list box in the class module, and you can write event procedures for the new object. (When you select the new object in the Object box, the valid events for that object are listed in the Procedure drop-down list box.)

Before the procedures will run, however, you must connect the declared object in the class module with the Application object. You can do this with the following code from any module.

Dim X As New EventClassModuleSub InitializeApp()
Set X.App = Application
End Sub

After you run the InitializeApp procedure, the App object in the class module points to the Microsoft Excel Application object, and the event procedures in the class module will run when the events occur.
Jul 27 '08 #65
puppydogbuddy
1,923 Expert 1GB
Stewart,
Thanks to the info you provided, I think we have the basis for a solution. There is more than one way to skin a cat, and I finally found an authoritative source on how to make this work with the Do Until Loop (instead of event handlers), I suggested earlier, but did not know there was a special way to code it in vba.

See this link(excerpted below):
http://support.microsoft.com/default...b;en-us;141762

Expand|Select|Wrap|Line Numbers
  1.  ' Performs Do loop, testing at top of loop.
  2.    ' Loops until empty cell is reached.
  3.    ' Note that you can also use a Do While Not IsEmpty(ActiveCell) loop.
  4.  
  5.    Sub test_before_do_loop()
  6.  
  7.       ' Test contents of active cell; if active cell is empty, exit loop.
  8.       Do Until IsEmpty(ActiveCell)
  9.  
  10.          ' Displays cell contents in message box.
  11.          MsgBox ActiveCell.Value
  12.  
  13.          ' Step down 1 row to the next cell.
  14.          ActiveCell.Offset(1, 0).Select
  15.  
  16.         ' Return to top of loop.
  17.       Loop
  18.    End Sub
Jul 27 '08 #66
puppydogbuddy
1,923 Expert 1GB
Based on my interpretation of the ms kb article, this should work:
Expand|Select|Wrap|Line Numbers
  1. Sub RunMacro()
  2. Dim xlApp As Excel.Application
  3. Dim xlWB As Excel.Workbook
  4. Set xlApp = New Excel.Application
  5.  
  6. With xlApp
  7. .Visible = True
  8. .Interactive = True
  9. Set xlWB = .Workbooks.Open("C:\ExtractPolicyList.xls", , False)                                 
  10. xlWB.Worksheets(1).Activate
  11. xlWB.ActiveSheet.Range("a5").Select
  12. ' Performs Do loop, testing at top of loop.
  13.  
  14.        ' Test contents of active cell;
  15.       Do Until ActiveCell.Value = "a"
  16.  
  17.          ' Displays cell contents in message box.
  18.          MsgBox ActiveCell.Value
  19.  
  20.          Worksheet_BeforeDoubleClick ActiveCell, False
  21.  
  22.          ' Step down 1 row to the next cell.
  23.        '  ActiveCell.Offset(1, 0).Select ..............commented out for now<<<<<
  24.  
  25.         ' Return to top of loop.
  26.       Loop
  27.    End Sub
  28.  
  29. End With
  30.  
  31. Exit_Routine:
  32. Set xlApp = Nothing
  33. Set xlWB = Nothing
  34. Exit Sub
  35. End Sub
Jul 27 '08 #67
puppydogbuddy
1,923 Expert 1GB
If there is still an interest vba event handlers for Excel, I found this authoritative reference on how to enable, fire, and disable event handlers using vba.

http://support.microsoft.com/default...b;en-us;165867
Jul 27 '08 #68
JFKJr
126 100+
If there is still an interest vba event handlers for Excel, I found this authoritative reference on how to enable, fire, and disable event handlers using vba.

http://support.microsoft.com/default...b;en-us;165867
Hello Puppydogbuddy and Stewart, first of all thanks a loooooooooooot for all your help and ideas on the post.

I am successfully able to make the code working by including "WithEvents", which handles excel events in Access.

The following are the steps I followed to open an excel file from Access VBA and call "BeforeDoubleClick" excel event whenever a user double clicks on a cell:

1. Created a new class module called "clsExcel" and defined the excel events, which I would like to use with the excel.

Expand|Select|Wrap|Line Numbers
  1. Public WithEvents AppExcel As Excel.Application
  2. Public WithEvents AppWB As Excel.Workbook
  3. Public WithEvents AppWS As Excel.Worksheet
  4.  
  5. Private Sub AppExcel_WorkbookBeforeClose(ByVal Wb As Excel.Workbook, Cancel As Boolean)
  6. With AppExcel.Workbooks
  7. AppExcel.Quit
  8. Set clsExcelApp.AppExcel = Nothing
  9. Set clsExcelApp.AppWB = Nothing
  10. Set clsExcelApp.AppWS = Nothing
  11. Set clsExcelApp = Nothing
  12. End With
  13. End Sub
  14.  
  15. Private Sub AppWS_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)
  16. Dim ColRange As String, LastUsedRow As Long, PoliciesCount As Integer
  17. With AppWS
  18. If Target.Count > 1 Then Exit Sub
  19.  
  20. If WorksheetFunction.CountA(Cells) > 0 Then
  21.    LastUsedRow = Cells.Find(What:="*", After:=[A1], _
  22.             SearchOrder:=xlByRows, _
  23.             SearchDirection:=xlPrevious).Row
  24. End If
  25.  
  26. ColRange = "A2:A" & LastUsedRow
  27. If Intersect(Target, Range(ColRange)) Is Nothing Then Exit Sub
  28. Target.Font.Name = "marlett"
  29. If Target.Value <> "a" Then
  30. PoliciesCount = Columns(1).SpecialCells(xlCellTypeConstants, 23).Cells.Count
  31.     If PoliciesCount > 5 Then
  32.         MsgBox "You cannot select more than 5 rows", vbOKOnly Or vbCritical, "Warning!"
  33.         Exit Sub
  34.     End If
  35.     Target.Value = "a"
  36.     Cancel = True
  37.     Exit Sub
  38. End If
  39. If Target.Value = "a" Then
  40.    Target.ClearContents
  41.    Cancel = True
  42.    Exit Sub
  43. End If
  44. End With
  45. End Sub
2. Created a standard module and instantiated the class "clsExcel" created above with a current Application Object:

Expand|Select|Wrap|Line Numbers
  1. Public clsExcelApp As New clsExcel
  2.  
  3. Sub RunMacro()
  4. With clsExcelApp
  5. If .AppExcel Is Nothing Then Set .AppExcel = New Excel.Application
  6. 'show the Application
  7. .AppExcel.Visible = True
  8. .AppExcel.Interactive = True
  9. Set .AppWB = .AppExcel.Workbooks.Open("C:\Users\Harika\Documents\ExtractPolicyList2.xls", , False)
  10. Set .AppWS = .AppWB.Worksheets(1)
  11. End With
  12. End Sub
3. And when I run the "RunMacro()" defined above, the code is opening the excel file and calling "BeforeDoubleClick" event whenever I double click a cell (wonderful, can you believe it!)

Stewart, thanks a lot for the idea on "WithEvents", which is working like a charm in handling excel events with access.

puppydogbuddy, thank you very very much for your most valuable time, ideas and help on the post. I am a new beginner to Access & Excel VBA, but with your ideas and the article links you provided on this post I think I learnt most VBA :). Thank You!

And, I am officially declaring the successfull end of this thread. Cheers :)

Please kindly let me know if any questions. Thanks!
Jul 27 '08 #69
JFKJr
126 100+
Hello Puppydogbuddy and Stewart, first of all thanks a loooooooooooot for all your help and ideas on the post.

I am successfully able to make the code working by including "WithEvents", which handles excel events in Access.

The following are the steps I followed to open an excel file from Access VBA and call "BeforeDoubleClick" excel event whenever a user double clicks on a cell:

1. Created a new class module called "clsExcel" and defined the excel events, which I would like to use with the excel.

Expand|Select|Wrap|Line Numbers
  1. Public WithEvents AppExcel As Excel.Application
  2. Public WithEvents AppWB As Excel.Workbook
  3. Public WithEvents AppWS As Excel.Worksheet
  4.  
  5. Private Sub AppExcel_WorkbookBeforeClose(ByVal Wb As Excel.Workbook, Cancel As Boolean)
  6. With AppExcel.Workbooks
  7. AppExcel.Quit
  8. Set clsExcelApp.AppExcel = Nothing
  9. Set clsExcelApp.AppWB = Nothing
  10. Set clsExcelApp.AppWS = Nothing
  11. Set clsExcelApp = Nothing
  12. End With
  13. End Sub
  14.  
  15. Private Sub AppWS_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)
  16. Dim ColRange As String, LastUsedRow As Long, PoliciesCount As Integer
  17. With AppWS
  18. If Target.Count > 1 Then Exit Sub
  19.  
  20. If WorksheetFunction.CountA(Cells) > 0 Then
  21.    LastUsedRow = Cells.Find(What:="*", After:=[A1], _
  22.             SearchOrder:=xlByRows, _
  23.             SearchDirection:=xlPrevious).Row
  24. End If
  25.  
  26. ColRange = "A2:A" & LastUsedRow
  27. If Intersect(Target, Range(ColRange)) Is Nothing Then Exit Sub
  28. Target.Font.Name = "marlett"
  29. If Target.Value <> "a" Then
  30. PoliciesCount = Columns(1).SpecialCells(xlCellTypeConstants, 23).Cells.Count
  31.     If PoliciesCount > 5 Then
  32.         MsgBox "You cannot select more than 5 rows", vbOKOnly Or vbCritical, "Warning!"
  33.         Exit Sub
  34.     End If
  35.     Target.Value = "a"
  36.     Cancel = True
  37.     Exit Sub
  38. End If
  39. If Target.Value = "a" Then
  40.    Target.ClearContents
  41.    Cancel = True
  42.    Exit Sub
  43. End If
  44. End With
  45. End Sub
2. Created a standard module and instantiated the class "clsExcel" created above with a current Application Object:

Expand|Select|Wrap|Line Numbers
  1. Public clsExcelApp As New clsExcel
  2.  
  3. Sub RunMacro()
  4. With clsExcelApp
  5. If .AppExcel Is Nothing Then Set .AppExcel = New Excel.Application
  6. 'show the Application
  7. .AppExcel.Visible = True
  8. .AppExcel.Interactive = True
  9. Set .AppWB = .AppExcel.Workbooks.Open("C:\Users\Harika\Documents\ExtractPolicyList2.xls", , False)
  10. Set .AppWS = .AppWB.Worksheets(1)
  11. End With
  12. End Sub
3. And when I run the "RunMacro()" defined above, the code is opening the excel file and calling "BeforeDoubleClick" event whenever I double click a cell (wonderful, can you believe it!)

Stewart, thanks a lot for the idea on "WithEvents", which is working like a charm in handling excel events with access.

puppydogbuddy, thank you very very much for your most valuable time, ideas and help on the post. I am a new beginner to Access & Excel VBA, but with your ideas and the article links you provided on this post I think I learnt most VBA :). Thank You!

And, I am officially declaring the successfull end of this thread. Cheers :)

Please kindly let me know if any questions. Thanks!
Hello guys, I am having a small issue when I run the above code, I know it got to do something with the application reference, but I am not sure how to do it.

When I run "RunMacro()" proc above, it is opening an excel file and when I double click on any cell it is calling "AppWS_BeforeDoubleClick" event.

The above code is working fine for the first time and generating "Method 'Cells' of object '_Global' failed" error at line #20 for the second time and so on..(the code is working sometimes and sometimes not).

Please kindly let me know what to do? Thanks!
Jul 29 '08 #70
puppydogbuddy
1,923 Expert 1GB
Hello guys, I am having a small issue when I run the above code, I know it got to do something with the application reference, but I am not sure how to do it.

When I run "RunMacro()" proc above, it is opening an excel file and when I double click on any cell it is calling "AppWS_BeforeDoubleClick" event.

The above code is working fine for the first time and generating "Method 'Cells' of object '_Global' failed" error at line #20 for the second time and so on..(the code is working sometimes and sometimes not).

Please kindly let me know what to do? Thanks!
Try changing this:

CountA(Cells())

To:

CountA(Range("A1:D500")) .......replace A1:D500 with whatever range you want
Jul 29 '08 #71
JFKJr
126 100+
Try changing this:

CountA(Cells())

To:

CountA(Range("A1:D500")) .......replace A1:D500 with whatever range you want
Hello Puppudogbuddy, thanks for the response, nice to see you again :-)

Unfortunately it is giving the same error. Let me simplify my problem by concentrating only on the error occuring code:

The following is the code opens the excel file and displays the last used row in the excel. The code is working fine for the first time and giving "Run-time error '13': Type mismatch" at line #15 during the second time and so on....

The code is working sometimes and sometimes not!

Please kindly let me know what to do?

Expand|Select|Wrap|Line Numbers
  1. Sub RunMacro()
  2. Dim xlApp As Excel.Application
  3. Dim xlWB As Excel.Workbook
  4. Dim xlWS As Excel.Worksheet
  5. Dim LastUsedRow As Long
  6. Set xlApp = New Excel.Application
  7.  
  8. With xlApp
  9. .Visible = True
  10. .Interactive = True
  11. Set xlWB = .Workbooks.Open("C:\ExtractPolicyList.xls", , False)
  12. Set xlWS = xlWB.Worksheets("SelectPolicies")
  13.  
  14. If .WorksheetFunction.CountA(xlWS.Cells) > 0 Then
  15.   LastUsedRow = xlWS.Cells.Find(What:="*", After:=[A1], _
  16.                 SearchOrder:=xlByRows, _
  17.                 SearchDirection:=xlPrevious).Row
  18.   MsgBox "" & LastUsedRow
  19. End If
  20. End With
  21. End Sub
  22.  
Jul 29 '08 #72
puppydogbuddy
1,923 Expert 1GB
Hi JFKJr,

This might be the problem.....the WorkSheetFunction is a method of the application object, so try this"

If Application.WorksheetFunction.CountA(xlWS.Cells) > 0
Jul 29 '08 #73
JFKJr
126 100+
Hi JFKJr,

This might be the problem.....the WorkSheetFunction is a method of the application object, so try this"

If Application.WorksheetFunction.CountA(xlWS.Cells) > 0
Yes, you are right the "WorkSheetFunction" is a method of the application object. That is the reason I used:

"With xlApp" (application object) at line #8 above

And," .WorksheetFunction" at line #14 above (used "." to explicitly refer the method to the "xlApp" application object).

By the way, the error is occuring at line #15 and it is not giving any error for " .WorksheetFunction" at line #14.

The error is occurring definitely because of not explicitly referring the application/cells object. But I am not sure how to solve it.
Jul 29 '08 #74
JFKJr
126 100+
Yes, you are right the "WorkSheetFunction" is a method of the application object. That is the reason I used:

"With xlApp" (application object) at line #8 above

And," .WorksheetFunction" at line #14 above (used "." to explicitly refer the method to the "xlApp" application object).

By the way, the error is occuring at line #15 and it is not giving any error for " .WorksheetFunction" at line #14.

The error is occurring definitely because of not explicitly referring the application/cells object. But I am not sure how to solve it.
I solved the issue, I changed the 15 line above to the following and it worked.

Expand|Select|Wrap|Line Numbers
  1. LastUsedRow = .Cells.Find(what:="*", after:=.Cells(1, 1), lookat:=xlPart, searchorder:=xlByRows, SearchDirection:=xlPrevious).Row
Thanks anyway though!
Jul 29 '08 #75
puppydogbuddy
1,923 Expert 1GB
I just got back on-line and see that you resolved your problem. That is great. Best of luck with your application.
Jul 29 '08 #76
Stewart Ross
2,545 Expert Mod 2GB
Excellent work both! I'm really glad you have resolved the events and the final glitches. The last one was a good example of why explicit qualification of the Excel application is needed - sometimes implicit references work (on first pass usually) - then refuse to work from there on. This kind of behaviour is really confusing when debugging, because it misleads you into thinking the algorithm implementation is incorrect, when in fact this is fine all along.

Glad PDog could assist you, and that my smaller contribution also assisted.

Cheers

Stewart
Jul 29 '08 #77

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

Similar topics

11
by: Mr. Smith | last post by:
Hello all, My code can successfully open, write to, format and save several worksheets in a workbook then save it by a given name, close and quit excel. My problem is that if I try and do it...
0
by: Mike Knight | last post by:
(I've also posted this problem on microsoft.public.excel.programming) I have a MS Access 2003 Database named "AS400 Fields.mdb". This database contains links to tables on an AS400. In MS...
18
by: Andre Laplume via AccessMonster.com | last post by:
I have inherited a bunch of dbs which are are shared among a small group in my dept. We typically use the dbs to write queries to extract data, usually dumping it into Excel. Most dbs originated...
1
by: com | last post by:
Extreme Web Reports 2005 - Soft30.com The wizard scans the specified MS Access database and records information such as report names, parameters and subqueries. ......
2
by: Bill | last post by:
I have a 200 record database that includes a date/time field, AnnivDate, for a wedding anniversary. AnnivDate has nulls and some incorrect year data. I have been creating the Access database...
2
by: =?Utf-8?B?UmljaA==?= | last post by:
Hello, I created a simple dll in VB2005 to use ADO.Net -- oleDBDataAdapter to write data to an Excel file. The code (below) works fine from an Excel file (invoke the dll from Excel to write to...
0
by: =?Utf-8?B?TW9udGU=?= | last post by:
I am using VB.NET to open and read info from Excel spreadsheets using code similar to this: Dim xlApp As Excel.Application Dim xlBook As Excel.Workbook Dim xlBooks As Excel.Workbooks Dim...
16
by: Phil Stanton | last post by:
I have a form with a button which is supposed to open an Excel file (With lots of Macros /VBA) in it. The Excel file gets it's data from the Access program Here is the code Private Sub...
3
by: khoward | last post by:
Hi, I have an Access 2007 database that contains customer contact information. There are over 8,000 that include name, organization (as a look-up column), email, phone, address, and events that...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...

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.