2014-11-04 94 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

相關問題