gpt4 book ai didi

excel - 循环浏览带有合并单元格的vba中excel工作簿的评论

转载 作者:行者123 更新时间:2023-12-04 22:26:18 25 4
gpt4 key购买 nike

我有一个带有多张工作表的工作簿,其中有评论。我必须遍历每张纸并收集评论。我已经实现了以下逻辑。

For Each Ip_Sheet In ActiveWorkbook.Worksheets
Set Rng = Ip_Sheet.Cells.SpecialCells(xlCellTypeComments)
If Rng Is Nothing Then
MsgBox "No comments in the sheet"
Else
For Each cell In Rng
Comment_Author_NameAndComment = Split(cell.Comment.Text, ":")
AuthName = Comment_Author_NameAndComment(0)
AuthComments = Comment_Author_NameAndComment(1)

如果工作表中没有合并的单元格,则上述逻辑可以正常工作。但是,如果有合并的单元格/行,则循环 For Each cell In Rng为合并的单元格范围中的每个单元格运行。例如,如果列 A:D 被合并,则循环针对每个单元格 A、B、C 和 D 运行,我在 AuthName 中得到相同的值。和 AuthComments变量。

我的问题是,如果我找到合并的单元格,如何使循环跳到工作表上的下一条评论?

编辑:
我还尝试通过以下方法循环浏览工作表中的所有注释,但是该方法不成功 - Rng.Comment对象总是空的。
        For Each cmnt_obj In Rng.Comment
cmt_txt = cmnt_obj.Text
Next cmnt_obj

最佳答案

由于SpecialCells(xlCellTypeComments)返回合并范围的所有单元格,您需要检测单元格何时是命名范围的一部分,并且只处理其中一个单元格。您可以使用 Range.MergeCells检测合并的单元格,Range.MergeArea返回合并范围本身。然后仅在单元格是合并范围的左上角单元格时才报告注释。

像这样的东西:

Sub Demo()
Dim rng As Range
Dim cl As Range
Dim wb As Workbook
Dim ws As Worksheet

Set wb = ActiveWorkbook
For Each ws In wb.Worksheets
Set rng = ws.Cells.SpecialCells(xlCellTypeComments)
If Not rng Is Nothing Then
For Each cl In rng.Cells
If cl.MergeCells Then
If cl.Address = cl.MergeArea.Cells(1).Address Then
ReportComment cl
End If
Else
ReportComment cl
End If
Next
End If
Next
End Sub

Sub ReportComment(cl As Range)
Dim Comment_Author_NameAndComment() As String
Dim AuthName As String
Dim AuthComments As String

Comment_Author_NameAndComment = Split(cl.Comment.Text, ":")
AuthName = Comment_Author_NameAndComment(0)
AuthComments = Comment_Author_NameAndComment(1)
Debug.Print AuthName, AuthComments
'...
End Sub

关于excel - 循环浏览带有合并单元格的vba中excel工作簿的评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57643865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com