Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 从选定范围创建注释_Vba_Excel - Fatal编程技术网

Vba 从选定范围创建注释

Vba 从选定范围创建注释,vba,excel,Vba,Excel,我基本上需要一个宏来将所选内容作为注释插入所选范围。所以基本上我需要有两个选择的范围?这是怎么回事 我的问题是,我对不同工作表中的单元格有行注释。在第二个工作表中,我有列标题,需要插入这些行作为注释 Sub TextIntoComments_GetFromRight() Dim cell As Range Selection.ClearComments For Each cell In Intersect(Selection, ActiveSheet.UsedRange)

我基本上需要一个宏来将所选内容作为注释插入所选范围。所以基本上我需要有两个选择的范围?这是怎么回事

我的问题是,我对不同工作表中的单元格有行注释。在第二个工作表中,我有列标题,需要插入这些行作为注释

Sub TextIntoComments_GetFromRight()
    Dim cell As Range
    Selection.ClearComments
    For Each cell In Intersect(Selection, ActiveSheet.UsedRange)
      If Trim(cell.Offset(0, 1).Text) <> "" Then
        cell.AddComment cell.Offset(0, 1).Text
        cell.Comment.Visible = False
        cell.Comment.Shape.TextFrame.AutoSize = True
      End If
    Next cell
End Sub
子文本输入注释\u GetFromRight()
暗淡单元格作为范围
Selection.ClearComments
对于Intersect中的每个单元格(选择,ActiveSheet.UsedRange)
如果修剪(单元格偏移量(0,1).Text)“,则
cell.AddComment cell.Offset(0,1).Text
cell.Comment.Visible=False
cell.Comment.Shape.TextFrame.AutoSize=True
如果结束
下一个细胞
端接头

以下代码将接受来自用户的两个量程输入。一个用于需要注释的范围,另一个用于注释范围。这两个范围的大小必须相同。然后,它会将第二个范围中的文本作为注释添加到第一个范围中。无论这两个范围在哪张图纸上,这都将起作用

Sub TextIntoComments_GetFromRight()
    Dim CommentRange    As Range
    Dim CellComments    As Range
    Dim cell            As Range
    Dim cell2           As Range
    Dim ws1 As Worksheet, ws2 As Worksheet

    Set CommentRange = Range("A1")
    Set CellComments = Range("A1:A2")
    Do Until CommentRange.Rows.Count = CellComments.Rows.Count And CommentRange.Columns.Count = CellComments.Columns.Count
        Set CommentRange = Application.InputBox("Select the range that needs comments.", Type:=8)
        Set CellComments = Application.InputBox("Select the range of comments to be inserted.", Type:=8)
        If CommentRange.Rows.Count <> CellComments.Rows.Count Or CommentRange.Columns.Count <> CellComments.Columns.Count Then MsgBox "The range sizes do not match. Please select matching range sizes.", vbCritical
    Loop

    Set ws1 = CommentRange.Worksheet
    Set ws2 = CellComments.Worksheet
    CommentRange.ClearComments
    For Each cell In CommentRange
        Set cell2 = ws2.Cells(CellComments.Row + (cell.Row - CommentRange.Row), CellComments.Column + (cell.Column - CommentRange.Column))
        If cell2.Text <> "" Then
            cell.AddComment cell2.Text
            cell.Comment.Visible = False
            cell.Comment.Shape.TextFrame.AutoSize = True
        End If
    Next cell
End Sub
子文本输入注释\u GetFromRight()
将范围作为范围
将注释设置为范围
暗淡单元格作为范围
暗淡的单元格2 As范围
将ws1标注为工作表,将ws2标注为工作表
设置范围=范围(“A1”)
设置单元格注释=范围(“A1:A2”)
直到CommentRange.Rows.Count=CellComments.Rows.Count和CommentRange.Columns.Count=CellComments.Columns.Count
设置CommentRange=Application.InputBox(“选择需要注释的范围”,类型:=8)
设置CellComments=Application.InputBox(“选择要插入的注释范围”,类型:=8)
如果CommentRange.Rows.Count CellComments.Rows.Count或CommentRange.Columns.Count CellComments.Columns.Count,则MsgBox“范围大小不匹配。请选择匹配的范围大小”。vb
环
设置ws1=CommentRange.worket
设置ws2=CellComments.Worksheet
CommentRange.ClearComments
对于范围内的每个单元格
设置cell2=ws2.Cells(CellComments.Row+(cell.Row-CommentRange.Row),CellComments.Column+(cell.Column-CommentRange.Column))
如果是cell2.Text“”,则
cell.AddComment cell2.Text
cell.Comment.Visible=False
cell.Comment.Shape.TextFrame.AutoSize=True
如果结束
下一个细胞
端接头

@Maciej Los well我只试过如何在同一张纸上插入注释。示例右侧的单元格作为注释。Sub Text intoComments_GetFromRight()将单元格作为范围选择进行调整。如果修剪(cell.Offset(0,1).Text)”,则为Intersect(Selection,ActiveSheet.UsedRange)中的每个单元格清除注释,然后是cell.AddComment cell.Offset(0,1).Text cell.Comment.Visible=False cell.Comment.Shape.TextFrame.AutoSize=True End如果下一个单元格结尾小计要向问题添加信息,请按帖子下方的“编辑”按钮。不要在评论中发布代码。@JAI您能使用下面的答案吗?