-1

As the Title suggest, I need to find the way to set the maximum and minimum value for the X and Y axes of a chart created through a vb.net code. This is the part of the code it should be inserted in:

 With ws

            .Shapes.AddChart(Excel.XlChartType.xlXYScatterSmoothNoMarkers, 300, 20, 400, 300).Select()

            With xlApp.ActiveChart

                .SeriesCollection(1).Name = "Force vs Displacement"
                .SeriesCollection(1).XValues = "='Sheet" & index & "'!$B$6:$B$14046"
                .SeriesCollection(1).Values = "='Sheet" & index & "'!$C$6:$C$14046"
                .HasLegend = False

                [...]

Thanks for any suggestion made.

maze11
  • 11
  • 1
  • Record a macro in excel while performing the actions you want to automate - that will give you the code you need. – Tim Williams Jul 16 '18 at 17:41
  • It should be fine if vba and vb.net were the same, but, and I can be wrong, they are not. I know the command is maximumscale/minimumscale or something, but I need the correct syntax in vb.net. – maze11 Jul 17 '18 at 12:53
  • It's not *that* different though - recorded VBA is useful even if you're working in C#, and even more so in VB.NET – Tim Williams Jul 17 '18 at 14:57
  • I know it's not that different, but it's different. I don't need the "almost correct", I need the "correct". VS doesn't give any hints when it's about excel commands. As I said in a previous comment, I know the command is maximumscal/minimumscale, at least it is for vba, but I don't know the correct order of commands to get it done. – maze11 Jul 18 '18 at 13:29
  • I may have made an improvement, but now I have both axes scaled, I need just one, the Y axis. I wrote: For Each axis In xlApp.ActiveChart.Axes axis.TickLabels.NumberFormat = "0" axis.MaximumScale = 1 axis.MinimumScale = 0 Next – maze11 Jul 18 '18 at 14:05
  • I did it!! Sorry to waste your time. Thanks! – maze11 Jul 18 '18 at 14:18

1 Answers1

0

Sorry everybody. I finally found the correct page to look at, now almost everything is clear. The code for me is:

xlApp.ActiveChart.Axes(2, 1).maximumscale = 1.2
xlApp.ActiveChart.Axes(2, 1).minimumscale = -0.2

where in brackets are XlAxisType and XlAxisGroup. 2 is for xlValue, whereas the 1 is for xlPrimary. Here you can find all I needed to not post my question:

https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.axes?view=excel-pia

Sorry once more. See ya!

maze11
  • 11
  • 1
  • If you're programming Excel from .Net, you should still keep Excel's VB Editor open. Record a macro, and refer to the VBE's Object Browser. You should have easily found `.Axes(XlAxisType, XlAxisGroup.MinimumScale' this way. The only difference between what I would have used in VBA vs .Net is the `xlApp` prefix, and in fact, with the prefix, it's exactly what I would have to use if I automate Excel from PowerPoint or Word in VBA (or automate Excel from VB6). – Jon Peltier Jul 23 '18 at 14:06
  • And that comment is not a criticism. It's a suggestion for using the tools already at your disposal. Using Microsoft's online documentation is also good, if you can find the right page and don't mind the lack of useful examples. – Jon Peltier Jul 23 '18 at 14:07
  • Thanks for the valuable suggestion. – maze11 Jul 25 '18 at 09:20