• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Copying a table into a comment?

Flopski

New Member
I was wondering if this was possible? I can save the table as a picture (Say "Picture 2") or a linked picture. How can I then add that to a comment? I have seen where picture files are used in some macros to achieve this but I am not sure whether this can be modified to use a Picture object from within Excel rather than an external file.

Thanks for any thoughts and assitance on this.
 
Hi, Flopski!

As a new user you might want (I'd say should and must) read this:
http://chandoo.org/forum/forums/new-users-please-start-here.14/

And regarding your issue, I think you're talking of this (or something alike):
http://chandoo.org/forum/threads/insert-photos-in-excel-upon-name-change-in-a-cell.3321/#post-16199

I use to do this with external files, almost always images or .pdf files, but I never tried to put a range in there. If you're not hurried, give me a little of time and I'll come back and tell you if I succeeded or not.

Regards!
 
Last edited:
Hi, Flopski!

Give a look at the uploaded file. A bit tricky, but not quick & dirty. Here's the code:
Code:
Option Explicit

Sub CheatingUserpictureRestrictions()
    ' constants
    Const ksFileExtension = ".jpg"
    ' declarations
    Dim cht As Chart
    Dim sFile As String, lWidth As Long, lHeight As Long
    ' start
    sFile = ActiveWorkbook.Path & Application.PathSeparator & _
        Format(Now(), "yyyymmddhhmmss") & ksFileExtension
    ' process
    '  build image file
    With [Tabla1[#All]]
        lWidth = .Width
        lHeight = .Height
        .CopyPicture Appearance:=xlScreen, Format:=xlBitmap
        Set cht = ActiveSheet.ChartObjects.Add(.Top, .Left, lWidth, lHeight).Chart
    End With
    With cht
        .Paste
        .ChartArea.Border.LineStyle = 0
        .Export sFile
        .Parent.Delete
    End With
    Application.CutCopyMode = False
    '  load image file
    With [CommentCell]
        If Not (.Comment Is Nothing) Then .ClearComments
        .AddComment
        With .Comment
            With .Shape
                .Fill.UserPicture sFile
                .Width = lWidth
                .Height = lHeight
            End With
            .Visible = True
        End With
    End With
    '  destroy image file
    Kill sFile
    ' end
    Set cht = Nothing
    Beep
End Sub

Just advise if any issue.

Regards!
 

Attachments

  • Copying a table into a comment_ (for Flopski at chandoo.org).xlsm
    28.1 KB · Views: 6
Hi, Flopski!

As a new user you might want (I'd say should and must) read this:
http://chandoo.org/forum/forums/new-users-please-start-here.14/

And regarding your issue, I think you're talking of this (or something alike):
http://chandoo.org/forum/threads/insert-photos-in-excel-upon-name-change-in-a-cell.3321/#post-16199

I use to do this with external files, almost always images or .pdf files, but I never tried to put a range in there. If you're not hurried, give me a little of time and I'll come back and tell you if I succeeded or not.

Regards!

Thanks for the reply on this. I have looked through number of posts on similar issues like the one you mention above but the "input" into the comments box always seems to come from an external file or web page rather than anything from within excel. Your macro seems to work ok ... I need to alter to see if it can copy a normal range ofdata or an excel Picture object of that data. I presume I need to alter the reference to the Tabla1[#All] to try and reference the range of cells or "Picture 2" if I copy it as a linked picture.
 
Hi, Flopski!

As you correctly wrote the input for a comment must be always a file yet stored in a disk, not in memory, since the method UserPicture for the Shape object fill requires an external file.

In my code I'm actually creating a file via exporting a chart object with an image pasted, created by the CopyPicture method of the Range object, so it'd work either with a structured table or with any range reference.

I recommend you to access thru the Range.CopyPicture method instead thru the ActiveSheet.Pictures(ActiveSheet.Pictures.Count)) to make things easier. But if you still choose this last, use the reference to the last created picture (.Count), otherwise it'd be a problem dealing with the correct internal picture name assigned by Excel.

Regards!
 
Just a follow up on this and to demonstrate my lack of VBA knowledge. The macro contains the variables Table1 and CommentCell as the table and comment to place it in. How would I change the macro to loop through other tables/comments without having to hardcode in Table1, Table2 etc and Comment1, Comment2...? Ihave seen lots examples for ranges but all table examples seem to just use "Table1". Thanks
 
Hi, Flopski!

You can define two constants, e.g., ksTable and ksComment, assign them these values:
Const ksTable = "Tabla1"
Const ksComment = "CommentCell"
and change the references of:
[Tabla1[...]] and [CommentCell]
by:
Range(ksTable) and Range(ksComment)
so as to handle the references regarding those constant defined values; then you could change its definitions as required.

Does it help? Otherwise, after Christmas I'll come back.

Regards!
 
Hi, Flopski!

You can define two constants, e.g., ksTable and ksComment, assign them these values:
Const ksTable = "Tabla1"
Const ksComment = "CommentCell"
and change the references of:
[Tabla1[...]] and [CommentCell]
by:
Range(ksTable) and Range(ksComment)
so as to handle the references regarding those constant defined values; then you could change its definitions as required.

Does it help? Otherwise, after Christmas I'll come back.

Regards!



Just got time to re-look at this and try again. The macro worked well with Table1 but still struggling to understand how to loop through so that I can run down a number of tables and comments. At the moment I have three tables which I want to copy into three comment boxes.
 
I tried to create two more macros based on the original and updated them for Table2 and Table 3 but the macros produce an error when they try and paste the table.
 
Hi, Flopski!

You should do something like this:

a) Change ksTable & ksComment definitions to:
"X, Table 1, Table 2, Table 3"
"X, CommentCell1, CommentCell2, CommentCell3"

b) Add the declaration for the array holders:
Dim vTable as Variant, vCommentCell as Variant

c) Load the arrays from the constants:
vTable = Split (ksTable, ",")
vCommentCell = Split (ksCommentCell, ",")

d) Embed the main stuff within a For...Next loop over the arrays:
Dim I as Integer
For I=1 to UBound (vTable)
<here goes the code that does the job>
Next I

