473,387 Members | 1,290 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,387 software developers and data experts.

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 #1
76 7203
puppydogbuddy
1,923 Expert 1GB
Your Excel code should work fine in MS Access provided that you:
1. set a reference to the Excel Object Library in the Visual Basic Editor in MS Access.
2. use excel automation code to open your workbook from ms access (see code below) before you call your function.

To set a reference,

Open the Access VBE (ALT + F11)
Go to Tools|References
Check the box next to the relevant Excel Library (for example Microsoft Excel 9.0 Object Library if using Office 2000)
Click OK


Expand|Select|Wrap|Line Numbers
  1.      ' opens the specified Spreadsheet
  2.     Dim xlApp As Excel.Application 
  3.     Dim xlWB As Excel.Workbook 
  4.     Set xlApp = New Excel.Application 
  5.     With xlApp 
  6.         .Visible = True 
  7.         Set xlWB = .Workbooks.Open("C:\Dir_path\xlfilename.xls", , False) 
  8.     End With 
Jul 19 '08 #2
JFKJr
126 100+
Your Excel code should work fine in MS Access provided that you:
1. set a reference to the Excel Object Library in the Visual Basic Editor in MS Access.
2. use excel automation code to open your workbook from ms access (see code below) before you call your function.

To set a reference,

Open the Access VBE (ALT + F11)
Go to Tools|References
Check the box next to the relevant Excel Library (for example Microsoft Excel 9.0 Object Library if using Office 2000)
Click OK


Expand|Select|Wrap|Line Numbers
  1.      ' opens the specified Spreadsheet
  2.     Dim xlApp As Excel.Application 
  3.     Dim xlWB As Excel.Workbook 
  4.     Set xlApp = New Excel.Application 
  5.     With xlApp 
  6.         .Visible = True 
  7.         Set xlWB = .Workbooks.Open("C:\Dir_path\xlfilename.xls", , False) 
  8.     End With 
Hello puppydogbuddy, first of all thank you very much for the response.

I did whatever you asked me to do, I set the reference to the Microsoft Excel 11.0 Object Library (I am using MS Access 2003) and I changed the code to the following, but I am unable to run the code could you please tell me what is the problem with the code.

I would like my code to open the spreadsheet and able to check/select 5 rows by double clicking the cell in the first column corresponding to that row (just like the way I did using excel VBE)

I would really appreciate your help. Thanks a million!

Expand|Select|Wrap|Line Numbers
  1. Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
  2.  
  3. ' opens the specified Spreadsheet
  4. Dim xlApp As Excel.Application
  5. Dim xlWB As Excel.Workbook
  6. Set xlApp = New Excel.Application
  7. Dim ColRange As String, PoliciesCount As Integer, LastUsedRow As Long
  8.  
  9. With xlApp
  10.     .Visible = True
  11.     Set xlWB = .Workbooks.Open("C:\ExtractPolicyList.xls", , False)
  12.     If Target.Count > 1 Then Exit Sub
  13.  
  14.     If WorksheetFunction.CountA(Cells) > 0 Then
  15.         LastUsedRow = Cells.Find(What:="*", After:=[A1], _
  16.                     SearchOrder:=xlByRows, _
  17.                     SearchDirection:=xlPrevious).Row
  18.     End If
  19.  
  20.     ColRange = "A2:A" & LastUsedRow
  21.     If Intersect(Target, Range(ColRange)) Is Nothing Then Exit Sub
  22.     Target.Font.Name = "marlett"
  23.  
  24.     If Target.Value <> "a" Then
  25.         PoliciesCount = Columns(1).SpecialCells(xlCellTypeConstants, 23).Cells.Count
  26.         If PoliciesCount > 5 Then
  27.             MsgBox "You cannot select more than 5 rows", vbOKOnly Or vbCritical, "Warning!"
  28.             Exit Sub
  29.         End If
  30.         Target.Value = "a"
  31.         Cancel = True
  32.         Exit Sub
  33.     End If
  34.  
  35.     If Target.Value = "a" Then
  36.         Target.ClearContents
  37.         Cancel = True
  38.         Exit Sub
  39.     End If
  40. End With
  41. End Sub
Jul 21 '08 #3
puppydogbuddy
1,923 Expert 1GB
When you say the code doesn't run, you need to provide some details. Do you get any compile or runtime errors? If the answer is yes, what is the error message and what lines of code are highlighted for the error? In case you don't know how to get the Code editor to highlight the error line, here's how: Go to the command menu for the VB code editor and select Tools>options>general tab>checkmark "break on all errors". Be sure to change it back to"break on unhandled errors when you are finished debugging.

If you don't get any errors, but program does not execute, your problem could be a security issue related to the sandbox security defaults implemented by Microsoft in 2003. To fix, go to the Access (not VB) command menu, and set your macro security level to low.
Jul 21 '08 #4
JFKJr
126 100+
When you say the code doesn't run, you need to provide some details. Do you get any compile or runtime errors? If the answer is yes, what is the error message and what lines of code are highlighted for the error? In case you don't know how to get the Code editor to highlight the error line, here's how: Go to the command menu for the VB code editor and select Tools>options>general tab>checkmark "break on all errors". Be sure to change it back to"break on unhandled errors when you are finished debugging.

If you don't get any errors, but program does not execute, your problem could be a security issue related to the sandbox security defaults implemented by Microsoft in 2003. To fix, go to the Access (not VB) command menu, and set your macro security level to low.
Sorry to confuse you. I have changed the code a little bit. Let me give you a detail description of my problem.

