Consulting

Results 1 to 15 of 15

Thread: Copy and paste styles of diagram

  1. #1
    VBAX Regular
    Joined
    Dec 2021
    Posts
    58
    Location

    Copy and paste styles of diagram

    Hello,

    I have many diagrams with about 15 data sets and I need to copy the styles of lines from one diagram, and copy the styles to the rest of the diagrams (after been selected). This is to do on very old Excel. I have 30 sheets - every contains 3 diagrams like that. So too hard to do it manially. I need to set the color of the like, color of the mark and the style of the mark (disabled/enabled). Can you please help with some VBA script to do?
    Attached Images Attached Images

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,049
    Location
    Once again.... attach a workbook with sample diagrams. Attachments (images) are nearly worthless to us.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    VBAX Regular
    Joined
    Dec 2021
    Posts
    58
    Location

    attachment

    I added data to separate file, 3 lists of data and one list of diagrams test.xls (diagram #1 source link corrected 9:16 AM)
    Last edited by vangog; 10-02-2022 at 12:17 AM.

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,638
    But you didn't indicate what should be changed into what.
    Last edited by snb; 10-02-2022 at 03:22 AM.

  5. #5
    VBAX Regular
    Joined
    Dec 2021
    Posts
    58
    Location
    There is problem that every column uses different color for curves. I need to copy the first diagram styles and paste them onto the selected diagrams. Possibly using VBA script. I would do that with the rest of the diagrams. I want to have all lines in same colors, same settings as in the first diagram in 'G30'. There is a lot of columns in that tables and setting every diagram colors would be very very hard.
    Last edited by vangog; 10-02-2022 at 03:58 AM.

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,638
    Fisrt you should remove dataseries 'days' form the first diagram.

    Then run:

    Sub M_snb()
       For Each it In Sheet4.ChartObjects
         For j = 1 To it.Chart.SeriesCollection.Count
            it.Chart.SeriesCollection(j).Format.Line.ForeColor.ObjectThemeColor = j
            it.Chart.SeriesCollection(j).MarkerStyle = j \ 5
            it.Chart.SeriesCollection(j).MarkerSize = 3
            it.Chart.SeriesCollection(j).MarkerForegroundColor = j \ 5
         Next
       Next
    End Sub

  7. #7
    VBAX Regular
    Joined
    Dec 2021
    Posts
    58
    Location
    Quote Originally Posted by snb View Post
    Fisrt you should remove dataseries 'days' form the first diagram.

    Then run:
    Thank you.

  8. #8
    VBAX Regular
    Joined
    Dec 2021
    Posts
    58
    Location
    How to modify the code to print the values from these properties to some sheet? I would write a macro but I need to get the values from the properties first. I mean something like:
    For Each it In Worksheets("G7").ChartObjects
         For j = 1 To it.Chart.SeriesCollection.Count
            Worksheets("t").Cells(i, j) = it.Chart.SeriesCollection(j).Format.Line.ForeColor.ObjectThemeColor
            Worksheets("t").Cells(i, j + 1) = it.Chart.SeriesCollection(j).MarkerStyle
            Worksheets("t").Cells(i, j + 2) = it.Chart.SeriesCollection(j).MarkerSize
            Worksheets("t").Cells(i, j + 3) = it.Chart.SeriesCollection(j).MarkerForegroundColor
         Next
       Next

  9. #9
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,186
    Location
    Perhaps something along the lines of:
    Sub test()    
        Dim it As ChartObject, j As Integer, i As Integer, r As Integer, wsT As Worksheet
        
        Set wsT = Sheets("t")
        
        For Each it In Worksheets("G7").ChartObjects
            For j = 1 To it.Chart.SeriesCollection.Count
                r = r + 1
                With it.Chart.SeriesCollection(j)
                    wsT.Cells(r, 1) = .Format.Line.ForeColor
                    wsT.Cells(r, 2) = .MarkerStyle
                    wsT.Cells(r, 3) = .MarkerSize
                    wsT.Cells(r, 4) = .MarkerForegroundColor
                End With
            Next
        Next
    End Sub
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2403, Build 17425.20146

  10. #10
    VBAX Regular
    Joined
    Dec 2021
    Posts
    58
    Location
    Quote Originally Posted by georgiboy View Post
    Perhaps something ...
    Thank you. I have modified your code to work on ActiveChart, but I got error on the line
    wsT.Cells(r, 1) = .Format.Line.ForeColor:

    Sub extract_chart_styles()
        Dim it As ChartObject, j As Integer, i As Integer, r As Integer, wsT As Worksheet
        
        Set wsT = Sheets("t")
        If TypeName(Selection) <> "ChartArea" Then
           MsgBox "Please select ChartArea, not" + TypeName(Selection)
        Exit Sub
        End If
        
        For j = 1 To ActiveChart.SeriesCollection.Count
            r = r + 1
            With ActiveChart.SeriesCollection(j)
                wsT.Cells(r, 1) = .Format.Line.ForeColor
                wsT.Cells(r, 2) = .MarkerStyle
                wsT.Cells(r, 3) = .MarkerSize
                wsT.Cells(r, 4) = .MarkerForegroundColor
            End With
        Next
    End Sub
    It looks like in old Excel the member Format does not exist. However there is a Fill member. So this works when I replace Format.Line to Fill.
    extract_chart_styles()
        Dim it As ChartObject, j As Integer, i As Integer, r As Integer, wsT As Worksheet
        
        Set wsT = Sheets("t")
        If TypeName(Selection) <> "ChartArea" Then
           MsgBox "Please select ChartArea, not" + TypeName(Selection)
        Exit Sub
        End If
        
        For j = 1 To ActiveChart.SeriesCollection.Count
            r = r + 1
            With ActiveChart.SeriesCollection(j)
                wsT.Cells(r, 1) = .Fill.ForeColor
                wsT.Cells(r, 2) = .MarkerStyle
                wsT.Cells(r, 3) = .MarkerSize
                wsT.Cells(r, 4) = .MarkerForegroundColor
            End With
        Next
    End Sub
    Last edited by vangog; 10-09-2022 at 03:02 AM.

  11. #11
    VBAX Regular
    Joined
    Dec 2021
    Posts
    58
    Location
    So far I have two procedures. One to extract styles from selected Chart. And the next one to set the styles... That one takes the table in sheets("t") as source. But I have problem setting the styles with an error. Can you please help to finish this?

    Sub extract_styles_of_specific_chart()
        Dim it As ChartObject, j As Integer, i As Integer, r As Integer, wsT As Worksheet
        
        Set wsT = Sheets("t")
        If TypeName(Selection) <> "ChartArea" Then
           MsgBox "Please select ChartArea, not" + TypeName(Selection)
        Exit Sub
        End If
        
        For j = 1 To ActiveChart.SeriesCollection.Count
            r = r + 1
            With ActiveChart.SeriesCollection(j)
                wsT.Cells(r, 1) = .Fill.ForeColor
                wsT.Cells(r, 2) = .MarkerStyle
                wsT.Cells(r, 3) = .MarkerSize
                wsT.Cells(r, 4) = .MarkerForegroundColor
            End With
        Next
    End Sub
    Try to set the styles on charts of some active Sheet of Charts:
    Sub set_chart_styles_from_table()
    
        Dim wsS As Worksheet, it As ChartObject, table As Range, c As Integer, l As Integer, ChartSheetName As String
        ChartSheetName = ActiveSheet.Name
        Set wsS = Sheets("t") ' Here is source table with Chart styles values
        Sheets("t").Activate
        Set table = wsS.Range("A1", Range("D11"))
        Sheets(ChartSheetName).Activate
        For l = 1 To table.Rows.Count
           For Each it In ActiveSheet.ChartObjects
             it.Chart.SeriesCollection(l).Fill.ForeColor = table.Rows(l).Columns.Cells(1).Value
             it.Chart.SeriesCollection(l).MarkerStyle = table.Rows(l).Columns.Cells(2).Value
             it.Chart.SeriesCollection(l).MarkerSize = table.Rows(l).Columns.Cells(3).Value
             it.Chart.SeriesCollection(l).MarkerForegroundColor = table.Rows(l).Columns.Cells(4).Value
           Next it
        Next
    End Sub
    The error here:
    error 450: Wrong number of arguments or invalid property assignment ...

    The value of table.Rows(l).Columns.Cells(1).Value
    checked in a watch and it is Correct
    Example values from the first row:
    10077403 -4142 5 58

  12. #12
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,186
    Location
    See attached
    Attached Files Attached Files
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2403, Build 17425.20146

  13. #13
    VBAX Regular
    Joined
    Dec 2021
    Posts
    58
    Location
    Thank you but can you please save the file as .xls? Or just paste the VBA code. I cannot open your file.

    Edit:
    I managed to open it in LibreOffice. Now studing the code.
    Last edited by vangog; 10-13-2022 at 04:18 AM.

  14. #14
    VBAX Regular
    Joined
    Dec 2021
    Posts
    58
    Location
    Currently I am having this problem:
    it.Chart.SeriesCollection(l).Fill.ForeColor.RGB = RGB(R, G, B)
    Generates error wrong number of arguments or invalid property assigment.

  15. #15
    VBAX Regular
    Joined
    Dec 2021
    Posts
    58
    Location
    Quote Originally Posted by georgiboy View Post
    See attached
    Is it possible that the fill property would be read only? For any reason? In the old excel there is no property format or line, so I wonder how to set color on Windows XP. Also I have noticed .MarkerForegroundColor is most times 58 : I copy the rows here:
    58
    58
    58
    58
    58
    57
    10
    58
    58
    58
    58
    57
    57
    46
    And it means that it sets to black color. Which is pretty odd.

Posting Permissions

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