Dynamic excel chart doesn't display all of the data - excel

I have this code that allows users to enter chart parameters into some cells and dynamically create a chart. Many series (up to four) are allowed on two vertical (y) axis and one shared horizontal (x) axis. The chart is a mixture of columns and lines normally, and the data ranges are of varying length. I have this code that adds the series like so (I'll try to stick to what I believe is the relevant code)
seriesCount = 1
If hasSeries1 = True Then
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(seriesCount).Name = .Cells(2, 6) & " " & axisside1
ActiveChart.SeriesCollection(seriesCount).ChartType = chartType1
ActiveChart.SeriesCollection(seriesCount).AxisGroup = axisgroup1
ActiveChart.SeriesCollection(seriesCount).Border.LineStyle = borderStyle1
ActiveChart.SeriesCollection(seriesCount).Border.Color = lineColor1
ActiveChart.SeriesCollection(seriesCount).Format.Line.Weight = lineWidth1
ActiveChart.SeriesCollection(seriesCount).Format.Fill.ForeColor.RGB = seriesColor1
ActiveChart.SeriesCollection(seriesCount).Format.Line.Visible = hasLine1
ActiveChart.SeriesCollection(seriesCount).XValues = dates1
ActiveChart.SeriesCollection(seriesCount).Values = dataset1
seriesCount = seriesCount + 1
End If
If hasSeries2 = True Then
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(seriesCount).Name = .Cells(3, 6) & " " & axisside2
ActiveChart.SeriesCollection(seriesCount).ChartType = chartType2
ActiveChart.SeriesCollection(seriesCount).AxisGroup = axisgroup2
ActiveChart.SeriesCollection(seriesCount).Border.LineStyle = borderStyle2
ActiveChart.SeriesCollection(seriesCount).Border.Color = lineColor2
ActiveChart.SeriesCollection(seriesCount).Format.Line.Weight = lineWidth2
ActiveChart.SeriesCollection(seriesCount).Format.Fill.ForeColor.RGB = seriesColor2
ActiveChart.SeriesCollection(seriesCount).Format.Line.Visible = hasLine2
ActiveChart.SeriesCollection(seriesCount).XValues = dates2
ActiveChart.SeriesCollection(seriesCount).Values = dataset2
seriesCount = seriesCount + 1
End If
If hasSeries3 = True Then
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(seriesCount).Name = .Cells(4, 6) & " " & axisside3
ActiveChart.SeriesCollection(seriesCount).ChartType = chartType3
ActiveChart.SeriesCollection(seriesCount).AxisGroup = axisgroup3
ActiveChart.SeriesCollection(seriesCount).Border.LineStyle = borderStyle3
ActiveChart.SeriesCollection(seriesCount).Border.Color = lineColor3
ActiveChart.SeriesCollection(seriesCount).Format.Line.Weight = lineWidth3
ActiveChart.SeriesCollection(seriesCount).Format.Fill.ForeColor.RGB = seriesColor3
ActiveChart.SeriesCollection(seriesCount).Format.Line.Visible = hasLine3
ActiveChart.SeriesCollection(seriesCount).XValues = dates3
ActiveChart.SeriesCollection(seriesCount).Values = dataset3
seriesCount = seriesCount + 1
End If
If hasSeries4 = True Then
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(seriesCount).Name = .Cells(5, 6) & " " & axisside4
ActiveChart.SeriesCollection(seriesCount).ChartType = chartType4
ActiveChart.SeriesCollection(seriesCount).AxisGroup = axisgroup4
ActiveChart.SeriesCollection(seriesCount).Border.LineStyle = borderStyle4
ActiveChart.SeriesCollection(seriesCount).Border.Color = lineColor4
ActiveChart.SeriesCollection(seriesCount).Format.Line.Weight = lineWidth4
ActiveChart.SeriesCollection(seriesCount).Format.Fill.ForeColor.RGB = seriesColor4
ActiveChart.SeriesCollection(seriesCount).Format.Line.Visible = hasLine4
ActiveChart.SeriesCollection(seriesCount).XValues = dates4
ActiveChart.SeriesCollection(seriesCount).Values = dataset4
End If
Here is the problem: the chart only displays part of the data it is supposed to. When I right-click on the data series, hit Select Data and choose Edit, the correct series (both x and y) become highlighted, but what is being shown is a truncated subset of what should be there.
Here is a sample of what I'm seeing
Here is some of the data for the light blue column
12/30/2005 307%
1/31/2006 302%
2/28/2006 248%
3/31/2006 262%
4/28/2006 285%
5/31/2006 256%
... ...
... ...
... ...
6/30/2014 147%
Notice how this data should be showing on the chart beginning at 12/30/2005, but it's starting at 11/30/2013 instead (though the values appear to be correct, 307%, 302%, etc.). It is almost as though excel is forcing the 2nd and 3rd data series to be the same length as the first one. The first one is charting correctly.

I think you're not using a XY chart, thus you must have the same labels (Xvalues = dates for you) for every series. That means that you need to create a unique dates-set containing all the dates and assign it (as Xvalues) to the first serie.

Related

Clear ComboBox Containig an Array VBA and Repopulate with Column Index

I have a sigle column ComboBox that I have populated with a dynamic array; however, I'd like the code to replace the value of the combo box with respective column from my ListBox.
'Prepares the Active Escorts list box.
ivb = 0
i = 0
With frmEntry
.listboxActiveEscorts.Clear
.listboxActiveEscorts.ColumnHeads = False
.listboxActiveEscorts.ColumnCount = "15"
.listboxActiveEscorts.ColumnWidths = "0,100,100,0,0,100,100,0,0,0,0,0,100,100,80"
i = 0
'Badge # combobox properties
ReDim vbArray(0 To vbArrayCount - 1)
For i = 0 To vbArrayCount - 1
ivb = ivb + 1
vbArray(ivb - 1) = loVisBadge.Range.Cells(i + 2, 1).Value
Next i
.cbxVisitorBadgeNumber.List = vbArray
End With
This population of the ComboBox executes beautifully, in my belief, but if you see a better way to implement the dynamic list, I'm all ears.
I have a button that I will use to reset the form, which also works for the most part. When an item is selected from the list, I am able to clear all controls except for the ComboxBox containing the array of values. I will be adding two additional arrays to each of the other ComboBoxes on the form, and I imagine I am going to have the same problem: "Could not set the value property. Invalid property value"
This is the code assigned to click event of the ListBox:
Private Sub listboxActiveEscorts_Click()
With frmEntry
.cbxEscortSelectName.Value = .listboxActiveEscorts.Column(1) + " " + .listboxActiveEscorts.Column(2)
.txtCredential.Value = .listboxActiveEscorts.Column(4)
.txtEscortCompany.Value = .listboxActiveEscorts.Column(3)
.cbxVisitorName.Value = .listboxActiveEscorts.Column(5) + " " + .listboxActiveEscorts.Column(6)
.txtVECompany.Value = .listboxActiveEscorts.Column(7)
.txtVEDOB.Value = .listboxActiveEscorts.Column(8)
.txtVEIdentification.Value = .listboxActiveEscorts.Column(9)
.txtVEIDNumber.Value = .listboxActiveEscorts.Column(10)
.txtVEExpirationDate.Value = .listboxActiveEscorts.Column(11)
.txtVEStart.Value = .listboxActiveEscorts.Column(12)
.txtVEEnd.Value = .listboxActiveEscorts.Column(13)
'.cbxVisitorBadgeNumber.Value = ""
.cbxVisitorBadgeNumber = vbNullString
.cbxVisitorBadgeNumber.Value = .listboxActiveEscorts.Column(14)
End With
End Sub
What am I missing here. I tried to ReDim the array that assigned the values, but that didn't work. Is it a data type thing, perhaps?
In the picture below, you'll see the values populated in the controls, all except the visitor badge # (ComboBox which throws the error...I have commented out the line for illustration purposes, so you'll see the visitor badge # is blank).

How can i add this other condition to this code?

i have this subroutine that i use to make labels. for some reason i cant just make another subroutine where instead of "CGI_SAMPLE_LABEL" it uses "YCI_SAMPLE_LABEL" because of other subroutines. any suggestions on how i can add this so that it chooses either one or the other. if tried using WHERE OR but that didn't work. i edited some of the tags because LIMS basic language is also like smalltalk.
LabelType = "CGI_SAMPLE_LABEL"
pTableNameStr = "SAMPLE"
pLabelNameStr = "CGI_SAMPLE_LABEL"
'Breakpoint(aReason)
NumSamples = Ubound(selectedObjects, 1)
FOR X = 1 TO NumSamples
SampleNumber = selectedObjects[x]
pKeyNameArr[1] = SampleNumber
pNumLabelsInt = 1
pLabelNameStr = "CGI_SAMPLE_LABEL"
pReasonStr = "Auto Label Generation"
pActivityStr = "Label printed for sample logged event"
GOSUB FN_LABEL_PRINT_ALL
NEXT 'Sample
RETURN

Graphs won't overlap eachother

I have X amount of graphs where X is always an even number. Every series shows up as two graphs. One line graph and a Xy Scatter plot. The problem is that the graphs are separated, one to the left, one to the right. I want them to overlap so the points from the Scatter plot shows where the lines break. This is what I want:
But this is what I get:
By the way, these are two different graphs. One shows dates, the other weeks. The one with dates work, the week one doesn't.
This is the code concerning the week graph:
With myChart.Chart
.SeriesCollection(i).XValues = actualdateweek
'actualdateweek is an array with week numbers like 21,22 and so on
.SeriesCollection.NewSeries
If LineGraph = True Then
.SeriesCollection(i).Values = actualdata
.SeriesCollection(i).ChartType = xlLine
.SeriesCollection(i).Format.Line.Visible = msoTrue
.SeriesCollection(i).Format.Line.ForeColor.RGB = RGB(ColorR(i - iModifier - 1), ColorG(i - iModifier - 1), ColorB(i - iModifier - 1))
'actualdata is an array with all the data
elseif XyScatterPlot = True Then
.SeriesCollection(i).Values = actualdata
.SeriesCollection(i).ChartType = xlXYScatter
.SeriesCollection(i).MarkerStyle = xlMarkerStyleSquare
.SeriesCollection(i).Format.Fill.Visible = msoTrue
.SeriesCollection(i).Format.Fill.ForeColor.RGB = RGB(ColorR(i - iModifier - 1), ColorG(i - iModifier - 1), ColorB(i - iModifier - 1))
.SeriesCollection(i).MarkerForegroundColorIndex = -4142
End If
.SeriesCollection(i).AxisGroup = 1
Next i
End With
The graphs are added in turns. First a data series (the actualdata array) is added as a line graph. Then the same data series is added but as a scatter plot. Then the data series changes and it all repeats. So it's like
seriescollection(1).XValues = array filled with week numbers
seriescollection(1).Values = Data series 1
seriescollection(1).ChartType = xlLine
seriescollection(2).XValues = array filled with week numbers
seriescollection(2).Values = Data series 1
seriescollection(2).ChartType = xlXYScatter
seriescollection(3).XValues = array filled with week numbers
seriescollection(3).Values = Data series 2
seriescollection(3).ChartType = xlLine
seriescollection(4).XValues = array filled with week numbers
seriescollection(4).Values = Data series 2
seriescollection(4).ChartType = xlXYScatter
This is what the first graph looks like:
Then when the data to the second graph is added:
.SeriesCollection(i).Values = actualdata
Then the second graph is changed into a scatter plot:
.SeriesCollection(i).ChartType = xlXYScatter
Then an axis group is assigned:
.SeriesCollection(i).AxisGroup = 1
Anyone have an idea what is happening?
edit
Ok problem solved I guess...
By removing the X value for the XYScatterPlot it aligned with the Line Graph.
This line:
.SeriesCollection(i).XValues = actualdateweek

How to export text from annotation fields in Nuance Power PDF to Excel using VBA?

I am trying to export some text from annotation fields in Nuance Power PDF to Excel using VBA. I added the Nuance Power PDF reference to Excel VBA (PDF Plus).
I used it and it works well but the text returned from fields is empty.
Set PDFApp = CreateObject("NuancePDF.App")
Set dvDoc = CreateObject("NuancePDF.DVDoc")
dvDoc.Open("\\adpdc-2\Users$\a.goudinoux\Documents\Macro Formulaire\fiche.pdf")
Set ddDoc = dvDoc.GetDDDoc()
Set ddPage = ddDoc.AcquirePage(0)
nbannots = ddPage.GetNumAnnots() - 1
For i = 0 To nbannots
Texte = ""
Set ddAnnot = ddPage.GetAnnot(i)
Set ddText = ddDoc.CreateTextSelect(0, ddAnnot.GetRect())
ThisWorkbook.Sheets(1).Cells(1, i) = ddAnnot.GetTitle()
For k = 0 To ddText.GetNumText()
Texte = Texte & ddText.GetText(k)
Next
ThisWorkbook.Sheets(1).Cells(2, i) = Texte
Next
Part of the PDF document :
Results :
As you can see the first line is working but not the second one.
I thought the problem was with ddText but ddText.GetNumText() gives the right number of text elements in the text selection (ex : 2, 5, 4, etc...) when I run my program in Debug Mode.
I think the problem is from the function GetText(k).
I made it work once but I can't find my code back..
Do you see any mistake ?
Either your annotations are more complex than indicated or you're overthinking it.
Just read the annotation with GetContents and call it a day.
Set PDFApp = CreateObject("NuancePDF.App")
Set dvDoc = CreateObject("NuancePDF.DVDoc")
dvDoc.Open("\\adpdc-2\Users$\a.goudinoux\Documents\Macro Formulaire\fiche.pdf")
Set ddDoc = dvDoc.GetDDDoc()
Set ddPage = ddDoc.AcquirePage(0)
nbannots = ddPage.GetNumAnnots() - 1
For i = 0 To nbannots
Set ddAnnot = ddPage.GetAnnot(i)
ThisWorkbook.Sheets(1).Cells(1, i) = ddAnnot.GetTitle
ThisWorkbook.Sheets(1).Cells(2, i) = ddAnnot.GetContents
Next i

How to add Calculated Member to Pivot Table via Python win32

I'm trying to add a calculated column into this pivot table that is being generated via a python script with import win32com.client.
Code is posted below that is generating the pivot table. I'm not sure how to add a column. In VBA it would be wb.PivotTable.CalculatedField.Add but that didn't work (at least the syntax I tried).
I'm trying to calculate [OoCUnits] / [GrossUnits]
#Make Pivot version with four weeks data
Excel = win32com.client.gencache.EnsureDispatch('Excel.Application')
win32c = win32com.client.constants
wb = Excel.Workbooks.Open(filename)
ws3 = wb.Worksheets('Dataset')
cl1 = ws3.Cells(1,1)
cl2 = ws3.Cells(max_row,max_col)
PivotSourceRange = ws3.Range(cl1,cl2)
ws3.Activate()
PivotSourceRange.Select()
wb.Sheets.Add (After=wb.Sheets(3))
ws4 = wb.Worksheets(4)
ws4.Name = 'Pivot'
cl3 = ws4.Cells(4,1)
PivotTargetRange = ws4.Range(cl3,cl3)
PivotTableName = 'OoCPivot'
#Make Pivot Table
PivotCache = wb.PivotCaches().Create(SourceType=win32c.xlDatabase, SourceData=PivotSourceRange, Version=win32c.xlPivotTableVersion14)
PivotTable = PivotCache.CreatePivotTable(TableDestination=PivotTargetRange, TableName=PivotTableName, DefaultVersion=win32c.xlPivotTableVersion14)
PivotTable.PivotFields('Product Name').Orientation = win32c.xlRowField
PivotTable.PivotFields('Product Name').Position = 1
PivotTable.PivotFields('Customer Number').Orientation = win32c.xlPageField
PivotTable.PivotFields('Customer Number').Position = 1
PivotTable.PivotFields('Customer Name').Orientation = win32c.xlPageField
PivotTable.PivotFields('Customer Name').Position = 2
PivotTable.PivotFields('Week Ending Date').Orientation = win32c.xlColumnField
PivotTable.PivotFields('Week Ending Date').Position = 1
DataField = PivotTable.AddDataField(PivotTable.PivotFields('GrossUnits'))
DataField.NumberFormat = '#0.00'
DataField = PivotTable.AddDataField(PivotTable.PivotFields('OoCUnits'))
DataField.NumberFormat = '#0.00'
I'm inserting the values with the DataField. The script is not presenting any issues as is.
EDIT: The exact code I implemented to solve. The last two lines are just formatting. I'm including in case it helps someone else.
CalcField = PivotTable.CalculatedFields().Add('OoC Unit %','= OoCUnits / GrossUnits')
DataField = PivotTable.AddDataField(PivotTable.PivotFields('OoC Unit %'))
DataField.NumberFormat = '#%'
PivotTable.DisplayErrorString = True
Actually, the method is PivotTable.CalculatedFields().Add() according to docs. Consider placing below at bottom:
CalcField = PivotTable.CalculatedFields().Add("OC_GrossPct", "= OoCUnits / GrossUnits")
PivotTable.PivotFields("OC_GrossPct").Orientation = win32c.xlDataField

Resources