When I run the following "RunMacro()" procedure, it opens the excel file and calls "Worksheet_BeforeDoubleClick" function. Since, I am sending "A3" as a range parameter, the code is automatically selecting the 3rd row by keeping a checkmark/tick into the cell in the 1st column corresponding to the 3rd row.

But, I would like my code to open an excel file, allow the user to double click a cell and when the user double click a cell the "Worksheet_BeforeDoubleClick" function should be called, which should take the range parameter as the name of the cell clicked.

I am not sure if I explained my problem in detail or created more confusion. Please let me know if you are unable to understand my problem, I will definitely come up with better words.

Thanks a lot for your valuable time and appreciate your help. Thank you very much :)

Expand|Select|Wrap|Line Numbers
  1. Sub RunMacro()
  2. ' opens the specified Spreadsheet
  3. Dim xlApp As Excel.Application
  4. Dim xlWB As Excel.Workbook
  5. Set xlApp = New Excel.Application
  6. With xlApp
  7.     .Visible = True
  8.     Set xlWB = .Workbooks.Open("C:\ExtractPolicyList.xls", , False)
  9.     Worksheet_BeforeDoubleClick .Range("A3"), False
  10. End With
  11. End Sub
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.  
  14. Target.Font.Name = "marlett"
  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 21 '08 #5
puppydogbuddy
1,923 Expert 1GB
Ok,
That was an excellent explanation.

Try this:
Target.Value = Active.Cell.Address

instead of your a3 parameter

Let me know what happens.
Jul 21 '08 #6
JFKJr
126 100+
Ok,
That was an excellent explanation.

Try this:
Target.Value = Active.Cell.Address

instead of your a3 parameter

Let me know what happens.
Thanks for the quick response, I changed the code as the following and it is showing "Run-time error '424': Object required"error at line #9.

Please kindly let me know what to do? Thanks.

Expand|Select|Wrap|Line Numbers
  1. Sub RunMacro()
  2. ' opens the specified Spreadsheet
  3. Dim xlApp As Excel.Application
  4. Dim xlWB As Excel.Workbook
  5. Set xlApp = New Excel.Application
  6. With xlApp
  7.     .Visible = True
  8.     Set xlWB = .Workbooks.Open("C:\ExtractPolicyList.xls", , False)
  9.     Worksheet_BeforeDoubleClick .Range(Target.Value = Active.Cell.Address), False
  10. End With
  11. End Sub
Note:
No changes in "Worksheet_BeforeDoubleClick" function.
Jul 21 '08 #7
puppydogbuddy
1,923 Expert 1GB
My fault.

Change this:
.Range(Target.Value = Active.Cell.Address)

to this:
Change this:
.Range(Target.Value = ActiveCell.Address)

However, in order to get this to work the way you want, you may have to leave "a3" as the target range in your calling proc RunMacro. Then somewhere in your called proc (on line 21??), put Target.Value = ActiveCell.Address.

Also, shouldn't your if statement following line 21 be.....if Target.Value <> "a" rather then ="a" as you have it??

Let me know.
Jul 21 '08 #8
JFKJr
126 100+
My fault.

Change this:
.Range(Target.Value = Active.Cell.Address)

to this:
Change this:
.Range(Target.Value = ActiveCell.Address)

However, in order to get this to work the way you want, you may have to leave "a3" as the target range in your calling proc RunMacro. Then somewhere in your called proc (on line 21??), put Target.Value = ActiveCell.Address.

Also, shouldn't your if statement following line 21 be.....if Target.Value <> "a" rather then ="a" as you have it??

Let me know.
Yes, I even tried with "Target.Value = ActiveCell.Address" just like you suggested but it is giving the same run-time error at line #9 in "RunMacro()" proc.

And, regarding the line #21 in called proc:

