所以我基本上试图插入注释并为基本符合我在代码中设置的条件的单元格着色。我搜遍了所有但似乎无法找到可行的解决方案。
这是我到目前为止的代码,我在下面的代码中提到了我想要的颜色和注释。我有这个宏设置的方式是它得到"被叫"来自工作表。我使用了Selection_Change函数。所以我有一个范围,在一列中有人输入数据,然后输入任何数据,下面的宏运行并检查它是否在限制范围内。
如果它不在excel表中设置的限制范围内(" M7"和" M19"),我想要一种颜色来突出显示某个单元格和一组评论那个单元格。我该怎么做?我非常感谢你的帮助。谢谢!
我也在网上找到了一个代码,我的问题是当我使用
时ActiveCell.AddComment ("Text")
我一直收到错误,在我输入数据点后按Enter键,评论进入下一个单元格。
这是被调用的宏:
Option Explicit
Public Sub OutofControlRestofData()
Dim lRow As Long
Dim lstRow As Long
Dim data As Variant
Dim ul As Variant
Dim ll As Variant
Dim wb As Workbook
Dim ws As Worksheet
With Application
.ScreenUpdating = True
.EnableEvents = True
.DisplayAlerts = True
End With
Set ws = Sheets(2)
ws.Select
lstRow = WorksheetFunction.Max(1, ws.Cells(Rows.Count, "R").End(xlUp).Row)
For lRow = 1 To lstRow
data = Cells(lRow, "E").Value
ul = Range("M7")
ll = Range("M19")
If data > ul Or data < ll Then
If IsNumeric(data) = True And data Like "" = False Then
MsgBox ("There was an Out of Control Point at " & Cells(lRow, "C").Value)
'THIS IS WHERE I THINK THE COMMENTING AND COLOR CODE WOULD BE
End If
End If
Next lRow
End Sub
此处还有调用宏的代码:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("E39:E138")) Is Nothing Then
Run ("OutofControlRestofData")
End If
End Sub
答案 0 :(得分:1)
为了安全起见,我建议您在此处更改代码以包含value
:
data = Range("E" & lRow).Value
ul = Range("M7").Value
ll = Range("M19").Value
然后在你想要做颜色/评论的地方:
Range("E" & lRow).Interior.Color = RGB(255, 0, 0)
Range("E" & lRow).AddComment("This is an Out of Control Point")
答案 1 :(得分:1)
有几点需要注意。
If
陈述。使
它更清楚。Target
&#34;那么您就不需要循环。因为那是你要检查的单元格(范围)。E39
和E138
之间时才有效。这总是如此吗?如果您希望更灵活地扩展工作表和数据,请考虑使用整个column E
。代码:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws As Worksheet
Set ws = Sheets(2)
If Not Intersect(Target, ws.Range("E39:E138")) Is Nothing Then
Dim lRow As Long
Dim lstRow As Long
Dim data As Variant
Dim ul As Variant
Dim ll As Variant
Dim wb As Workbook
Dim ws As Worksheet
With Application
.ScreenUpdating = True
.EnableEvents = True
.DisplayAlerts = True
End With
data = Target.Value
ul = Range("M7").Value
ll = Range("M19").Value
If data > ul Or data < ll Then
If IsNumeric(data) = True And data Like "" = False Then
MsgBox ("There was an Out of Control Point at " & Target.Address)
Target.Interior.Color = RGB(255, 0, 0)
Target.AddComment ("This is an Out of Control Point")
End If
End If
End If
End Sub