2014-11-04 120 views
0

如果cell.offset(,45)= 1VBA:如果其他单元格的内容为其他单元格的内容添加注释= 1

我需要使用cell.offset(,23)

这是我尝试:

Sub CellToCmt2() 

    Dim c As Range 
    For Each c In Selection 
    If target.Offset(, 45) <> 1 Then Resume Next 
    If target.Offset(, 45) = 1 Then 
     Application.EnableEvents = False 
     On Error Resume Next 
     With c.Offset(, 23) 
     .NoteText Text:=.Value 
     End With 
     target.ClearContents 
     On Error GoTo 0 
    End If 
    Next c 

End Sub 

但因为我有一点VBA的知识,我不断收到错误。

任何帮助,将不胜感激

编辑:这是我的作品的代码

Sub CellToCmt2() 
Dim c As Range 
For Each c In Selection 
If c.Offset(, 45) = 1 Then 
    On Error Resume Next 
    With c 
     c.NoteText Text:=c.Offset(, 23).Value 
    End With 
    target.ClearContents 
    On Error GoTo 0 
End If 
Next c 
End Sub 
+1

你得到了什么错误? – Fred 2014-11-04 13:45:42

+0

运行时错误'13'使用修订后的代码尝试Steve S – 2014-11-04 14:54:33

+0

什么是'target',它在哪里声明?你知道哪一行代码导致错误吗? – Fred 2014-11-04 15:09:24

回答

0

原因之一,此位不执行任何操作:

If target.Offset(, 45) <> 1 Then Resume Next 

不管条件是否满足,你沿着下一步移动。也可以把它拿出来。

此外,当我使用宏录制输入评论我得到的代码:

c.Comment.Text Text:="test" 

所以也许尝试 “Comment.Text” 而不是 “NoteText”

编辑:

你可能想改变

With c.Offset(, 23) 
    .NoteText Text:=.Value 
End With 

c.NoteText Text:=c.Offset(, 23).Value 
+0

谢谢,这是摆脱了错误,但它没有添加评论 – 2014-11-04 15:45:22

+0

要清楚,你正在寻找一个“1 “在某些”搜索索引“列的右侧有45列,如果找到了,您希望将注释文本放在右侧的23列中,对吗?你知道列“c.Offset(,23)”是从它自己的单元格值获取评论吗? – 2014-11-04 15:51:14

+0

这是正确的,没有我没有 – 2014-11-04 16:01:30

0

应该targetc?例如:

If c.Offset(, 45) <> 1 Then Resume Next 
If c.Offset(, 45) = 1 Then 
+0

感谢您的回应。 – 2014-11-04 14:42:56

+0

刚刚尝试过,但没有运气,我得到运行时错误'13'类型不匹配 – 2014-11-04 14:45:49

相关问题