Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS Access 2013 late binding to Excel & WorkSheetFunction 1

Status
Not open for further replies.

Knicks

Technical User
Apr 1, 2002
383
0
0
US
I am trying to work with the Excel object without references to Excel (Late Binding). I am unable to get MS Access to recognize the excel function/method WorkSheetFunction in order to test for duplicates and make a simple background or border color change for the duplicates in a column/range. I was wondering if there is an alternative to WorkSheetFunction or if I am using it wrong

Here is the code. The Null value color change works just fine, it is when I attempt to use the WorkSheetfunction that the code fails:

Dim objexcel_app As Object
Dim xlsExcel_wkbook As Object
Dim xlsExcel_sheet As Object
Dim xlsExcel_range As Object
Dim Z As Long

Set objexcel_app = CreateObject("Excel.Application")
Set xlsExcel_wkbook = objexcel_app.Workbooks.Open("C:\Deal\New_NMTC_TLR_Note.xls")
Set xlsExcel_sheet = xlsExcel_wkbook.sheets("New_NMTC_TLR_Note")

Z = xlsExcel_sheet.UsedRange.rows(xlsExcel_sheet.UsedRange.rows.Count).row

Set xlsExcel_range = xlsExcel_sheet.Range("C5:C" & Z)
For Each Cell In xlsExcel_range

If Cell.Value = vbNullString Then
Cell.Borders(9).LineStyle = 4
Cell.Borders(8).LineStyle = 4
Cell.Borders(7).LineStyle = 4
Cell.Borders(10).LineStyle = 4
Cell.Borders(9).Color = RGB(255, 0, 0)
Cell.Borders(8).Color = RGB(255, 0, 0)
Cell.Borders(7).Color = RGB(255, 0, 0)
Cell.Borders(10).Color = RGB(255, 0, 0)

Else

If worksheetfunction.countif(xlsExcel_range, Cell) > 1 Then
Cell.Borders(9).LineStyle = 4
Cell.Borders(8).LineStyle = 4
Cell.Borders(7).LineStyle = 4
Cell.Borders(10).LineStyle = 4
Cell.Borders(9).Color = RGB(0, 0, 204)
Cell.Borders(8).Color = RGB(0, 0, 204)
Cell.Borders(7).Color = RGB(0, 0, 204)
Cell.Borders(10).Color = RGB(0, 0, 204)
End If




End If

Next Cell
 
Hi,

Try...
[tt]
If objexcel_app.worksheetfunction.countif(xlsExcel_range, Cell.Value) > 1 Then
[/tt]


Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Thanx Skip!

The code ran without failing
 
Just curios...

Wouldn't be beneficial (safer?) to have some:
[tt]With objexcel_app
...
End With[/tt]
or[tt]
With xlsExcel_sheet
...
End With
[/tt]
around the code to fully qualify which property of which object we are 'messing' with?


---- Andy

There is a great need for a sarcasm font.
 
I’d do it this way...
Code:
With xlsExcel_sheet
   Set xlsExcel_range = .Range("C5:C" & Z) 
   For Each Cell In xlsExcel_range
      With Cell
         If .Value = vbNullString Then
            .Borders(9).LineStyle = 4
            .Borders(8).LineStyle = 4
            .Borders(7).LineStyle = 4
            .Borders(10).LineStyle = 4
            .Borders(9).Color = RGB(255, 0, 0)
            .Borders(8).Color = RGB(255, 0, 0)
            .Borders(7).Color = RGB(255, 0, 0)
            .Borders(10).Color = RGB(255, 0, 0)

         Else

            If objexcel_app.worksheetfunction.countif(xlsExcel_range, Cell.Value) > 1 Then
               .Borders(9).LineStyle = 4
               .Borders(8).LineStyle = 4
               .Borders(7).LineStyle = 4
               .Borders(10).LineStyle = 4
               .Borders(9).Color = RGB(0, 0, 204)
               .Borders(8).Color = RGB(0, 0, 204)
               .Borders(7).Color = RGB(0, 0, 204)
               .Borders(10).Color = RGB(0, 0, 204)
            End If

         End If
      End With
   Next Cell
End With

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
So [tt]Cell [/tt]does not have to be defined? Or used with a period: [tt].Cell[/tt] ?

Always wonder if [tt]Option Explicit[/tt] would allow some of the code to compile.
And I know from experience that Excel object (started from VB6) will allow not fully qualified code to run once, but the second time around complains about not fully qualified objects. But that may be just the VB's 'feature'... :)


---- Andy

There is a great need for a sarcasm font.
 
Cell in his code, is IMPLICITLY a Variant data type. As a Variant, it can be used virtually as any kind of variable.

I would not have used “Cell” but in his code, Cell is an Each for the Object in the For...Next statement, and the Each for a Range object happens to be a cell with a implied reference to the Range object.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top