let us assume the user clicked a cell (lets say 'A3') in an excel sheet, then the font of that cell will change into "marlett" (as per in line #14).

And, if there is no value in that cell, it assigns "a" to it (as per line #21). The reason I changed the value to "a" is, in marlett font the value "a" gives checkmark/ tick sign, which makes the user to think that he/she has selected/checked that row.

In similar way, if the user double clicks again on the cell which has value "a", the code clear the contents (as per line #26) giving the user a feeling that they have unchecked the row.

My main problem is, how to call "Worksheet_BeforeDoubleClick" function when a user clicks a cell in excel sheet?
And, how to assign the name of the clicked cell to the range parameter of the function?

I am having a very hard time to figure out how to deal with the problem. Your help on this will be greatly appreciated!
Jul 21 '08 #9
puppydogbuddy
1,923 Expert 1GB
Ok, now i am curious! I will do some research and get back to you tomorrow.
Jul 21 '08 #10
JFKJr
126 100+
Ok, now i am curious! I will do some research and get back to you tomorrow.
Sure :), thanks for your time. Have a great day!

I will keep in touch with the post.
Jul 21 '08 #11
Stewart Ross
2,545 Expert Mod 2GB
Hi puppydogbuddy and jfkjr. The object error in this case is occurring because the Excel application object, though defined, is not being referenced when the worksheet event handler is being called in line 9. When Excel is running as an automation server from Access the application object involved must be explicitly referenced - there can be no implied active workbook as there can be when code is run within Excel itself (the code is actually running in an Access instance, hence the need to refer through the defined Excel object instead).

To refer to the properties of the Excel application you need to do so through the xlApp object itself. This applies to all occurrences - where you are referencing the current workbook, or the active worksheet, or a range object, or the active cell etc. Examples:

something = xlApp.activeCell
with xlApp
.worksheets(1).cells(1,1) = somevalue
.activesheet.select
end with

I have not myself run code in an Excel worksheet from Excel running as an Access automation server (the point at which your error occurs). I run all my Excel code from the automation instance itself via Access - but assuming that there is no problem in doing so it will still need to be referenced through the application object if it is to run at all.

I do suspect that to run the code in the active worksheet you will need to qualify the application reference to be explicit about which sheet you are referring to but I have not tried this myself so I do not know the syntax (xlApp.ActiveSheet.worksheet_beforedoubleclick perhaps?)

-Stewart
Jul 21 '08 #12
puppydogbuddy
1,923 Expert 1GB
Hi Stewart,
Thanks for that info. It is definitely part of the problem...unfortunately there are other idiosyncracies that need to be addressed. Hope to have it resolved tomorrow. Congrats on the great job you are doing as moderator.

pDog
Jul 22 '08 #13
puppydogbuddy
1,923 Expert 1GB
Try this and let me know what happens. Note the initial range assignment to "a3" to set focus on the worksheet, and note the use of a selectionChange event to trigger the worksheet_beforeDoubleClick event .
Expand|Select|Wrap|Line Numbers
  1. Sub RunMacro()
  2. ' opens the specified Spreadsheet
  3. Dim xlApp As Excel.Application
  4. Dim xlWB As Excel.Workbook
  5.  
  6. Set xlApp = New Excel.Application
  7.  
  8. With xlApp
  9.     .Visible = True
  10.     Set xlWB = .Workbooks.Open("C:\ExtractPolicyList.xls", , False)
  11.     Worksheet_SelectionChange.Range(Target=:"a3"), False
  12. End With 
  13. End Sub
__________________________________________________ __

Expand|Select|Wrap|Line Numbers
  1. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  2.     If ActiveCell.Address <> Target.Address Then
  3.        Exit Sub
  4.     Else
  5.     Worksheet_BeforeDoubleClick.Range(Target.Value = xlApp.ActiveCell.Address), False    
  6.     End If
  7. End Sub
Jul 22 '08 #14
JFKJr
126 100+
Try this and let me know what happens. Note the initial range assignment to "a3" to set focus on the worksheet, and note the use of a selectionChange event to trigger the worksheet_beforeDoubleClick event .
Expand|Select|Wrap|Line Numbers
  1. Sub RunMacro()
  2. ' opens the specified Spreadsheet
  3. Dim xlApp As Excel.Application
  4. Dim xlWB As Excel.Workbook
  5.  
  6. Set xlApp = New Excel.Application
  7.  
  8. With xlApp
  9.     .Visible = True
  10.     Set xlWB = .Workbooks.Open("C:\ExtractPolicyList.xls", , False)
  11.     Worksheet_SelectionChange.Range(Target=:"a3"), False
  12. End With 
  13. End Sub
__________________________________________________ __

Expand|Select|Wrap|Line Numbers
  1. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  2.     If ActiveCell.Address <> Target.Address Then
  3.        Exit Sub
  4.     Else
  5.     Worksheet_BeforeDoubleClick.Range(Target.Value = xlApp.ActiveCell.Address), False    
  6.     End If
  7. End Sub
Hello puppydogbuddy, I have tried your code, but it is giving me "Run-time error 424: Object Required" error on line #5 in "Worksheet_SelectionChange" proc.

Expand|Select|Wrap|Line Numbers
  1. Sub RunMacro()
  2. ' opens the specified Spreadsheet
  3. Dim xlApp As Excel.Application
  4. Dim xlWB As Excel.Workbook
  5.  
  6. Set xlApp = New Excel.Application
  7.  
  8. With xlApp
  9.     .Visible = True
  10.     Set xlWB = .Workbooks.Open("C:\ExtractPolicyList.xls", , False)
  11.     Worksheet_SelectionChange .Range("A3")
  12. End With
  13. End Sub
Expand|Select|Wrap|Line Numbers
  1. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  2.     If ActiveCell.Address <> Target.Address Then
  3.      Exit Sub
  4.     Else
  5.     Worksheet_BeforeDoubleClick Range(Target.Value = xlApp.ActiveCell.Address), False
  6.     End If
  7. End Sub
Please note that I have changed the following code
Expand|Select|Wrap|Line Numbers
  1. Worksheet_SelectionChange.Range(Target=:"a3"), False
to
Expand|Select|Wrap|Line Numbers
  1. Worksheet_SelectionChange .Range("A3")
B'coz, the function is having only one argument.
Jul 22 '08 #15
puppydogbuddy
1,923 Expert 1GB
I know the use of ActiveCell.Address is ok, so it has to be the parent object reference, or maybe the syntax of other portions of the code on line 5. Try several different variations of the code for line 5 and see if you can get the syntax to work. For example, try the variations below and let me know what happens:

Worksheet_BeforeDoubleClick Range(Target = xlApp.ActiveCell.Address), False

Worksheet_BeforeDoubleClick Range(Target = xlApp.xlWB.ActiveCell.Address), False

Worksheet_BeforeDoubleClick Range(Target = xlWB.Sheet(1).ActiveCell.Address), False

Worksheet_BeforeDoubleClick Range(xlWB.Sheet(1).ActiveCell.Address), False
Jul 22 '08 #16
JFKJr
126 100+
I know the use of ActiveCell.Address is ok, so it has to be the parent object reference, or maybe the syntax of other portions of the code on line 5. Try several different variations of the code for line 5 and see if you can get the syntax to work. For example, try the variations below and let me know what happens:

Worksheet_BeforeDoubleClick Range(Target = xlApp.ActiveCell.Address), False

Worksheet_BeforeDoubleClick Range(Target = xlApp.xlWB.ActiveCell.Address), False

Worksheet_BeforeDoubleClick Range(Target = xlWB.Sheet(1).ActiveCell.Address), False

Worksheet_BeforeDoubleClick Range(xlWB.Sheet(1).ActiveCell.Address), False
Hello puppydogbuddy, I have tried all the given variations but it is giving the same error.

So, I changed the code to the following and I found something interesting, may be this give you an idea on how to deal with the problem.

This is what I did:
1. Opened the excel file, activated a cell (say A5), saved and closed the file.
2. Run "RunMacro()" proc:
a) for the first time, the proc called automatically "Worksheet_BeforeDoubleClick" function and checked/ticked the cell in the first column corresponding to the 5th row.
b) for the second time, it is giving "Run-time error 91: Object variable or With block variable not set" error.

