We help IT Professionals succeed at work.
Get Started

Conditional Format Duplicates in Excel from Access

460 Views
Last Modified: 2016-02-12
Using Microsoft Access/VBA, I'm copying a recordset from an Access query into an Excel spreadsheet.  I'd like to use conditional formatting on column N to check for duplicates and highlight the cells in red.

I'm having a hard time finding a solution for this.  Any help is greatly appreciated!

Function CDataAudit
Dim Rst As DAO.Recordset, i As Long
Dim ExlApp As Object, ExlBook As Object, ExlSheet As Object
On Error GoTo Function_Error

Dim strTemp As String: strTemp ='location of template
Dim strXLS As String: strXLS='location of output

Set ExlApp = CreateObject("Excel.Application")
Set ExlBook = ExlApp.Workbooks.Open(strTemp)
Set Rst = CurrentDb.OpenRecordset("qselDataAudit")

If Not Rst.EOF Then
    Rst.MoveLast
    i = Rst.RecordCount
End If

Rst.MoveFirst

With ExlBook
    .ActiveSheet.Range("A2").CopyFromRecordset Rst
    .Worksheets(1).Range("H2:H" & i).NumberFormat = "$#,##0.00"
    .Worksheets(1).Range("J2:J" & i).NumberFormat = "$#,##0.00"
    .Worksheets(1).Range("L2:L" & i).NumberFormat = "$#,##0.00"
End With

With ExlApp
    .ActiveWorkbook.SaveAs (strXLS)
End With

Function_Exit:
    If Not Rst Is Nothing Then
        Rst.Close
        Set Rst = Nothing
    End If
    If Not ExlSheet Is Nothing Then
        Set ExlSheet = Nothing
    End If
    If Not ExlBook Is Nothing Then
        Set ExlBook = Nothing
    End If
    If Not ExlApp Is Nothing Then
        ExlApp.Quit
        Set ExlApp = Nothing
    End If
        Exit Function
Function_Error:
    MsgBox Error$
    Resume Function_Exit
End Function

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Top Expert 2015
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE