2015-04-08 132 views
1

我无法找到或创建VBA代码,允许从一个细胞中的另一个工作表(Sheet2中)到另一个工作表(工作表Sheet1)以前创建的注释粘贴复制的文本。将文本粘贴到Excel中评VBA

这里是我已经成功迄今编译的代码,而我停留在如何让发现到注释中的文本。

Sub For_Reals() 

'Add Comment 
Sheets("Sheet1").Range("F2").AddComment 
Range("F2").Comment.Visible = False 

'Find Value in Sheet2 based on Value from Sheet1 
Dim FindString As String 
    Dim Rng As Range 
    FindString = Sheets("Sheet1").Range("F2").Value 
    If Trim(FindString) <> "" Then 
     With Sheets("Sheet2").Range("C:C") 
      Set Rng = .Find(What:=FindString, _ 
          After:=.Cells(.Cells.Count), _ 
          LookIn:=xlValues, _ 
          LookAt:=xlWhole, _ 
          SearchOrder:=xlByRows, _ 
          SearchDirection:=xlNext, _ 
          MatchCase:=False) 
      If Not Rng Is Nothing Then 
       Application.Goto Rng, True 
      Else 
       MsgBox "Nothing found" 
      End If 
     End With 
    End If 

'Copy Value 4 cells to the right of found Value 
Selection.Offset(0, 4).Copy 

'Need Code to paste copied value in previously created comment 

End Sub 

回答

0

不是将单元格的值复制粘贴到注释中,而是在创建注释框的同时创建文本。如果评论框已经存在,则会引发错误 - 因此请事先删除该单元格中的任何评论框。

VBA帮助给这个作为一个例子:

Worksheets(1).Range("E5").AddComment "Current Sales" 

所以记住,这个代码就可以了:

Sub For_Reals() 

    'Find Value in Sheet2 based on Value from Sheet1 
    Dim FindString As String 
    Dim Rng As Range 
    FindString = Sheets("Sheet1").Range("F2").Value 
    If Trim(FindString) <> "" Then 
     With Sheets("Sheet2").Range("C:C") 
      Set Rng = .Find(What:=FindString, _ 
         After:=.Cells(.Cells.Count), _ 
         LookIn:=xlValues, _ 
         LookAt:=xlWhole, _ 
         SearchOrder:=xlByRows, _ 
         SearchDirection:=xlNext, _ 
         MatchCase:=False) 
      'Remove any existing comments, create comment and add text. 
      If Not Rng Is Nothing Then 
       Sheets("Sheet1").Range("F2").ClearComments 
       Sheets("Sheet1").Range("F2").AddComment Rng.Offset(0, 4).Value 
       Range("F2").Comment.Visible = True 
      Else 
       MsgBox "Nothing found" 
      End If 
     End With 
    End If 

End Sub 
+0

Darren,非常感谢响应和简化代码。我遇到运行时错误'1004':当我尝试你的版本时,应用程序定义或对象定义的错误。该错误似乎在行中:表格(“Sheet1”)。范围(“F2”)。AddComment Rng.Offset(0,4).Value为什么我会收到错误的任何想法?我正在Microsoft Excel 2013中运行VBA。谢谢,Jeff –

+0

它看起来像rng.Offset(0,4)是空白单元格时会抛出错误。我添加另一个变量“昏暗sCommentText作为字符串”,并设置该保持值“sCommentText = rng.Offset(0,4)。价值”,然后使用这个添加的文本“表(”工作表Sheet“ )。范围(“F2”)。AddComment sCommentText“ 然后它似乎并不介意空白单元格。 –

+0

达伦,这个伎俩!我无法告诉你这是多么可怕,所以非常感谢你的帮助! –

0

最终的代码,我结束了在下面。添加了一个循环来遍历列,并添加了第二个引用,将定义和描述都引入到注释中。谢谢Darren Bartrup-Cook在我被困时帮助我!

Sub Add_Comment_As_Def_Desc_Reference() 
'Posted by Jeff Barrett 2015-04-10  

    Dim FindString1 As String 
    Dim Rng1 As Range 
    Dim sCommentText1 As String 
    Dim sCommentText2 As String 
    Dim str1 As String 
    Dim str2 As String 
    Dim cmmt As String 
    Dim i As Integer   
    str1 = "Definition: " 
    str2 = "Description: "    
'Loop Code, must specify range for i based on # of FieldAlias  
Sheets("Fields").Select 
Range("F4").Select 
For i = 4 To 59   
    'Find Definition & Description in NASDefs based on Value from FieldAlias 
    FindString1 = ActiveCell.Value 
    If Trim(FindString1) <> "" Then 
     With Sheets("NASDefs").Range("C:C") 
      Set Rng1 = .Find(What:=FindString1, _ 
         After:=.Cells(.Cells.Count), _ 
         LookIn:=xlValues, _ 
         LookAt:=xlWhole, _ 
         SearchOrder:=xlByRows, _ 
         SearchDirection:=xlNext, _ 
         MatchCase:=False) 
     End With 
    End If  
      'Remove any existing comments, create comment and add text in FieldAlias 
      If Not Rng1 Is Nothing Then 
       ActiveCell.ClearComments 
       sCommentText1 = Rng1.Offset(0, 4).Value 
       sCommentText2 = Rng1.Offset(0, 5).Value 
       ActiveCell.AddComment.Text Text:=str1 & Chr(10) & Chr(10) & sCommentText1 & Chr(10) & Chr(10) & str2 & Chr(10) & Chr(10) & sCommentText2 
       ActiveCell.Comment.Visible = False 
       ActiveCell.Comment.Shape.AutoShapeType = msoShapeRoundedRectangle      
       'Format lines of text 
        With ActiveCell.Comment.Shape.TextFrame 
          .Characters.Font.ColorIndex = 5 
        End With 
       Else 
       MsgBox "Nothing found" 
      End If 
'End Loop 
ActiveCell.Offset(RowOffset:=1, ColumnOffset:=0).Select 
Next i 
    'Resize Comment to fit text 
    'posted by Dana DeLouis 2000-09-16 
    Dim MyComments As Comment 
    Dim lArea As Long 
    For Each MyComments In ActiveSheet.Comments 
    With MyComments 
     .Shape.TextFrame.AutoSize = True 
     If .Shape.Width > 300 Then 
     lArea = .Shape.Width * .Shape.Height 
     .Shape.Width = 300 
     ' An adjustment factor of 1.1 seems to work ok. 
     .Shape.Height = (lArea/200) * 0.6 
     End If 
    End With 
    Next ' comment 

End Sub