Results 1 to 4 of 4

Thread: [RESOLVED] How to highlight a cell ?

  1. #1

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Resolved [RESOLVED] How to highlight a cell ?

    My Excel Macro
    ++++++++++++++

    I have written a macro in Excel that allows me to check for the differences in outputs of two queries.
    Now my boss wants me to highlight the cells instead of changing the font to bold and red.

    I wrote some code after having experimented with excel a bit, but thats not working.

    Here is my code.
    VB Code:
    1. Sub MarkUniques()
    2.    
    3.     'Date       - 05-August-2005
    4.     'Author     - Abhijit Shrikhande
    5.     'Purpose    - To check the output from two different result sets.
    6.     '           - Works by highlighting the cells that are different.
    7.    
    8.     Dim iColCounter As Integer
    9.     Dim iColsToCheck As Integer
    10.     Dim lRowCounter As Long
    11.     Dim lRowsToCheck As Long
    12.    
    13.    
    14. 'Find out number of columns
    15.     iColCounter = 1
    16.     Do While Trim(Sheet1.Cells(1, iColCounter)) <> ""
    17.         iColCounter = iColCounter + 1
    18.     Loop
    19.         iColsToCheck = iColCounter - 1
    20.  
    21. 'Find out number of rows
    22.     lRowCounter = 1
    23.     Do While Trim(Sheet1.Cells(lRowCounter, 1)) <> ""
    24.         lRowCounter = lRowCounter + 1
    25.     Loop
    26.         lRowsToCheck = lRowCounter - 1
    27.  
    28. 'Start from 2nd Row, 1st Column
    29.     For lRowCounter = 2 To lRowsToCheck
    30.         For iColCounter = 1 To iColsToCheck
    31.             If Worksheets(1).Cells(lRowCounter, iColCounter) <> Worksheets(2).Cells(lRowCounter, iColCounter) Then
    32.                     Worksheets(2).Cells(lRowCounter, iColCounter).Font.Bold = True
    33.                     Worksheets(2).Cells(lRowCounter, iColCounter).Font.Color = vbRed
    34.                     '<--'Code fails at the next line -->
    35.                     Worksheets(2).Range(lRowCounter, iColCounter).Select
    36.                     With Selection.Interior
    37.                         .ColorIndex = 6
    38.                         .Pattern = xlSolid
    39.                     End With
    40.             End If
    41.         Next iColCounter
    42.     Next lRowCounter
    43. End Sub

    Cheers,
    Abhijit
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: How to highlight a cell ?

    On a different note; As an alternate to looping you can determine the last Row/Column used using UsedRange like:

    VB Code:
    1. intRowCount = objExcel.ActiveSheet.UsedRange.Rows.Count 'Capture the number of USED Rows
    2.     intColCount = objExcel.ActiveSheet.UsedRange.Columns.Count  'Capture the number of USED Columns


    Here is an example: http://www.vbforums.com/showthread.php?threadid=306042

    Bruce.

  3. #3
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: How to highlight a cell ?

    It the use of Range that is the problem in this case, use Cells instead.

    I have successfully used Worksheets(2).Cells(lRowCounter, iColCounter).Select

  4. #4

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: How to highlight a cell ?

    Thanks mate,
    Got that done now.

    Btw - Do you know how to unselect a cell?
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width