在另一个表格中总结行数据和带线程的评论时出现错误。

huangapple go评论62阅读模式
英文:

Error in Summarizing Row Data and Threaded Comments in Another Sheet

问题

在一个包含学生数据的工作表中,我试图自动化从一个工作表中获取水平数据并在另一个工作表中垂直总结它。第一个工作表将垂直总结学生姓名,水平总结相应的数据。第二个工作表将只是一个临时报告,显示一个学生的数据。我无法弄清楚这个问题,已经研究了很多页面,包括:https://stackoverflow.com/questions/60757377/excel-vba-read-cell-comment,https://stackoverflow.com/questions/66852586/reading-excel-cell-comments-gives-vba-error。任何帮助将不胜感激。

期望的功能:

  • 单击列A中的偶数单元格以运行学生数据的报告。
  • 每个学生的数据占用2行,即偶数行和下一个奇数行。
  • 循环遍历2行范围中的每个单元格,将数据放入另一个临时工作表中。
  • 学生姓名在B列中。
  • 学生数据从E列开始,根据需要扩展到右侧的列。
  • 许多数据单元格包含线程化注释,这些注释需要包含在内,但以垂直方式放置在单元格中。

如果需要任何额外信息,请告诉我。该代码能够插入新的临时工作表并插入标题(Number,Author,Date,Text),但是没有传输单元格数据和线程化注释数据。谢谢。

Sub ListCommentsThreaded()
    Application.ScreenUpdating = False
    
    Dim wb As Workbook
    Dim myCmt As CommentThreaded
    Dim curwks As Worksheet
    Dim newwks As Worksheet
    Dim currng As Range
    Dim cell As Range
    Dim i As Long
    Dim cmtCount As Long
    
    Set wb = ThisWorkbook
    Set curwks = ActiveSheet
    
    '当前范围将是两行,从B列开始
    '范围长度(列数)将根据学生而变化,任意设置为90列
    '范围中的许多单元格将具有线程化注释,从E列开始
    '某些单元格可能缺乏注释
    
    'Set currng = curwks.Range(ActiveCell.Offset(0, 0), ActiveCell.Offset(1, 90))
    Set currng = curwks.Range(ActiveCell.Resize(1, 90).Address)
    
    Set newwks = wb.Worksheets.Add
    
    newwks.Range("B5:E5").Value = Array("Number", "Author", "Date", "Text")
          
    curwks.Activate
    i = 0
    For Each cell In currng.Cells
        If Not IsNumeric(cell.Value) Then
            With cell
                If Not .CommentThreaded Is Nothing Then
                    With newwks
                        i = i + 1
                        On Error Resume Next
                        .Cells(i, 1).Value = i - 1
                        .Cells(i, 2).Value = myCmt.Author.Name
                        .Cells(i, 3).Value = myCmt.Date
                        .Cells(i, 4).Value = myCmt.Text
                    End With
                End If
            End With
        End If
    Next cell
    
    With newwks
        .Columns(4).ColumnWidth = 50
        .Columns.AutoFit
        With .Cells
            .EntireRow.AutoFit
            .VerticalAlignment = xlTop
            .WrapText = True
        End With
    End With
    
    Application.ScreenUpdating = True
    
End Sub
英文:

In a sheet containing student data, I'm trying to automate taking horizontal data from one sheet and summarizing it vertically in another sheet. The 1st sheet tallies student names vertically and corresponding data horizontally. The 2nd sheet will just be a temporary report showing just one student's data. I cannot figure this out and have studied many pages, including: https://stackoverflow.com/questions/60757377/excel-vba-read-cell-comment, https://stackoverflow.com/questions/66852586/reading-excel-cell-comments-gives-vba-error. Any help would be greatly appreciated.

DESIRED FUNCTIONALTY:

  • Click in an even cell in col A to run a report of student data.
  • Each student's data occupies 2 rows, the even row and the next odd row.
  • Loop through each cell in the range of the 2 rows, placing the data in another temporary sheet.
  • Student names are in column B.
  • Student data starts in column E and expands columns to the right as needed.
  • Many of the data cells contain threaded comments, and these comments need to be included but placed vertically into cells.

Please let me know if you need any additional info. The code is able to insert the new, temp worksheet and insert the headings (Number, Author, Date, Text), but no cell data and threaded comments data are being transferred over. Thank you.

