Just a word of caution with the macro recorder - it mimics every time you
select a cell and builds code that is very long. Often you can compact the
lines and get more robust code. A small example:
Recorder:
' just copying from cell A1 on Sheet1 to cell A1 on Sheet2
Workbooks("Test1.xls").Activate
Sheets("Sheet1").Select
Range("A1").Select
Selection.Copy
Sheets("Sheet2").Select
Range("A1").Paste
Alternative:
Workbooks("Test1.xls").Sheets("Sheet1").Range("A1").Copy
Workbooks("Test1.xls").Sheets("Sheet2").Range("A1").Paste
This has the advantage that you don't need to have the Test1.xls book active
for the code to work.
To answer your question, the following will do the trick:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' clear previous colour
Target.Parent.Cells.Interior.ColorIndex = xlColorIndexNone
Target.Interior.ColorIndex = 3 ' <--- change this number to get a
different colour
' or use
Target.Interior.Color = RGB (255,0,0)
End Sub
Paste this code into the module for the sheet in which you want to highlight
the cells. This will only work on that sheet though.
--
Simon
Andibevan said:
Trying recording a macro that does what you want (Tools>Macros) then look at
the created code in the VB editor (Press Alt+F11).
This is quite a good way of learning the basics. The code generated when
you do this will be exactly what you want