Consulting

Results 1 to 6 of 6

Thread: Need a little help with my (working) code regarding putting headers into comments

  1. #1
    VBAX Regular
    Joined
    Dec 2014
    Posts
    25
    Location

    Need a little help with my (working) code regarding putting headers into comments

    Hello

    I currently have the following code (which works a treat) and it puts all the data in A2, B2, C2, D2 etc into a comment in A2.

    However I also have headers in A1, B1, C1, and I would like them adding like the screenshot if anyone could give me the code?

    Thanks!

    Current Code:-
    Dim cmt As Comment 
    Dim rng As Range 
     
    Set rng = Range("A1") 
     
    Do 
        Set cmt = rng.Comment 
         
        If cmt Is Nothing Then 
            rng.AddComment 
            Set cmt = rng.Comment 
        End If 
         
        cmt.Text Join(Application.Transpose(Application.Transpose(rng.Resize(, 6).Value)), Chr(10)) 
         
        cmt.Visible = True ' optional
         
        Set rng = rng.Offset(1) 
    Loop Until rng.Value = ""
    Click on picture:-
    comment_picture.jpg

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Your code doesn't quite match your picture but it will be something like:
        Dim cmt                   As Comment    Dim rng                   As Range
        Dim n                     As Long
        Dim sComment              As String
    
    
        Set rng = Range("A1")
    
    
        Do
            Set cmt = rng.Comment
    
    
            If cmt Is Nothing Then
                rng.AddComment
                Set cmt = rng.Comment
            End If
            For n = 1 To 6
                sComment = sComment & vbLf & Cells(1, n).Value & ": " & Cells(rng.Row, n).Value
            Next n
            cmt.Text Mid$(sComment, 2)
    
    
            cmt.Visible = True    ' optional
    
    
            Set rng = rng.Offset(1)
        Loop Until rng.Value = ""
    Be as you wish to seem

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    hi.

    i would include below line in the loop as well...

    cmt.Shape.TextFrame.AutoSize = True
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  4. #4
    VBAX Regular
    Joined
    Dec 2014
    Posts
    25
    Location
    Hello Aflatoon

    Thankyou for your help, it's 99% working. However what is happening is this (I have taken a picture to explain it a lot better);-
    (it should not be copying the previous data)

    comment_picture2.jpg

  5. #5
    VBAX Regular
    Joined
    Dec 2014
    Posts
    25
    Location
    Hello Mancubus

    I have done this and the formatting is much better now, thanks agian for your input and help.

    Quote Originally Posted by mancubus View Post
    hi.

    i would include below line in the loop as well...

    cmt.Shape.TextFrame.AutoSize = True

  6. #6
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Sorry that was silly. Add this after the Do line:
    sComment = vbNullstring
    Be as you wish to seem

Posting Permissions

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