-1

I have a difficult task. I am not sure if anyone out there can help me.

Please read my concerns:

#1 I have "2" workbooks name: Unemployment_Rate GDP_Annual_Growth_Rate_% #2 in "Unemployment_Rate" workbook. I have "1" Master worksheet and "181" worksheet which is name after a country. Example Sheet #1 "Afghanistan"; Sheet #2 "Albania"; Sheet #3 "Algeria" and so on...

#3 In "GDP_Annual_Growth_Rate_%" workbook. I have "1" Navigation worksheet, "1" Master worksheet and "185 worksheet which is name after a country. Example Sheet #1 "Afghanistan"; Sheet #2 "Albania"; Sheet #3 "Algeria" and so on...

My problem here is this:

a) How can I create a chart using VBA to automatically get the data in each country worksheet from "Unemployment_Rate" and "GDP_Annual_Growth_Rate_%" workbook? b) How can I get data from column "B" (which is the date column) and column "E" (which is the value column) in all country worksheet in Unemployment_Rate" workbook? And c) How can I get data from column "B"(which is the date column) and column "E" (which is the value column) in all country worksheet in "GDP_Annual_Growth_Rate_%" workbook? See example photo of how the chart is displayed:

Unemployment-and-GDP

My Other Problem:

How can I paste each chart into each country worksheet in "Unemployment_Rate" workbook automatically using a VBA?

With lack of knowledge for excel. I don't know if this request can be accomplish. To me it sounds difficult. I am opened for a better outcome, once available.

Below you can view other supported images and excel files that relates with this project.

How can you all help me?

Thanks in advance

https://drive.google.com/file/d/1mmsL54lWBSVNsrP0INleJe-uW8U-2VVD/view https://drive.google.com/file/d/1hbxg2eJEq0E6hBwMputV2lvOzeuNPkYu/view

Example#1 Example#2 Example#3

braX
  • 11,506
  • 5
  • 20
  • 33
Matrix
  • 9
  • 3

1 Answers1

0

Here's my take on this. Find something in common on all the sheets, loop through each sheet, perform a common/standardized operation on each sheet.

Sub Macro1()

Dim rs As Worksheet

For Each rs In ThisWorkbook.Worksheets
rs.Select
   
   If rs.Name <> "Unemployment" Then
        Columns("B:D").Select
        Range("B5").Activate
        ActiveSheet.Shapes.AddChart2(227, xlLine).Select
        ActiveChart.SetSourceData Source:=Range(rs.Name & "!$B:$D")
        ActiveChart.ChartType = xlColumnClustered
        ActiveChart.FullSeriesCollection(1).ChartType = xlColumnClustered
        ActiveChart.FullSeriesCollection(1).AxisGroup = 1
        ActiveChart.FullSeriesCollection(2).ChartType = xlLine
        ActiveChart.FullSeriesCollection(2).AxisGroup = 1
        ActiveChart.FullSeriesCollection(1).AxisGroup = 2
        ActiveChart.FullSeriesCollection(1).ChartType = xlLine
   End If

Next rs

End Sub

Notice, I am skipping the first sheet, named 'Unemployment' because this sheet is different than all the others. Also, the code between the If...End If is simply a recorded macro. If it doesn't do exactly what you want, record a macro to do what you want, and copy/paste that code between the If...End If.

Let me know if you have any questions.

Based on your most recent question, here are my findings.

#1) In the workbook named 'GDP_Annual_Growth_Rate', you actually have a space after the tab named 'GDP Annual %' so the actual name is 'GDP Annual % '. I don't think you want that!

#2) You had to close your Sub with 'End Sub'. You ALWAYS need an 'End Sub' at the end.

#3) As you start to loop through the sheets, you start to slect charts and make modifications to those charts, like this: 'ActiveSheet.ChartObjects("Chart 1").Activate'. But, what if you don't have a chart on one sheet, you can't select a chart if it doesn't exist.

#4) Finally, rs sound like a records set in MS Access; that's my take on it. Why not declare a Worksheet as ws: 'Dim ws As Worksheet'

Here's some working code to get you started.

Sub DeleteallCharts()
Dim chtObj As ChartObject
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.Select
    For Each chtObj In ActiveSheet.ChartObjects
    chtObj.Delete
    Next
Next ws
End Sub


Sub BuildAllCharts()

Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets
ws.Select
   
    If ws.Name <> "GDP Annual %" And ws.Name <> "Navigation" Then

        Range("A:A,E:E").Select
        Range("" & ws.Name & "[[#Headers],[12 Mth Rolling Avg.]]").Activate
        ActiveSheet.Shapes.AddChart2(227, xlColumnClustered).Select
        ActiveChart.SetSourceData Source:=Range("" & ws.Name & "!$A:$A," & ws.Name & "!$E:$E")
        ActiveChart.FullSeriesCollection(1).Select
        With Selection.Format.Fill
            .Visible = msoTrue
            .ForeColor.RGB = RGB(255, 0, 0)
            .Transparency = 0
            .Solid
        End With
        Range("" & ws.Name & "[[#Headers],[Country]]").Select

    End If

Next ws

End Sub

Notice: the first Macro deletes all charts and the second Macro builds all charts. The only thing that may be a bit confusing is the '& ws.Name &' but you need this as you loop through all sheets, so you get dynamic references to each relative data set on each sheet. Record a Macro yourself, and you will see hot it is linked to one specific sheet.