Hope it helps. If not, then consider uploading a sample file (including manual examples of desired output if applicable), it'd be very useful for those who read this and might be able to help you. Thank you.

Regards!
 
Hi

I attach a sample copy of what I am trying to achieve. The CheatingUserPictureRestriction macro works well and I can now create a comment in a cell consisting of a picture of a table in the sheet. I have modified the code to create three different comment cells but have failed to work out how to loop this rather than create separate macros.

The final spreadsheet has a changing number of companies and hence would need to use a dynamic looping system. One other issue is the tables created seem to come with random numbers when created and I then rename them Table1, Table2 etc. Is there a way of ensuring this system is kept when I add new companies i.e. if I add Company 4 and it creates the table "Table38" can I rename it to Table4 within a macro.

Many thanks for your help with this.
 

Attachments

  • Sample V3.xlsm
    73.2 KB · Views: 3
Hi, Flopski!

Give a look at the uploaded file. It does the same stuff as the original one but loops for each structured table in 2nd worksheet, with a few assumptions:
a) In 2nd worksheet all tables have a cell with the company name in the same column of the table and 2 rows before.
b) This company name should match with that of 1st worksheet Table0 at column Name.
c) All structured tables in 2nd worksheet are company tables.

This is the updated code:
Code:
Option Explicit

Sub MultiCheatingUserpictureRestrictions()
    ' constants
    Const ksFileExtension = ".jpg"
    Const ksWS1 = "Summary Sheet"
    Const ksWS2 = "Table Data"
    ' declarations
    Dim cht As Chart, tbl As ListObject, rngC As Range
    Dim sFile As String, lWidth As Long, lHeight As Long
    Dim sCompany As String
    Dim I As Integer, J As Integer, A As String
    ' start
    sFile = ActiveWorkbook.Path & Application.PathSeparator & _
        Format(Now(), "yyyymmddhhmmss") & ksFileExtension
    ' process
    For I = 1 To Worksheets(ksWS2).ListObjects.Count
        ' assing table
        Set tbl = Worksheets(ksWS2).ListObjects(I)
        sCompany = tbl.Range.Offset(-2, 0).Cells(1, 1).Value
        '  build image file
        With tbl.Range
            lWidth = .Width
            lHeight = .Height
            .CopyPicture Appearance:=xlScreen, Format:=xlBitmap
            Set cht = ActiveSheet.ChartObjects.Add(.Top, .Left, lWidth, lHeight).Chart
        End With
        With cht
            .Paste
            .ChartArea.Border.LineStyle = 0
            .Export sFile
            .Parent.Delete
        End With
        Application.CutCopyMode = False
        '  identify company
        Set tbl = Worksheets(ksWS1).ListObjects(1)
        With tbl
            For J = 1 To .DataBodyRange.Rows.Count
                If .DataBodyRange.Cells(J, .ListColumns("Name").Index).Value = sCompany Then Exit For
            Next J
        End With
        If J <= tbl.DataBodyRange.Rows.Count Then
            ' assign comment cell
            Set rngC = tbl.DataBodyRange.Cells(J, tbl.ListColumns("Backend").Index)
            '  load image file
            With rngC
                If Not (.Comment Is Nothing) Then .ClearComments
                .AddComment
                With .Comment
                    With .Shape
                        .Fill.UserPicture sFile
                        .Width = lWidth
                        .Height = lHeight
                    End With
                    .Visible = False
                End With
            End With
            '  destroy image file
            Kill sFile
        End If
    Next I
    ' end
    Set rngC = Nothing
    Set tbl = Nothing
    Set cht = Nothing
    Beep
End Sub

Just advise if any issue.

Regards!
 

Attachments

  • Copying a table into a comment_ - Sample V3 (for Flopski at chandoo.org).xlsm
    91.1 KB · Views: 3
Back
Top