求VBA代码 excel中选中单元格,高亮显示这行每个单元格边框和整行!

excel中选中单元格,高亮显示这行每个单元格边框(是每一个单元格,有颜色的边框)和整行(有背景颜色),移除边框和行恢复原来的格式!请帮忙写段VBA!
想在那个worksheet中实现以上功能,
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With ActiveSheet
.Range("A1:IV65536").Interior.ColorIndex = 0
.Range("A1:IV65536").Borders.LineStyle = 0
.Range(.Cells(Target.Row, 1), .Cells(Target.Row, 256)).Interior.ColorIndex = 6
.Range(.Cells(Target.Row, 1), .Cells(Target.Row, 256)).Borders.ColorIndex = 5
End With
End Sub
能不能保留原来单元格的边框颜色和背景颜色,用这代码原来的格式就没有了???

Private r1 As Long '前一个选定的行号
public Const r0 = 65536 '可以调整为不使用的行号
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False
If r1 > 0 Then
Rows(r0).Copy
Rows(r1).PasteSpecial Paste:=xlPasteFormats
End If
r1 = Target.Row
Rows(r1).Copy
Rows(r0).PasteSpecial Paste:=xlPasteFormats
Rows(r1).Interior.ColorIndex = 6
Rows(r1).Borders.ColorIndex = 5
Application.CutCopyMode = False
Target.Select
Application.EnableEvents = True
End Sub

'在退出时,需要恢复:
'在ThisWorkbook中,加入以下4行代码(删除第一个')
'Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Sheets("Sheet1").Select 'Sheet1, 需要调整为调整了颜色所在的表名称
'Range("A65536").Select
'End Sub追问

public Const r0 = 65536 '可以调整为不使用的行号 提示编译错误?是不是第一段代码复制到sheet1就可以了?第二段代码复制到ThisWorkbook?

追答

把public 改为 private
是的,注意删除前面的 “'”

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-03-27
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With ActiveCell.CurrentRegion
.Interior.ColorIndex = 0
.Borders.LineStyle = 1
.Cells(ActiveCell.Row, 1).Resize(1, .Columns.Count).Interior.ColorIndex = 6
.Cells(ActiveCell.Row, 1).Resize(1, .Columns.Count).Borders.ColorIndex = 5
End With
End Sub
相似回答