Sub ListCommentsThreaded()
    Application.ScreenUpdating = False
    
    Dim wb As Workbook
    Dim myCmt As CommentThreaded
    Dim curwks As Worksheet
    Dim newwks As Worksheet
    Dim currng As Range
    Dim cell As Range
    Dim i As Long
    Dim cmtCount As Long
    
    Set wb = ThisWorkbook
    Set curwks = ActiveSheet
    
    'THE CURRENT RANGE WILL BE A SET OF TWO ROWS, STARTING IN COL B
    'THE RANGE LENGTH (# COLUMNS) WILL VARY PER STUDENT, ARBITRARILY SET AT 90 COLS
    'MANY OF THE CELLS IN THE RANGE WILL HAVE THREADED COMMENTS, STARTING IN COLUMN E
    'SOME CELLS MAY LACK COMMENTS
    
    'Set currng = curwks.Range(ActiveCell.Offset(0, 0), ActiveCell.Offset(1, 90))
    Set currng = curwks.Range(ActiveCell.Resize(1, 90).Address)
    
    Set newwks = wb.Worksheets.Add
    
    newwks.Range("B5:E5").Value = Array("Number", "Author", "Date", "Text")
          
    curwks.Activate
    i = 0
    For Each cell In currng.Cells
        If Not IsNumeric(cell.Value) Then
            With cell
                If Not .CommentThreaded Is Nothing Then
                    With newwks
                        i = i + 1
                        On Error Resume Next
                        .Cells(i, 1).Value = i - 1
                        .Cells(i, 2).Value = myCmt.Author.Name
                        .Cells(i, 3).Value = myCmt.Date
                        .Cells(i, 4).Value = myCmt.Text
                    End With
                End If
            End With
        End If
    Next cell
    
    With newwks
        .Columns(4).ColumnWidth = 50
        .Columns.AutoFit
        With .Cells
            .EntireRow.AutoFit
            .VerticalAlignment = xlTop
            .WrapText = True
        End With
    End With
    
    Application.ScreenUpdating = True
    
    End Sub

答案1

得分: 3

这对我来说是有效的:

子过程 列出评论线程()
    
    应用程序.屏幕更新 = 假
    
    Dim wb 作为 工作簿
    
    Dim curwks 作为 工作表,newwks 作为 工作表
    Dim currng 作为 范围,rw 作为 范围,cell 作为 范围
    Dim i 作为 长整型,cmtCount 作为 长整型,v
    
    设定 curwks = 活动工作表
    设定 wb = 本工作簿
    
    设定 currng = 活动单元格.调整大小(2, 90)
    
    设定 newwks = wb.工作表.添加
    设定 rw = newwks.范围("B5:G5")
    rw.数值 = 数组("编号", "地址", "单元格数值", "Cmt. 作者", "Cmt. 日期", "Cmt. 文本")
    
    i = 0
    对 每个 cell 在 currng.单元格中
        v = cell.数值
        如果 不是数值(v) 则
            i = i + 1
            设定 rw = rw.偏移(1)
            rw.单元格(1).数值 = i
            rw.单元格(2).数值 = cell.地址(False, False)
            rw.单元格(3).数值 = v
            '此单元格有评论吗?
            如果 不是 cell.评论线程 为空 则
                以 cell.评论线程 为对象
                    rw.单元格(4).数值 = .作者.名称
                    rw.单元格(5).数值 = .日期
                    rw.单元格(6).数值 = .文本
                结束 以
            结束 '有评论
        结束 如果 '不是数值
    结束 对 '每个单元格
    
    以 newwks 为对象
        .列(4).列宽 = 50
        .列.自适应
        以 .单元格 为对象
            .整行.自适应
            .垂直对齐 = xlTop
            .换行 = 真
        结束 以
    结束 以 newwks

    应用程序.屏幕更新 = 真
    
结束 子过程
英文:

This works for me:

Sub ListCommentsThreaded()
    
    Application.ScreenUpdating = False
    
    Dim wb As Workbook
    
    Dim curwks As Worksheet, newwks As Worksheet
    Dim currng As Range, rw As Range, cell As Range
    Dim i As Long, cmtCount As Long, v
    
    Set curwks = ActiveSheet
    Set wb = ThisWorkbook
    
    Set currng = ActiveCell.Resize(2, 90)
    
    Set newwks = wb.Worksheets.Add
    Set rw = newwks.Range("B5:G5")
    rw.Value = Array("Number", "Address", "Cell Value", _
                     "Cmt. Author", "Cmt. Date", "Cmt. Text")
    
    i = 0
    For Each cell In currng.Cells
        v = cell.Value
        If Not IsNumeric(v) Then
            i = i + 1
            Set rw = rw.Offset(1)
            rw.Cells(1).Value = i
            rw.Cells(2).Value = cell.Address(False, False)
            rw.Cells(3).Value = v
            'does this cell have a comment?
            If Not cell.CommentThreaded Is Nothing Then
                With cell.CommentThreaded
                    rw.Cells(4).Value = .Author.Name
                    rw.Cells(5).Value = .Date
                    rw.Cells(6).Value = .Text
                End With
            End If 'has comment
        End If     'not numeric
    Next cell
    
    With newwks
        .Columns(4).ColumnWidth = 50
        .Columns.AutoFit
        With .Cells
            .EntireRow.AutoFit
            .VerticalAlignment = xlTop
            .WrapText = True
        End With
    End With
    
    Application.ScreenUpdating = True
    
End Sub

huangapple
  • 本文由 发表于 2023年7月10日 20:52:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76653935.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定