+ Reply to Thread
Results 1 to 18 of 18

empty chart after macro

Hybrid View

  1. #1
    Registered User
    Join Date
    04-11-2016
    Location
    Belgium
    MS-Off Ver
    2013
    Posts
    52

    empty chart after macro

    Hello

    In my workbook I retrieve data from an external file.
    On the sheet "Controlepaneel" I have a drop down list with sales reps that triggers a series of macros to get the filtered data for the chosen sales rep.
    After a series of macros I get a chart on the "Grafiek" sheet showing the sales results for the past months.
    Everything works fine for Chris, Els and Yves, but for "René" the chart doesn't show.
    As you will notice, the number of rows is different for each of the sales reps, the number of columns is the same (it will only grow with the months to come).

    I checked the different macros to get to the chart and all work OK.
    I also tried to copy a good working macro and change the data concerning René, but I still don't get a chart.

    I checked the empty graph and there are no source data selected.
    For the other reps I have the following source data:
    CC: =Grafiek!$A$1:$E$98
    EV: =Grafiek!$A$1:$E$162
    YG: =Grafiek!$A$1:$E$121
    When I check for RH, the source data are =Grafiek!$A$1:$E$252. After entering this manually, I get a graph, but rows and columns are reversed compared to the graphs of the other reps.
    Switching rows and columns is not possible because Excel limits the number of data rows to 255 (that's what a message box is telling me when I try to do this).

    Anyone any idea why the data source is not there?
    Attached Files Attached Files

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,926

    Re: empty chart after macro

    A lot of this code looks familiar. It looks like you used one of my old programs as a template .

    You are doing things the hard way by replicating your code for each person. You can pass parameters to subroutines. I don’t know if this will solve your problem but it will make coding and debugging it easier.

    I suggest that you modify the change event on Controlpannel to read:
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("B9")) Is Nothing Then
        Select Case Range("B9")
            Case "Chris": Bijwerken "CC"
            Case "Els": Bijwerken "EV"
            Case "René": Bijwerken "RH"
            Case "Yves": Bijwerken "YG"
        End Select
    End If
    End Sub
    Then make a “Generic” Biweken subroutine
    Sub Bijwerken(Initials As String)
    
        DataKopieren Initials
        KopieerWaarden_Grafiek Initials
        
    End Sub
    And likewise a generic DatKopieren
    Sub DataKopieren(Initials as String)
    Dim MyPath As String
    Dim MyFile As String
    Dim xlThis As Workbook
    Dim xlData As Workbook
    Dim shThis As Worksheet
    Dim shData As Worksheet
    Dim LRow As Long, LCol As Long
    
        Set xlThis = ThisWorkbook
        Set shThis = xlThis.Sheets("Data")
        MyPath = Range("Def_Dir")
        MyFile = Range("Data_File")
        
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        
        ' Clear the old data
        ClearTable "Data", "Table_Data"
        
        ' Open data workbook
        Set xlData = Workbooks.Open(MyPath & "\" & MyFile)
        
        ' Set the sheet and get the last row and column
        Set shData = xlData.Sheets(1)
        LRow = shData.Range("A" & Rows.Count).End(xlUp).row
        LCol = shData.Cells("1", Columns.Count).End(xlToLeft).Column
        
        ' Set the filter
        shData.Range(shData.Cells(1, 1), shData.Cells(LRow, LCol)).AutoFilter Field:=3, Criteria1:= Initials
        
        shData.Range(shData.Cells(1, 1), shData.Cells(LRow, LCol)).SpecialCells(xlCellTypeVisible).Copy
            shThis.Range("A1").PasteSpecial xlPasteValues
            
        ' Close the workbook
        Workbooks(MyFile).Close savechanges:=False
        
        Sheets("Controlepaneel").Select
        Application.DisplayAlerts = True
        Application.ScreenUpdating = True
        
        Range("A1").Select
        
        SubtotaalMaken
    
    End Sub
    Next a generic KopieerWaarden_Grafiek
    Sub KopieerWaarden_Grafiek(Initials As String)
        
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        
        Worksheets("Grafiek").Activate
        Cells.Select
        Selection.Delete Shift:=xlUp
        Range("A1").Select
        
        ThisWorkbook.Worksheets("Subtotaal").Cells.Copy
        ThisWorkbook.Worksheets("Grafiek").Select
        Selection.PasteSpecial Paste:=xlPasteValues, _
            Operation:=xlNone, SkipBlanks:=True, Transpose:=False
        Selection.PasteSpecial Paste:=xlPasteFormats
        
        ActiveSheet.Outline.ShowLevels rowlevels:=1
        
        Laatste3KolommenVerwijderen
        
        Application.DisplayAlerts = True
        Application.ScreenUpdating = True
        
        Range("A1").Select
        
        GrafiekMaken Initials
        
        MsgBox "Klaar!" & vbCr & vbCr & "De gegevens werden verwerkt.", vbInformation, "Verkoopsresultaten"
        
        
    End Sub
    And finally a generic GrafiekMaken – note that I deleted the title. I’ll address that later. In this case, you don’t need to pass the parameter since the only thing you need it for is to set the title and we will do that elsewhere.
    Sub GrafiekMaken()
    
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        
        With Range("A1")
            .Parent.ListObjects.Add(xlSrcRange, Range(.End(xlDown), .End(xlToRight)), , xlYes).Name = "Tabel1"
        End With
    
        Range("a1").Select
        Selection.CurrentRegion.Select
        MyRange = Selection.Address
        mysheetname = ActiveSheet.Name
        ActiveSheet.ChartObjects.Add(100, 60, 500, 250).Select
        Application.CutCopyMode = False
        ActiveChart.ChartWizard _
           Source:=Sheets(mysheetname).Range(MyRange), _
           Gallery:=xlColumnClustered, Format:=10, PlotBy:=xlRows, _
           CategoryLabels:=1, SeriesLabels:=1, HasLegend:=0, _
           CategoryTitle:="", _
           ValueTitle:="", ExtraTitle:=""
        Application.DisplayAlerts = True
        Application.ScreenUpdating = True
        Range("a1").Select
        
    End Sub
    To set the title, go to the Controlepanneel and set a cell (for example B11) ="Omzet 2016 - " & B9. Then go to the chart and click in the title. Then go to the formula bar, type = and then select this cell.

    There is probably a better way of doing this using a “template” chart and named dynamic ranges rather than building the chart with VBA.

    If you decide to try that, here are a couple of articles that will help.
    http://www.utteraccess.com/wiki/inde...Dynamic_Ranges
    http://www.utteraccess.com/wiki/inde...namic_Charting
    Last edited by dflak; 05-13-2016 at 09:42 AM. Reason: missing code tag
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  3. #3
    Registered User
    Join Date
    04-11-2016
    Location
    Belgium
    MS-Off Ver
    2013
    Posts
    52

    Re: empty chart after macro

    Hello dflak

    This is indeed your code. I first posted this thread in the vba/macro section, but since I have this problem with one of my charts, I moved it to this section.
    Thanks a lot for simplifying the code. It is indeed a lot less complex than what I had been doing.
    Nevertheless I still don't get a chart for René and I also noticed that not all the data for Els are in the data table. I did a MS Query in a blank workbook and I get more rows than in this workbook.

    Any idea how to solve this? I've been trying copying codes and so one, but no result.

  4. #4
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,926

    Re: empty chart after macro

    I decided to take your suggestion and use MS-Query. The issue is to keep it dynamic. My apologies for introducing English (or more appropriately American) into your spreadsheet. I don’t speak Dutch … yet, but I am learning by working with you and your fellow countrymen .

    The SQL has to change every month to get the new months. So I address this issue on the Sales SQL page.

    I have a little table in Columns D:F where, based on the date entered in the Controlepanneel, I make a string to get the months that I expect to find in the data.

    Cell I2 contains the fragment of SQL code that will change every month.

    Columns A & B on the Sales SQL page are my “standard” way of dealing with dynamic SQL Code. I use whatever formulas I need to create the SQL code. Normally I am substituting parameters, but in this case, I am adding a dynamic list. Cell B1 concatenates the translated code into a SQL String.

    The module SubSQL goes along with this. What this module does is it replaces the Command string for the query with what is in Cell B1 (which I named SQL_Sales_Data).

    You can modify the FROM clause to reflect the directory where your data spreadsheet exists. In fact you can probably use a formula referencing Cell B6 on the Controlepaneel.

    I also tacked on a subroutine called MakeFunction.

    So when you click on the Refresh Data button. It creates the SQL, substitutes it into the connection, refreshes the data and then runs the MakeFunction subroutine.

    The MakeFunction subroutine reads the months returned by the data query and creates the headers and the formulas on the Sales Chart page.

    On the Sales Chart page, I made dynamic named ranges for Sales_Month (on row 1) and Sales_Data (on row 2). I “attached” these to the chart. The title for the chart comes from the Controlepannel Cell B11.

    Using the dynamic names eliminated the need to use VBA to manage the charts. See the two references I provided in the previous post to see how to make named dynamic ranges and how to use them in charts.

    One other tweak I added was a lookup table on the parameters tab. This table looks up the initials and the person’s full name. This keeps you out of the code when personnel change; all you have to do is change the table. I also made the data validation on the Controlepaneel work off this table.

    I think you should be able to throw out a lot of your code.

    If you have any questions, just ask.

    One thing I did notice as I was playing with the spreadsheet. When I changed a name in Cell B9 on the Controlepaneel, it fired off your code. I got an error message because the code was looking for data in a directory I don’t have. I wasn’t testing that part of the code so I exited the debugger.

    Except for Rene. When I changed to Rene from another name, the trigger probably worked, but I did not get an error message. Something is not being called. So, there is some sort of disconnect and that may be the root of your problem.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    04-11-2016
    Location
    Belgium
    MS-Off Ver
    2013
    Posts
    52

    Re: empty chart after macro

    Hi dflak

    Thanks for all the efforts.

    I changed the path on the Sales SQL sheet (cell A8) to FROM `F:\Documenten\Excel\Omzet klanten VM 2016 tem 16 04 cc.xlsx`.`blad1$` b. But when I hit the Refresh button on the Controlepaneel sheet I get an error.
    The SQL is still referencing to the C:\Users\dlfak\Temp folder. When I look at the VBA 'ActiveWorkbook.Connections(ConnectionName).Refresh' in the SubSQL routine is highlighted.
    At first I tought maybe the Dutch Excel version did not know the 'FROM', but this is not the case.

    You also proposed to use a formula referencing to cell B6 on the Controlepaneel sheet. How do you do this without losing the extra info like the from, sheetname, ...

  6. #6
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,926

    Re: empty chart after macro

    Oh yeah. The connection string. I forgot about that. Let me work on that one. It's a relatively simple fix but I'll want to test it.

  7. #7
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,926

    Re: empty chart after macro

    I think I have it now. It reads both Def_dir and Data_File and sets thing up in both the connection string (via vba) and the command string (via a formula on the SQL page). To reset the command string, first you break it into pieces using the split command and then you change the pieces you want, and then you put it back together.
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    04-11-2016
    Location
    Belgium
    MS-Off Ver
    2013
    Posts
    52

    Re: empty chart after macro

    Sorry, but now I get this error: "Error 1004: there are too few parameters. Expected number is: 4."

  9. #9
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,926

    Re: empty chart after macro

    Well, there are 4 months, I suspect that they are the culprits. I could not get that error. Please attach both the program and the source data again. I want to see exactly what you've filled in and what is contained in the data. I think I will have to do an ASCII analysis on the strings. The issue is that the dates have a slash in them and have to be handled with a back single quote.

  10. #10
    Registered User
    Join Date
    04-11-2016
    Location
    Belgium
    MS-Off Ver
    2013
    Posts
    52

    Re: empty chart after macro

    Hello dflak

    two files attached.
    Attached Files Attached Files

  11. #11
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,926

    Re: empty chart after macro

    I opened up the file you just sent and checked the connection string. It is still pointing to my temp directory. You can check the strings by going to Data -> Connections, select the string name and click on properties. Click on the definition tag and the two strings are shown there. You can copy and paste them to notepad or Word for further analysis.

    I tried refreshing data with the strings you provided and it failed as expected. I got a message saying that the path was not valid.

    If you were to try it with my temp directory path still in there, you should be getting a message saying that the path is invalid. You should not be seeing the "too few parameters" error.

    I switched the string back to my temp directory and it ran ok. I cannot duplicate the error.

    It is possible that when the subroutine fails, it does not switch the strings. This might explain why my strings were still in the connection string and command sting as shown on the Data ribbon. Even if that is the case, you are getting the wrong error message for the conditions.

    What could be happening, but I can't make it happen on my end, is that the Sales SQL isn't doing the translations properly because they are not recalculating. The part of the code that changes the connection string uses the named ranges. However the part of the code that changes the command string uses the Sales SQL page.

    So with your latest version, please take a look at the Sales SQL page. First of all, cell B1 should have the connection string in it (don't worry that you can't see all of it). Then look at cell A7 - this should have the correct months. Then look at cell A8 - this should have the correct path and file name to where the database is.

    If Cell B1 has the #Value error in it, select it and click in the formula bar and press ENTER. This should clear the error. If either of the other two cells is incorrect, press F9.

    Let me know if any of this works. It should be all automatic, but I might be able to put some checks in the code nonetheless.

  12. #12
    Registered User
    Join Date
    04-11-2016
    Location
    Belgium
    MS-Off Ver
    2013
    Posts
    52

    Re: empty chart after macro

    Hi dflak

    I checked the following:
    - cell B1 has the connection string with my path OK
    - cell A7 has 4 months
    - cell A8 has the correct path
    - the two strings in the definition tab had your path in them. I did get a warning that the path was not correct when I closed the Data-Connection pop-up without changing the path.
    After changing the path there was no longer a warning.

    I ran the refresh data on the Controlepaneel again and got the same message:

    img01.JPG

    Translation (more or less): Error 1004 during execution. [Microsoft][ODBC Excel driver] there are insufficient parameters. The expected number is:4.

    Degugging shows:

    img02.JPG


    Thinking the number of months has something tot do with the 4 parameters error, I tried with the March file who has only 3 months.
    Then cell B1 has the correct FROM... path but shows 4 months to select.
    Then cell A7 still has 4 months.
    Then cell A8 has the correct path to the "Omzet klanten VM 2016 tem 16 03 CC.xlsx" file.
    Checking the Data-Connection-Definitions: the path to "Omzet...03..." is in there. When closing the windows, I aslo get an error message about the 4 parameters.

  13. #13
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,926

    Re: empty chart after macro

    When you went back to three months, did you change the date on the control panel? The date controls the months that go into the query. This is managed by the table in columns D:F on the Sales SQL and cells I1 & I2.

    I'm fairly certain the connection string is working. It's the command string that would produce that error. Let's try one more thing: after you get the error can you go to Data -> Connections and get the connection string and command string and post them. I probably isn't the command string, but you might as well throw it in while you are there.

    Also copy and paste the translated SQL.

  14. #14
    Registered User
    Join Date
    04-11-2016
    Location
    Belgium
    MS-Off Ver
    2013
    Posts
    52

    Re: empty chart after macro

    The date was changed on the control panel.

    This is the connection string:
    DSN=Excel Files;DBQ=f:\Documenten\Excel\Omzet klanten VM 2016 tem 16 03 CC.xlsx;DefaultDir=f:\Documenten\Excel;DriverId=1046;MaxBufferSize=2048;PageTimeout=5;

    This is the command string:
    SELECT b.Kltnr, b.Naam, b.`Vert#`, b.Land, b.`jan/yy`, b.`feb/yy`, b.`mrt/yy`, b.`apr/yy` FROM `f:\Documenten\Excel\Omzet klanten VM 2016 tem 16 03 CC.xlsx`.`blad1$` b

    And this is the translated SQL:

    img03.JPG

    where
    B1 = =SuperCat(Table_SQL_Sales[Translated SQL])
    B3, B4, B5, B6; B7 and B8 = =SPATIES.WISSEN([@[Raw SQL]]) -->Translation: =TRIM([@[Raw SQL]])

    Regards
    Chris

  15. #15
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,926

    Re: empty chart after macro

    I see the problem: b.`jan/yy`, b.`feb/yy`, b.`mrt/yy`, b.`apr/yy` should be b.`jan/16`, b.`feb/16`, b.`mrt/16`, b.`apr/16`

    On the Sales SQL Page. Check columns D:F and cells I1 & I2 to see why this isn't happening.

    The formula in cell F2 should read =IF(MONTH(MyDate)>'Sales SQL'!D2,"b." & CHAR(96) & E2 & "/" & TEXT(MyDate,"yy") & CHAR(96) & ",","")

    Check to see what MyDate is defined as. It should point to Cell B1 on the Control Panel.

  16. #16
    Registered User
    Join Date
    04-11-2016
    Location
    Belgium
    MS-Off Ver
    2013
    Posts
    52

    Re: empty chart after macro

    I found the problem .

    Cell F2 reads =IF(MONTH(MyDate)>'Sales SQL'!D2,"b." & CHAR(96) & E2 & "/" & TEXT(MyDate,"yy") & CHAR(96) & ",","").
    But since 'year' in Dutch is 'jaar', I replaced the "yy" with "jj" and the error is gone.
    Everything works like a charm now .

    dflak, thanks a lot for your help and patience. You're the best!!!

  17. #17
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,926

    Re: empty chart after macro

    Ah, got to love the language barrier! I've learned something! It's been a pleasure. I've enjoyed working with you and good luck with the rest of the project.

  18. #18
    Registered User
    Join Date
    04-11-2016
    Location
    Belgium
    MS-Off Ver
    2013
    Posts
    52

    Re: empty chart after macro

    I found another little bug, but got it solved .

    The name string 'MyDate' was referring to cell B1 on the control panel. But B1 is today's date.
    Cell F2 and down on the Sales SQL sheet have the following formula: =IF(Month(MyDate)>'Sales SQL'!D2;"b."&Char(96)&E2&"/"&Text(MyDate;"jj")&Char(96)&",";""). But since these cells also take today's date in account, even when selecting March as the data source file, April was still in the list. This created again a parameter error (number of parameters missing depending on the number of data source months less that 4).

    So I changed the reference date to cell D4 on the control panel and changed the formula in F2 and down to: =IF(Month(MyDate)>='Sales SQL'!D2;"b."&Char(96)&E2&"/"&Text(MyDate;"jj")&Char(96)&",";"").

    Now there are no more parameter errors if I change the data source file.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Empty cells not in chart
    By NLatuny in forum Excel Charting & Pivots
    Replies: 3
    Last Post: 04-07-2016, 06:33 AM
  2. Replies: 0
    Last Post: 07-14-2015, 05:10 PM
  3. MACRO to empty a range of cells based on whether specified cell is empty
    By TBJV in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-01-2013, 10:35 PM
  4. Don't want chart to display empty cells
    By Dakota27 in forum Excel Charting & Pivots
    Replies: 8
    Last Post: 12-09-2009, 04:32 AM
  5. Removing empty categories from Pie chart
    By Pasha81 in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 11-18-2009, 06:50 AM
  6. Empty Pivot Chart
    By Tony in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-06-2005, 01:06 PM
  7. Pie chart ignore empty cells
    By cpopham in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 04-06-2005, 09:40 AM

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