the code is performing the above tasks alternatively (i.e, sometimes working and sometimes not)

I am not understanding what to do, I am stuck with this!
Hope you get some idea on the problem!

Expand|Select|Wrap|Line Numbers
  1. Sub RunMacro()
  2. ' opens the specified Spreadsheet
  3. Dim xlApp As Excel.Application
  4. Dim xlWB As Excel.Workbook
  5.  
  6. Set xlApp = New Excel.Application
  7.  
  8. With xlApp
  9.     .Visible = True
  10.     Set xlWB = .Workbooks.Open("C:\ExtractPolicyList.xls", , False)
  11.     Worksheet_BeforeDoubleClick .Range(ActiveCell.Address), False
  12. End With
  13. End Sub
Expand|Select|Wrap|Line Numbers
  1. Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
  2. Dim ColRange As String, LastUsedRow As Long
  3.  
  4. If Target.Count > 1 Then Exit Sub
  5. 'MsgBox "" & Target.Count
  6.  
  7. If WorksheetFunction.CountA(Cells) > 0 Then
  8.    LastUsedRow = Cells.Find(What:="*", After:=[A1], _
  9.             SearchOrder:=xlByRows, _
  10.             SearchDirection:=xlPrevious).Row
  11.     'MsgBox "" & LastUsedRow
  12. End If
  13.  
  14. ColRange = "A2:A" & LastUsedRow
  15.  
  16. 'MsgBox "" & Intersect(Target, Range(ColRange))
  17. If Intersect(Target, Range(ColRange)) Is Nothing Then Exit Sub
  18.  
  19. Target.Font.Name = "marlett"
  20. If Target.Value <> "a" Then
  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 22 '08 #17
puppydogbuddy
1,923 Expert 1GB
Progress.....See if the changes below help. Also you need to add an error handler to the runmacro(). If you need the code for that, let me know. Let know if the code changes helped any.

Sub RunMacro()
' opens the specified Spreadsheet
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook

Set xlApp = New Excel.Application

With xlApp
.Visible = True
.Interactive = True '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Set xlWB = .Workbooks.Open("C:\ExtractPolicyList.xls", , False)
xlWB.Worksheet(1).Activate '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Worksheet_BeforeDoubleClick .Range(ActiveCell.Address), False
End With
Exit_Routine: '<<<<<<<<<<<<<<<
Set xlApp = Nothing
Set xlWB = Nothing
Exit Sub
End Sub
Jul 22 '08 #18
JFKJr
126 100+
Progress.....See if the changes below help. Also you need to add an error handler to the runmacro(). If you need the code for that, let me know. Let know if the code changes helped any.

Sub RunMacro()
' opens the specified Spreadsheet
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook

Set xlApp = New Excel.Application

With xlApp
.Visible = True
.Interactive = True '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Set xlWB = .Workbooks.Open("C:\ExtractPolicyList.xls", , False)
xlWB.Worksheet(1).Activate '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Worksheet_BeforeDoubleClick .Range(ActiveCell.Address), False
End With
Exit_Routine: '<<<<<<<<<<<<<<<
Set xlApp = Nothing
Set xlWB = Nothing
Exit Sub
End Sub
HI, I have changed the code as per your suggestions but it is giving me "Run-time error '438': Object doesn't support this property or method" error on line #10.

Thanks a lot for your help. Hopefully we are coming towards the solution!
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.Worksheet(1).Activate
  11. Worksheet_BeforeDoubleClick .Range(ActiveCell.Address), False
  12. End With
  13. Exit_Routine:
  14. Set xlApp = Nothing
  15. Set xlWB = Nothing
  16. Exit Sub
  17. End Sub
Jul 22 '08 #19
puppydogbuddy
1,923 Expert 1GB
change this: xlWB.Worksheet(1).Activate

to this: xlWB.Worksheets(1).Activate


or this: Worksheets(1).Activate
Jul 22 '08 #20
JFKJr
126 100+
change this: xlWB.Worksheet(1).Activate

to this: xlWB.Worksheets(1).Activate