Finally...if you want to create a Secondary Axis, follow the steps below.

Select a chart to open Chart Tools.

Select Design > Change Chart Type.

Select Combo > Cluster Column - Line on Secondary Axis.

Select Secondary Axis for the data series you want to show.

Select the drop-down arrow and choose Line.

Select OK.

I just created a sample for you, below. You exact requirement may be slightly different, but this should illustrate the point.

enter image description here

Here is a link for some really, really, really cool charting ideas.

https://peltiertech.com/Excel/Charts/ChartIndex.html

ASH
  • 20,759
  • 19
  • 87
  • 200
  • ASH. It an honor. I really appreciate your response. The link below is a video that show the challenges that I am going through with this project. Please view the link. Any feed back would be greatly appreciated. Thanks in advance.https://drive.google.com/file/d/1MEc3BwvuVOFqErZxBTBa-lOpm8sMJMJF/view?usp=sharing – Matrix Aug 02 '20 at 01:04
  • I don't have access to that link. – ASH Aug 02 '20 at 01:10
  • I am sorry for the late reply. the link was restricted. What about this one? See if it workout for you... Thanks again: https://drive.google.com/file/d/1MEc3BwvuVOFqErZxBTBa-lOpm8sMJMJF/view?usp=sharing – Matrix Aug 02 '20 at 10:59
  • ASH. Good Morning. How Service Yesterday. Hope it was wonderful. However, I am speaking base on what your profile have outline about God. On m end it was wonderful...I am not sure if you got the opportunity to view or see the video at your end. Please let me know what you suggest I do to fix the problem. Thanks in advance. – Matrix Aug 03 '20 at 13:44
  • I only have access to 'Unemployment_Rate.xlsm'; I can't open the other file. For the other workbook, you can turn on the Macro Recorder, click through the steps you need to do, to build 1 chart, then turn off the Macro Recorder, and copy/paste the code you have between the If...End If... in the code sample I posted. Try it and see if that works for you. Or, revise your original question with a couple before/after images of what you are trying to do. What do you have before the code runs? What do you want to have after the code runs? – ASH Aug 03 '20 at 18:12
  • Thanks for your response ASH. would try your recommendation would get back to you. Here is the unrestricted link: https://drive.google.com/file/d/1hbxg2eJEq0E6hBwMputV2lvOzeuNPkYu/view?usp=sharing – Matrix Aug 03 '20 at 19:08
  • I am new in excel. I am not sure if I miss a point. I am trying to get data from "GDP_Annual_Rate_%" workbook and data from "Unemployment_Rate" workbook. Both data are link and paste in Each Worksheet in "Unemployment" workbook. Together, as one chart. Please forgive me ASH. I have not given up. I have receive an error while using "Record Macro". I am not sure if I have place the code in the right place or if I have done wrong in the beginning. Please see link: Thanks for your patience. https://drive.google.com/file/d/1DKfSbKtezfMBIy10GLxbswhosjhUHAME/view?usp=sharing – Matrix Aug 03 '20 at 20:23
  • Looks like you were very close, btu there are 2 issues. #1) Around 2:30 you forgot to COPY the 'End With' #2) Around 2:45 you need to KEEP the 'If...End If' Fist those two things and you should be good to go. Remember, when testing your code, put your cursos inside the script and hit the F8 button over and over and over, to step through the code one line at a time. When it is fully debugged, hit F5 to run the code. – ASH Aug 03 '20 at 22:04
  • ASH. I have a link that display the code I modify with your direction. It can be modify by you also. Please let me know what I have left out. https://codeshare.io/5MLrry Thanks in advance – Matrix Aug 03 '20 at 22:47
  • ASH. Thank You. I made the correction on "GDP Annual %" and also made some adjustments on "("A:A,E:E")" and ("" & ws.Name & "!$A:$A," & ws.Name & "!$E:$E"). To "("A:A,B:B,E:E")" and "("" & ws.Name & "!$A:$A," & ws.Name & "!$B:$B," & ws.Name & "!$E:$E")" I wanted to add the date column. – Matrix Aug 04 '20 at 17:47
  • Both worksheet works wonderfully and individually. I appreciate your patience and support for your continual participation. I have one concern. I am not sure if it can be done. How can I use both codes to get a complement chart. Please view an example photo. It is not the best example. but How can I get the two chart on the above photo to become one? "Similar to the third chart below". What is your recommendation? https://imgur.com/w7G0VDk – Matrix Aug 04 '20 at 18:39
  • It's called a Secondary Axis. I just made one more update to my original post, to show how to create a Secondary Axis. – ASH Aug 04 '20 at 19:00
  • ASH. I am sorry. It probably my dialect. Your explanation is straight to the point and thank you for the correct name of my explanation "Secondary Axis"and the excel link you have provide. My concern here is this: (Note: This Is One Worksheet Example). How can I Get the "Primary Axis" from "Afghanistan worksheet" in "GDP_Annual_Growth_Rate Workbook" and the "Secondary Axis" from "Afghanistan worksheet" in "Unemployment_Rate Workbook". In other Words: Afghanistan gets both data from "GDP_Annual_Growth_Rate Workbook" and "Unemployment_Rate Workbook" into one chart. I'm not sure if this can work. – Matrix Aug 04 '20 at 20:53
  • Just consolidate the data into one workbook, and then build your charts. It's going to be tough to maintain all of this, the way you have it now. – ASH Aug 05 '20 at 01:04