突出显示整行

时间:2017-02-24 16:40:41

标签: excel vba excel-vba

每当我尝试突出显示D列时,我都会收到“运行时错误'13'”。当我点击调试时,它会从代码If Target.Value = "Closed" Or Target.Value = "Closed" Then中突出显示这一部分。我非常感谢有关如何解决这个问题的任何建议。

编辑*

Private Sub Worksheet_SelectionChange(ByVal Target As Range)


Dim wsd As Worksheet
Dim wsc As Worksheet
Application.ScreenUpdating = False
Set wsd = Sheets("Pipeline")
Set wsc = Sheets("Closed")
If Not Intersect(Target, Range("D6:D65536")) Is Nothing Then
  If Target.Value = "Closed" Or Target.Value = "Closed" Then
erow = Target.Row
MsgBox "Moved to Closed"
numberofrow = wsc.Range("A65536").End(xlUp).Row
For i = 1 To numberofrow
If wsd.Cells(erow, 1) = wsc.Cells(i, 1) Then
Exit Sub
End If
Next

wsd.Range("A" & erow & ":A" & erow).EntireRow.Copy wsc.Range("A" &   numberofrow + 1)
With Target.Parent
  Union(.Range(.Cells(Target.Row, "A"), .Cells(Target.Row, "V")), _
      .Range(.Cells(Target.Row, "Y"), .Cells(Target.Row,    .Columns.Count))).ClearContents
`enter code here`End With
wsc.Activate
ActiveSheet.Range("A2:D" & numberofrow + 1).Select
ActiveWorkbook.Worksheets("Closed").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Closed").Sort.SortFields.Add Key:=Range("C2:C" &   numberofrow + 1), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Closed").Sort
.SetRange Range("A2:D" & numberofrow + 1)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
wsd.Activate
End If
End If


End Sub

我必须将一些东西拼凑在一起才能让它发挥作用,这就是为什么它非常难看。我无法弄清楚在哪里放保护条款。到目前为止,这已经适用于我需要它(除了运行时错误),根据输入到D列的销售阶段将帐户转移到单独的工作表。再次,我感谢所有帮助。

1 个答案:

答案 0 :(得分:0)

您的处理程序假定只选择了1个单元格。当您突出显示整个列时,Target代表所有选定的单元格,然后Target.Value无法进行有意义的评估,因此会引发运行时错误。

您需要修改处理程序以删除该假设,as was suggested

If Target.Cells.Count > 1 Then Exit Sub

据推测,事件处理程序需要进行更多调整以验证Target是宏认为/假设它是什么,但考虑到您提供的信息,可能是它。