2015-12-21 96 views
0

我组成了突出显示所选行的代码。如果选择更改 - 新选中的行将突出显示,并且之前选择的格式将返回到初始状态。我用VBA突出显示所选行导致选择整行

  • 列9作为高亮格式样品并作为基线为未选中的行的条件格式
  • 排10。

该代码工作正常。但是,选中单元格后,该行将突出显示,所选单元格保持活动状态,但选中整行。有人可以帮助我取消选择除目标单元格外的所有内容吗?

here没有帮助。

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 

LastRowA = Range("A" & Rows.Count).End(xlUp).Row 

If Target.Cells.Count > 1 Or Target.Cells.Count < 1 Then 'If selected 1 cell 
    'Do nothing 
Else 
    Application.ScreenUpdating = False 
    If Target.Row > 10 And Target.Row < LastRowA + 1 Then 

     Rows("10:10").Copy 'Restore all rows to custom conditional formatting of row 10 
     For tableRow = 11 To LastRowA 
      Rows(tableRow & ":" & tableRow).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False 
     Next tableRow 

     Rows("9:9").Copy 'Highlight active row using formating of row #9 
     Rows(Target.Row).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False 

     Application.CutCopyMode = False 
     Target.Cells.Activate 'Return Target to initially selected cell 
    End If 
    Application.ScreenUpdating = True 
End If 

End Sub 
+0

试着改变你的'行(Target.Row).PasteSpecial'为'细胞(Target.Row,Target.Column).PasteSpecial' – BruceWayne

+0

@BruceWayne - 在这种情况下, ,那么只有一个单元格会粘贴格式。 –

回答

3

试试这个

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 
Dim LastRowA As Long 
Dim tableRow As Long 

LastRowA = Range("A" & Rows.Count).End(xlUp).Row 

If Target.Cells.Count > 1 Or Target.Cells.Count < 1 Then 'If selected 1 cell 
    'Do nothing 
Else 
    Application.ScreenUpdating = False 
    If Target.Row > 10 And Target.Row < LastRowA + 1 Then 

     Rows("10:10").Copy 'Restore all rows to custom conditional formatting of row 10 
     For tableRow = 11 To LastRowA 
      Rows(tableRow & ":" & tableRow).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False 
     Next tableRow 

     Rows("9:9").Copy 'Highlight active row using formating of row #9 
     Rows(Target.Row).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False 

     Application.CutCopyMode = False 
     Target.Cells.Activate 'Return Target to initially selected cell 
     Application.EnableEvents = False 
     Target.Cells.Select 
     Application.EnableEvents = True 
    End If 
    Application.ScreenUpdating = True 
End If 

End Sub 
+2

不错,'EnableEvents = False'。我会补充说,循环可以用'Range(Cells(11,1),Cell(LastRowA,Columns.Count))来代替)。PasteSpecial ...' –

+1

谢谢你们!这工作非常好 – Meursault