or this: Worksheets(1).Activate
Yes, I changed it to "xlWB.Worksheets(1).Activate", now there is no "Run-time error '438': Object doesn't support this property or method" error.

But, it is working same as the code in post #17. I do not see any difference :(
Jul 22 '08 #21
puppydogbuddy
1,923 Expert 1GB
I think we need to activate a cell within the worksheet(via code) to trigger the process. I will be out of the office until later today. I will get back to you. If you want play with it, Iook at this reference.

http://msdn.microsoft.com/en-us/libr...ffice.11).aspx
Jul 22 '08 #22
JFKJr
126 100+
I think we need to activate a cell within the worksheet(via code) to trigger the process. I will be out of the office until later today. I will get back to you. If you want play with it, Iook at this reference.

http://msdn.microsoft.com/en-us/libr...ffice.11).aspx
Sure, I will workaround with the code and see how it goes!

Have a great day! :-)
Jul 22 '08 #23
puppydogbuddy
1,923 Expert 1GB
ok, let me know what happens with this code:

Sub RunMacro()
Expand|Select|Wrap|Line Numbers
  1. Dim xlApp As Excel.Application
  2. Dim xlWB As Excel.Workbook
  3. Set xlApp = New Excel.Application
  4.  
  5. With xlApp
  6. .Visible = True
  7. .Interactive = True
  8. Set xlWB = .Workbooks.Open("C:\ExtractPolicyList.xls", , False)                                   
  9. xlWB.Worksheets(1).Activate
  10. xlWB.ActiveSheet.Range("a5").Select
  11. Worksheet_BeforeDoubleClick ActiveCell, False
  12. End With
  13. Exit_Routine:
  14. Set xlApp = Nothing
  15. Set xlWB = Nothing
  16. Exit Sub
  17. End Sub
Jul 23 '08 #24
JFKJr
126 100+
ok, let me know what happens with this code:

Sub RunMacro()
Expand|Select|Wrap|Line Numbers
  1. Dim xlApp As Excel.Application
  2. Dim xlWB As Excel.Workbook
  3. Set xlApp = New Excel.Application
  4.  
  5. With xlApp
  6. .Visible = True
  7. .Interactive = True
  8. Set xlWB = .Workbooks.Open("C:\ExtractPolicyList.xls", , False)                                   
  9. xlWB.Worksheets(1).Activate
  10. xlWB.ActiveSheet.Range("a5").Select
  11. Worksheet_BeforeDoubleClick ActiveCell, False
  12. End With
  13. Exit_Routine:
  14. Set xlApp = Nothing
  15. Set xlWB = Nothing
  16. Exit Sub
  17. End Sub
Hi, the above code automatically activated "A5" cell and checked/ticked the cell.

And, when I double-click on other cell it is not working.
Jul 23 '08 #25
puppydogbuddy
1,923 Expert 1GB
Can you put code breaks before and after the double click and tell me what values are shown for the target vs the active cell?
Jul 23 '08 #26
JFKJr
126 100+
Can you put code breaks before and after the double click and tell me what values are shown for the target vs the active cell?
ok, I have used both the code breaks to know the program flow and "MsgBox" to know the "Target" and "ActiveCell" values.

This is what happening:

1. For the first time when I run "RunMacro()" proc,
a) The value of "ActiveCell" is empty at line #12 (since there is no value in the "a5" cell) and it called "Worksheet_BeforeDoubleClick" proc.
b) In the called proc the value of "Target" is empty at line #3, the following steps are executed and checked/ticked the "a5" cell.

2. For the second time when I run "RunMacro()" proc,
a)The value of "ActiveCell" is "a" at line #12 (since "a5" cell has "a" value) and it called "Worksheet_BeforeDoubleClick" proc.
b) In the called proc the value of "Target" is "a" at line #3, the following steps are executed and unchecked the "a5" cell.

Hope this helps!

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. Worksheet_BeforeDoubleClick ActiveCell, False
  14. End With
  15. Exit_Routine:
  16. Set xlApp = Nothing
  17. Set xlWB = Nothing
  18. Exit Sub
  19. End Sub
Expand|Select|Wrap|Line Numbers
  1. Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
  2. Dim ColRange As String, LastUsedRow As Long
  3. MsgBox "" & Target 
  4. If Target.Count > 1 Then Exit Sub
  5.  
  6. If WorksheetFunction.CountA(Cells) > 0 Then
  7.    LastUsedRow = Cells.Find(What:="*", After:=[A1], _
  8.             SearchOrder:=xlByRows, _
  9.             SearchDirection:=xlPrevious).Row
  10.     'MsgBox "" & LastUsedRow
  11. End If
  12.  
  13. ColRange = "A2:A" & LastUsedRow
  14. If Intersect(Target, Range(ColRange)) Is Nothing Then Exit Sub
  15.  
  16. Target.Font.Name = "marlett"
  17. If Target.Value <> "a" Then
  18.    Target.Value = "a"
  19.    Cancel = True
  20.    Exit Sub
  21. End If
  22.  
  23. If Target.Value = "a" Then
  24.    Target.ClearContents
  25.    Cancel = True
  26.    Exit Sub
  27. End If
  28. End Sub
Jul 23 '08 #27
puppydogbuddy
1,923 Expert 1GB
insert this code between lines 10 and 11 and let me know results:

ActiveCell.Value = "Hello"
Jul 23 '08 #28
JFKJr
126 100+
insert this code between lines 10 and 11 and let me know results:

ActiveCell.Value = "Hello"
ok, I have entered the above code in between 10 and 11 lines,

1. For the first time,
a) "ActiveCell" value is "Hello" at line #12 and "Worksheet_BeforeDoubleClick" proc is called.
b) And in the called proc, "Target" value is "Hello" at line #3, the following steps are executed and checked/ticked the "a5" cell.

2. For the second time,
a) "ActiveCell" value is "Hello" at line #12 and "Worksheet_BeforeDoubleClick" proc is called.
b) And in the called proc, "Target" value is "Hello" at line #3, the following steps are executed and checked/ticked the "a5" 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:\ExtractPolicyList2.xls", , False)
  10. xlWB.Worksheets(1).Activate
  11. ActiveCell.Value = "Hello"
  12. xlWB.ActiveSheet.Range("a5").Select
  13. MsgBox "" & ActiveCell
  14. Worksheet_BeforeDoubleClick ActiveCell, False
  15. End With
  16. Exit_Routine:
  17. Set xlApp = Nothing
  18. Set xlWB = Nothing
  19. Exit Sub
  20. End Sub
Jul 23 '08 #29
puppydogbuddy
1,923 Expert 1GB
So is working or not? if not, tell me what was supposed happen in terms of the previous post.
Jul 23 '08 #30
JFKJr
126 100+
So is working or not? if not, tell me what was supposed happen in terms of the previous post.
Hi, the previous code is working fine but the code is automatically selecting/checking only the "A5" cell. It is not working when I double click on any other cell.
Jul 23 '08 #31
puppydogbuddy
1,923 Expert 1GB
The initial cell must have a value to trigger your function.


Change line 11 to:
If IsEmpty(Range("a5")) Then
Range("a5").Cells(1,5).Value = "Hello"
End If
Jul 23 '08 #32
JFKJr
126 100+
The initial cell must have a value to trigger your function.


Change line 11 to:
If IsEmpty(Range("a5")) Then
Range("a5").Cells(1,5).Value = "Hello"
End If
Hello puppydogbuddy, just like the past, the code is selecting only "A5" cell and is not invoking "Worksheet_BeforeDoubleClick" proc when I double click on any other cell.

I am confused, because the code in the post #1 is working perfectly fine in excel VBA, it is triggering "Worksheet_BeforeDoubleClick" event when I double click on any cell.

Why it is not happening here? Please help me on this!

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. If IsEmpty(Range("a5")) Then
  12. Range("a5").Cells(1, 5).Value = "Hello"
  13. End If
  14. xlWB.ActiveSheet.Range("a5").Select
  15. MsgBox "" & ActiveCell
  16. Worksheet_BeforeDoubleClick ActiveCell, False
  17. End With
  18. Exit_Routine:
  19. Set xlApp = Nothing
  20. Set xlWB = Nothing
  21. Exit Sub
  22. End Sub
Jul 23 '08 #33
puppydogbuddy
1,923 Expert 1GB
Hello puppydogbuddy, just like the past, the code is selecting only "A5" cell and is not invoking "Worksheet_BeforeDoubleClick" proc when I double click on any other cell.

I am confused, because the code in the post #1 is working perfectly fine in excel VBA, it is triggering "Worksheet_BeforeDoubleClick" event when I double click on any cell.

Why it is not happening here? Please help me on this!
Apparently, your function does not loop automatically in Access VBA like it does in pure excel??

Change the following code from this:
Expand|Select|Wrap|Line Numbers
  1. MsgBox "" & ActiveCell
  2. Worksheet_BeforeDoubleClick ActiveCell, False
  3. End With

To this:
Expand|Select|Wrap|Line Numbers
  1. MsgBox "" & ActiveCell
  2. End With
  3.  
  4. Do Until Target.Value = 0
  5.     Worksheet_BeforeDoubleClick ActiveCell, False
  6. Loop
Jul 23 '08 #34
puppydogbuddy
1,923 Expert 1GB
PS:
you need to determine the criteria that terminates the loop....I used Target.Value = 0 for illustrative purposes.
Jul 23 '08 #35
JFKJr
126 100+
Apparently, your function does not loop automatically in Access VBA like it does in pure excel??

Change the following code from this:
Expand|Select|Wrap|Line Numbers
  1. MsgBox "" & ActiveCell
  2. Worksheet_BeforeDoubleClick ActiveCell, False
  3. End With

To this:
Expand|Select|Wrap|Line Numbers
  1. MsgBox "" & ActiveCell
  2. End With
  3.  
  4. Do Until Target.Value = 0
  5.     Worksheet_BeforeDoubleClick ActiveCell, False
  6. Loop
hmm, I replaced the code but it is generating "Run-time error 424: Object required" error at line #14, since there is no "Target" object defined in the proc.

So, I replaced "Target.Value" at line #14 with "ActiveCell.Value" then it is coming out of the loop without calling "Worksheet_BeforeDoubleClick" proc.

Is there any way in Access VBA to run a macro when a cell in an excel file is clicked. Does it is possible to do? I am clueless!

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. Do Until Target.Value = 0
  15.     Worksheet_BeforeDoubleClick ActiveCell, False
  16. Loop
  17. Exit_Routine:
  18. Set xlApp = Nothing
  19. Set xlWB = Nothing
  20. Exit Sub
  21. End Sub
