+ Reply to Thread
Results 1 to 16 of 16

How to read left side cell value to the comment cell

Hybrid View

  1. #1
    Registered User
    Join Date
    05-09-2010
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003
    Posts
    11

    How to read left side cell value to the comment cell

    I can read all comments in excel file using below code. How do I read left side cell value to the comment cell?

    For Each mySht In Worksheets
        For Each mycomment In Worksheets(mySht.Name).Comments
                    mycomment.Text                                
        Next mycomment
    Next mySht

    sample data:
    A B C Col B comments
    ---------------
    a1 b1 c1 b1 comment
    a2 b2 c2 b2 comment

    expected output:
    ------------------------
    left side cell val Col B comments
    ----------------------------------------------------------
    a1 b1 comment
    a2 b2 comment


    Please find attached test.xls for sample. Thanks.
    Attached Files Attached Files

  2. #2
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: How to read left side cell value to the comment cell

    gsr
    not to sure what you need but this will show the comments
    For Each mySht In Worksheets
        For Each mycomment In Worksheets(mySht.Name).Comments
         MsgBox mycomment.Text
    
        Next mycomment
    Next mySht
    Last edited by pike; 05-13-2010 at 11:57 PM.
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  3. #3
    Registered User
    Join Date
    05-09-2010
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: How to read left side cell value to the comment cell

    I wanted to read left side cell value while reading comments from the worksheet. For example, when code reading Cell D3 comment, I also wanted to read value in the cell C3. When reading cell D4 comment, I wanted to read value in cell C4. Please find attached attached test.xls file to my first post, Sheet "Before" has input data. Sheet "After" shows expected data to be extracted from Sheet "Before". Thanks.

  4. #4
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: How to read left side cell value to the comment cell

    Do you know where all the comments are?

  5. #5
    Registered User
    Join Date
    05-09-2010
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: How to read left side cell value to the comment cell

    There are lot of sheets in workbook and most of the sheets has bunch of comments. I used below code to read all the comments in workbook.

    Open outFilePath For Output As #1
        For Each mySht In wkb.Worksheets
          Print #1, vbCrLf & mySht.Name & vbCrLf
          For Each mycomment In Worksheets(mySht.Name).Comments
            Print #1, "From " & mycomment.Parent.Parent.Name _
                & mycomment.Parent.Address _
                & " Comes the comment: " _
                & mycomment.Text
                                    
        Next mycomment
    Next mySht
    However, I need to read cell value along with comments.
    ex:
    column next to the comment cell
    2 columns over from the comment cell

    Range("E9:G17") - would this look through entire sheet.
    myCell.Comments.Shape.Visible - what this line checks for?

    Thanks.
    Last edited by gsr; 05-15-2010 at 10:58 AM.

  6. #6
    Registered User
    Join Date
    05-09-2010
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: How to read left side cell value to the comment cell

    Pike,
    code you posted works fine within workbook. I'm actually reading comments from another workbook. I modified your code to read from another workbook, I got error at below line, seems like, myCell.Comments is null.
    If myCell.Comments.Shape.Visible = True Then
    Is there something I have to change in code?

    Also, I'm getting error while closing workbook.
    wkb.Close
    is it possible to read comments without opening workbook?

    code to read comments from another spreadsheet:
    ----------------------------------------------------------------------
    
    Dim mycomment As Comment
       Dim mySht As Worksheet
       Dim inputFilePath As String
       Dim outFilePath As String
       inputFilePath = Cells(3, 4)
       outFilePath = Cells(4, 4)
       
       On Error GoTo OpenWorkBook_Err
       
       Application.EnableEvents = False
       Set wkb = Workbooks.Open(inputFilePath)
       Application.EnableEvents = True
       
       Open outFilePath For Output As #1
        
        For Each mySht In wkb.Worksheets
        
        Print #1, vbCrLf & mySht.Name & vbCrLf
          
        For Each myCell In wkb.Worksheets(mySht.Name).Range("E9:G17")
            
            If myCell.Comments.Shape.Visible = True Then
                            
                Print #1, "From " & myCell.Comment.Parent.Parent.Name _
                         & myCell.Comment.Parent.Address _
                         & " Comes the comment: " _
                         & myCell.Comment.Text _
                         & "  OneColumnNext:=" & myCell.Offset(0, 1).Value _
                         & "  TwoColsOver:=" & myCell.Offset(-2, 1).Value _
                         
            End If
         Next myCell
         
    Next mySht
    
    Close #1
    
    wkb.Close
    
    OpenWorkBook_Err:
    Thank you.
    Last edited by gsr; 05-15-2010 at 01:41 PM.

  7. #7
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: How to read left side cell value to the comment cell

    try this
    Worksheets(mySht.Name).Comments(1).Shape.Select
    it comes from the vba help but I cant get in to work #@#$%
    Thinking is that if you can select the cell you can find its address then the offset value

  8. #8
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: How to read left side cell value to the comment cell

    Not pretty but does the job
    Sub test()
        For Each myCell In Range("E9:G17")
            On Error Resume Next
            If myCell.Comments.Shape.Visible = True Then
                MsgBox myCell.Comment.Text & "Offset cell Value:=" & myCell.Offset(0, -1).Value
            End If
        Next myCell
    End Sub

  9. #9
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: How to read left side cell value to the comment cell

    Code tags, dude, can you add please code tags to you posts
    Last edited by pike; 05-14-2010 at 06:33 PM.

  10. #10
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: How to read left side cell value to the comment cell

    Hi gsr

    The
    on error resume next
    as The syntax will away be an error unless the cell contains a Comment

  11. #11
    Registered User
    Join Date
    05-09-2010
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: How to read left side cell value to the comment cell

    pike,
    I'm getting runtime error while closing workbook. I tried adding error handling "on error resume next", however, everytime excution breaks at wbk.Close line. Seems like, workbook is closing when I stopped/end run mode. Can you please suggest how to handle this error.

    wkb.Close
    Run-time error '1004'
    Application-defined or object-defined error

    Thanks.

  12. #12
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: How to read left side cell value to the comment cell

    Can you first create and collate an array of all the workbook values then
    print it to the txt file?
    other wise it not a good option as it uses errors and will err on nearly every cell

  13. #13
    Registered User
    Join Date
    05-09-2010
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: How to read left side cell value to the comment cell

    can you point me to a reference or plz post some code if possible.

    If I collate an array of all the workbook values, wouldn't be an issue while closing workbook?

    Thanks.

  14. #14
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: How to read left side cell value to the comment cell

    you could add the values to current workbook sheet then print form it?

  15. #15
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: How to read left side cell value to the comment cell

    dude,
    This works for me
    Sub tester()
    Dim mycomment As Comment
       Dim mySht As Worksheet
       Dim inputFilePath As String
       Dim outFilePath As String
       inputFilePath = ThisWorkbook.Path & "\scroller_example.xls"
    
       outFilePath = ThisWorkbook.Path & "\tester.txt"
       
       'On Error GoTo OpenWorkBook_Err
       
       Application.EnableEvents = False
       Set wkb = Workbooks.Open(inputFilePath)
       Application.EnableEvents = True
       
       Open outFilePath For Output As #1
        
        For Each mySht In wkb.Worksheets
    
        Print #1, vbCrLf & mySht.Name & vbCrLf
          
        For Each myCell In wkb.Worksheets(mySht.Name).Range("A1:J20")
            On Error Resume Next
            If myCell.Comments.Shape.Visible = True Then
                            
              Print #1, myCell.Comment.Text & " " & mycell.address
    
               
               
               ' Print #1, "From " & myCell.Comment.Parent.Parent.Name _
                '         & myCell.Comment.Parent.Address _
                '         & " Comes the comment: " _
                '         & myCell.Comment.Text _
                '         & "  OneColumnNext:=" & myCell.Offset(0, 1).Value _
                 '        & "  TwoColsOver:=" & myCell.Offset(-2, 1).Value _
    
            End If
         Next myCell
         
    Next mySht
    
    Close #1
    
    wkb.Close SaveChanges:=False
    
    End Sub
    Last edited by pike; 05-16-2010 at 01:31 AM. Reason: SaveChanges:=False

  16. #16
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: How to read left side cell value to the comment cell

    This works for me
    Sub test()
        Dim sourceSheet As Worksheet
        Dim destinationSheet As Worksheet
        Dim destinationRange As Range
        Dim sourceCell As Range, sourceComment As Comment
        Dim commentStr As String, outRRay As Variant
        
        Set sourceSheet = ThisWorkbook.Sheets("Before")
        On Error GoTo Halt
        If sourceSheet.Comments.Count > 0 Then
            On Error GoTo 0
            
            Set destinationSheet = ThisWorkbook.Sheets.Add
            Set destinationRange = destinationSheet.Range("A1:D1")
            destinationRange.Value = Array("More info", "Size", "ID", "Name")
            
            For Each sourceComment In sourceSheet.Comments
                With sourceComment
                    commentStr = .Text
                    commentStr = Application.Substitute(Application.Substitute(commentStr, Chr(10), vbNullString), Chr(13), vbNullString)
                    commentStr = Application.Substitute(commentStr, ",", "=")
                    outRRay = Array(vbNullString, _
                        Split(Split(commentStr, ":")(1), "=")(1), _
                        Split(Split(commentStr, ":")(1), "=")(3), _
                        Split(Split(commentStr, ":")(1), "=")(5))
                    outRRay(0) = sourceComment.Parent.End(xlToLeft).Text
                    With .Parent
                        If .End(xlUp).Row > 1 Then
                            outRRay(0) = outRRay(0) & .End(xlUp).Text
                        End If
                    End With
                End With
                destinationRange.EntireColumn.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Resize(1, 4) = outRRay
            Next sourceComment
        End If
    Halt:
        On Error GoTo 0
    End Sub
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1