如果在列中找到另一个工作表中的Target.value,我正试图弄清楚如何突出显示行(A到Q)。我设法达到了确定是否在另一个工作表中的列中找到Target.Value的点,但不确定如何突出显示仅从列A到Q找到值的行。这是我到目前为止创建的代码。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Lastrow As Long
Dim Lastrow2 As Long
Dim c As Range
Lastrow = ActiveSheet.UsedRange.Rows.Count
Lastrow2 = Worksheets("Action Sheet").UsedRange.Rows.Count
Application.EnableEvents = False
For Each c In Target
If c.Column = 16 Then
Worksheets("Action Sheet").Cells(Lastrow2 + 1, 1).Value = Target.Value
End If
Next c
Application.EnableEvents = True
Set Finder = Sheets("BC Contact List").Range("A:A").Find(Target.Value, LookAt:=xlWhole)
If Not Finder Is Nothing Then
Sheets("BC Contact List").Cells(Finder.Row, 18).Value = "Y"
Sheets("BC Contact List").Activate
ActiveSheet.Range(Cells(Finder.Row, 1), Cells(Finder.Row, 18)).Select
With Selection.Interior
.PatternColorIndex = xlAutomatic
.Color = 5296274
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
End Sub
答案 0 :(得分:0)
提供错误"方法'范围'对象' _Worksheet'失败"本来有助于理解我们如何提供帮助。如果你知道你想要列A:Q着色,只需使用Range方法的重载。
Worksheets("BC Contact List").Range("A" & finder.Row & ":Q" & finder.Row).Select
您现有代码的问题在于您尝试在" BC联系人列表中选择范围"使用对已触发Worksheet_Change事件的工作表的Cells对象的引用。选择该范围的正确方法是(使用Worksheet对象以简化):
Dim ws as Worksheet
Set ws = Worksheets("BC Contact List")
ws.Range(ws.Cells(finder.Row, 1), ws.Cells(finder.Row, 18)).Select
工作代码:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Worksheets("BC Contact List")
Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Worksheets("Action Sheet")
Dim lastRow As Long: lastRow = ws2.UsedRange.Rows.Count
If (Not (Intersect(Range("P:P"), Target) Is Nothing)) Then
Dim finder As Range: Set finder = ws1.Range("A:A").Find(Target.Value, LookAt:=xlWhole)
If (Not (finder Is Nothing)) Then
ws1.Cells(finder.Row, 18).Value = "Y"
With ws1.Range("A" & finder.Row & ":Q" & finder.Row).Interior
.PatternColorIndex = xlAutomatic
.Color = 5296274
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
End If
Application.EnableEvents = True
End Sub
几点说明:
Application.ScreenUpdating = False
放在例程的开头,将Application.ScreenUpdating = True
放在例程的末尾(或者例程的任何退出)