Jul 23 '08 #36
JFKJr
126 100+
Hello puppudogbuddy, let us make the problem short so that we can only concentrate on the main cause.

I would like to run the following macro whenever a cell in the excel file is clicked, is it possible to do in Access VBA?

Expand|Select|Wrap|Line Numbers
  1. Sub RunMacro()
  2. MsgBox "Hello! You have clicked a cell!"
  3. End Sub
I am stuck with this!
Your idea/help will be greatly appreciated. Thanks!
Jul 23 '08 #37
puppydogbuddy
1,923 Expert 1GB
try this:

Do Until Target.Value = "a"
Worksheet_BeforeDoubleClick Target = ActiveCell, False
Loop
Jul 23 '08 #38
JFKJr
126 100+
try this:

Do Until Target.Value = "a"
Worksheet_BeforeDoubleClick Target = ActiveCell, False
Loop
Same again, the code is checking only "A5" cell and coming out of the loop.

I think when I clicked on other cell the "RunMacro()" proc is not running, if we make that proc running when the cell is clicked then the code will check/ tick the active cell.

Let us make this simple and run the following simple proc when a cell is clicked as I mentioned in the above post.

What do you think?

Expand|Select|Wrap|Line Numbers
  1. Sub RunMacro()
  2. MsgBox "Hello! You have clicked a cell!"
  3. End Sub
Jul 23 '08 #39
puppydogbuddy
1,923 Expert 1GB
i will get back to you tomorrow.......one thing you can try is changing your function from Private to Public.....
Jul 23 '08 #40
puppydogbuddy
1,923 Expert 1GB
I think we are on the right track it now. We were missing a subroutine called Worksheet_SelectionChange, which is an event handler that will be called automatically by Excel to execute Worksheet_BeforeDoubleClick whenever the user selects a different cell. Also note that the “Target” parameter represents the cell that the user has just selected.....I used instead of ActiveCell because it is the listed parameter and is defined as a range object.

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. Worksheet_SelectionChange Target.Value
  14. End With
  15. Exit_Routine:
  16. Set xlApp = Nothing
  17. Set xlWB = Nothing
  18. Exit Sub
  19. End Sub
------------------------------------------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  2.  
  3. Worksheet_BeforeDoubleClick Target.Value
  4.  
  5. End Sub
Jul 24 '08 #41
JFKJr
126 100+
I think we are on the right track it now. We were missing a subroutine called Worksheet_SelectionChange, which is an event handler that will be called automatically by Excel to execute Worksheet_BeforeDoubleClick whenever the user selects a different cell. Also note that the “Target” parameter represents the cell that the user has just selected.....I used instead of ActiveCell because it is the listed parameter and is defined as a range object.

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. Worksheet_SelectionChange Target.Value
  14. End With
  15. Exit_Routine:
  16. Set xlApp = Nothing
  17. Set xlWB = Nothing
  18. Exit Sub
  19. End Sub
------------------------------------------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  2.  
  3. Worksheet_BeforeDoubleClick Target.Value
  4.  
  5. End Sub
Hello puppydogbuddy, the code is automatically selecting only "a5" cell and coming out of macro. It is not calling "Worksheet_SelectionChange" event handler automatically when I select other cells.

Do you think Access VBA supports event handlers or should we have to select any reference to enable them?

Because, if Access VBA supports event handlers then it should have supported "Worksheet_BeforeDoubleClick" event handler also, which is automatically called whenever user double clicks on a cell just like in excel VBA.

I am using MS Access 2003 format.

Please feel free to correct me if I am wrong.

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. Worksheet_SelectionChange ActiveCell
  14. End With
  15. Exit_Routine:
  16. Set xlApp = Nothing
  17. Set xlWB = Nothing
  18. Exit Sub
  19. End Sub
Expand|Select|Wrap|Line Numbers
  1. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  2. Worksheet_BeforeDoubleClick Target, False
  3. End Sub
Please note that the following are the changes in the above code:

1. changed "Worksheet_SelectionChange Target.Value" to "Worksheet_SelectionChange ActiveCell" at line #13. Because it is generating "Object required" error.

2. changed "Worksheet_BeforeDoubleClick Target.Value" to "Worksheet_BeforeDoubleClick Target, False" at line #2, since the proc takes two arguments.

Please let me know if the above changes affect the solution. Thanks!
Jul 24 '08 #42
puppydogbuddy
1,923 Expert 1GB
You should have a reference set to the excel object library.

it should be Target because target is rangebject and ActiveCell is not.

I just found a vba tutorial that demos code very close to what we are doing above. Read it very carefully and see if you can spot anything we did wrong.

http://pubs.logicalexpressions.com/P...cle.asp?ID=407
Jul 24 '08 #43
JFKJr
126 100+
You should have a reference set to the excel object library.

it should be Target because target is rangebject and ActiveCell is not.

I just found a vba tutorial that demos code very close to what we are doing above. Read it very carefully and see if you can spot anything we did wrong.

http://pubs.logicalexpressions.com/P...cle.asp?ID=407
Hello puppydogbuddy, I am sorry that I did not exactly follow the above post, do you mean that we need to set a reference to excel object library, if yes then which library do we need to set?
And, I already had a reference to "Microsoft Excel 11.0 Object Library".

ok, as per your suggestion I changed "Worksheet_SelectionChange ActiveCell" to "Worksheet_SelectionChange Target.Value" at line #13. But, it is generating "Run-time error '424': Object required" error. What should I do?

