Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 915941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:58:14+00:00 2026-05-15T17:58:14+00:00

So I am working on a reporting tool in Access. It queries the local

  • 0

So I am working on a reporting tool in Access. It queries the local tables and creates a Excel document (through VBA) and makes an Excel file.

I use the following code to color alternative code and it works beautifully

 For a = 1 To rs.RecordCount
    With ExcelSheet          
        .Cells(a + 1, 1) = a 
        .Cells(a + 1, 1).EntireRow.Interior.ColorIndex = IIf((a + 1) Mod 2 = 0, 2, 15)
    End With
Next

Note I have to do a + 1 because a = 1 is the title row, and that is the title row.

Note: .Cells(a + 1, 1) = 1 numbers the rows (1 , 2, 3, …)

Note : IIf((a + 1) Mod 2 = 0, 2, 15) The 2 and 15 are color codes.

Now my question is that when someone gets the Excel report they might delete a row, or do a sort operation or whatever and when they do that, it messes up the rows.

ex:

1  white row
2  grey row
3  white row 
4  grey row

if i sort them I get

3  white row
1  white row 
2  grey row 
4  grey row 

which is not what I want, I want it to keep the formatting and the numbering
Anyone to accomplish this using VBA in Access?

Tech: Office 2007

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T17:58:14+00:00Added an answer on May 15, 2026 at 5:58 pm

    This can be accomplished with the ROW() function and some conditional formatting. The ROW() function returns the current row of the cell it is in, so it will change whenever cells are deleted, moved or sorted. Conditional formatting is re-applied whenever its conditions change, so moving or sorting rows would inform Excel to update the colors accordingly. The code would look as follows:

    Dim a As Integer
    Dim oneRow As Range
    
    For a = 1 To rs.RecordCount 
        With ExcelSheet
    
            ''// show the row number in the first cell
            .Cells(a + 1, 1).Formula = "=ROW()"
    
            ''// set formatting to alternate row colors
            Set oneRow = .Cells(a + 1, 1).EntireRow
            oneRow.FormatConditions.Delete
            oneRow.FormatConditions.Add Type:=xlExpression, Formula1:= _
                "=MOD(ROW()-1, 2)=0"
            oneRow.FormatConditions(1).Interior.ColorIndex = 2
            oneRow.FormatConditions.Add Type:=xlExpression, Formula1:= _
                "=MOD(ROW()-1,2)=1"
            oneRow.FormatConditions(2).Interior.ColorIndex = 15
    
        End With
    Next a
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Subversion working copy with at least one missing file (the local
I'm working on a report in Reporting Services and I can't figure out why
I'm working on a report in SSRS (SQL Server Reporting Services) right now, and
I'm working on a web application that renders a reporting services report as a
Is there any reporting tool which offers multilingual support (namely English en-US and Arabic
brief history. I have a little reporting tool that I am creating that uses
I've been tasked with converting some text log files from a test reporting tool
We are using JasperReports as our reporting tool. Earlier we were using relative paths
I’m currently working on a reporting library as part of a large project. It
I am currently working on a project where reporting services are required. The database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.