And regarding the article, it is mentioning only about the event handlers using excel VBA. The event handlers such as "Worksheet_SelectionChange" and "Worksheet_BeforeDoubleClick" working perfectly fine in excel VBA.

But my question is does Access VBA support excel event handlers?

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. Worksheet_SelectionChange Target.Value
  14. End With
  15. Exit_Routine:
  16. Set xlApp = Nothing
  17. Set xlWB = Nothing
  18. Exit Sub
  19. End Sub
Jul 24 '08 #44
puppydogbuddy
1,923 Expert 1GB
Hello puppydogbuddy, I am sorry that I did not exactly follow the above post, do you mean that we need to set a reference to excel object library, if yes then which library do we need to set?
And, I already had a reference to "Microsoft Excel 11.0 Object Library".

ok, as per your suggestion I changed "Worksheet_SelectionChange ActiveCell" to "Worksheet_SelectionChange Target.Value" at line #13. But, it is generating "Run-time error '424': Object required" error. What should I do?

And regarding the article, it is mentioning only about the event handlers using excel VBA. The event handlers such as "Worksheet_SelectionChange" and "Worksheet_BeforeDoubleClick" working perfectly fine in excel VBA.

But my question is does Access VBA support excel event handlers?

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. Worksheet_SelectionChange Target.Value
  14. End With
  15. Exit_Routine:
  16. Set xlApp = Nothing
  17. Set xlWB = Nothing
  18. Exit Sub
  19. End Sub
you have the correct reference

change : Worksheet_SelectionChange Target.Value
to: Worksheet_SelectionChange Target

Access VBA supports excel event handlers once excel automation object is created, which has been done at the begining of your code.
Jul 24 '08 #45
JFKJr
126 100+
you have the correct reference

change : Worksheet_SelectionChange Target.Value
to: Worksheet_SelectionChange Target

Access VBA supports excel event handlers once excel automation object is created, which has been done at the begining of your code.
Yes, I even changed it to "Worksheet_SelectionChange Target" and it is giving me same "Object Required" error.

And, it's a relief to know that Access VBA supports excel event handlers. But I have searched internet a lot and unfortunately I didn't found any article on using "Worksheet_SelectionChange" and "Worksheet_BeforeDoubleClick" event handlers in Access VBA!
Jul 24 '08 #46
puppydogbuddy
1,923 Expert 1GB
on the obj error, try moving that code line to the line after the "end with" statement.

the beauty of vba is that once an instance of excel automation object is created, Access has access to most of excels objects, methods, etc.

idea: try copying the code from the vba tutorial and see if it runs
Jul 24 '08 #47
JFKJr
126 100+
on the obj error, try moving that code line to the line after the "end with" statement.

the beauty of vba is that once an instance of excel automation object is created, Access has access to most of excels objects, methods, etc.

idea: try copying the code from the vba tutorial and see if it runs
Hi, I moved the code to the line after the "end with" statement and it is giving same "Object Required" error.

And, regarding the vba tutorial I used the following code in both excel and Access:

1. In excel, every time I switch to the worksheet, the event is called and it is changing the rectangular cells to square
2. But in access, the event is called only for the first time I run "RunMacro()" proc below.

The problem is, the code is running properly in both excel and access. But, in access the events doesn't trigger unless we manually run a proc, which calls the event.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Worksheet_Activate()
  2. Columns.ColumnWidth = 1
  3. Rows.RowHeight = 8
  4. End Sub
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. 'Worksheet_SelectionChange Target
  15. Worksheet_Activate
  16. Exit_Routine:
  17. Set xlApp = Nothing
  18. Set xlWB = Nothing
  19. Exit Sub
  20. End Sub
Jul 24 '08 #48
puppydogbuddy
1,923 Expert 1GB
Hi, I moved the code to the line after the "end with" statement and it is giving same "Object Required" error.

And, regarding the vba tutorial I used the following code in both excel and Access:

1. In excel, every time I switch to the worksheet, the event is called and it is changing the rectangular cells to square
2. But in access, the event is called only for the first time I run "RunMacro()" proc below.

The problem is, the code is running properly in both excel and access. But, in access the events doesn't trigger unless we manually run a proc, which calls the event.
Am with a client and will be busy for about 2 hours.

I am not sure what you mean in the following statements?
The problem is, the code is running properly in both excel and access. But, in access the events doesn't trigger unless we manually run a proc, which calls the event

1.which event doesn't trigger ? the SelectionChange event?

2. in access the events doesn't trigger unless we manually run a proc, which calls the event.....run which proc ....the RunMacro?
Jul 24 '08 #49
JFKJr
126 100+
Am with a client and will be busy for about 2 hours.

I am not sure what you mean in the following statements?
The problem is, the code is running properly in both excel and access. But, in access the events doesn't trigger unless we manually run a proc, which calls the event

1.which event doesn't trigger ? the SelectionChange event?

2. in access the events doesn't trigger unless we manually run a proc, which calls the event.....run which proc ....the RunMacro?
Sorry to confuse you, well this is what I mean:

for example lets take "Worksheet_Activate" event handler from the above post,

1. In excel, this event is called every time I switch to the worksheet.
2. But, in access the event is called only when I run above "RunMacro()" proc, which calls "Worksheet_Activate" event.

In contrary to excel VBA, the access vba is not calling the event whenever the user switches to the current worksheet.
Jul 24 '08 #50

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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
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...

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.