数组被错误地加载到图表中

时间:2018-10-09 15:34:44

标签: arrays excel vba excel-vba

我有7种不同的用户窗体,它们都遵循相同的逻辑。我在Excel中的工作表上有一组原始数据。每个用户窗体都有一个单独的“控制器”子例程,该子例程指导过程达到最终结果,即在用户窗体中显示图表。当用户窗体初始化时,它将调用所需的“ controller”子控件。然后,“ controller”子控件调用另一个子例程,该子例程访问我的excel工作表中的原始数据集,其中确定了数据范围。确定数据范围后,我将在下一个构建数组的子区域中使用此信息。我构建数组,然后返回到控制器子目录。一旦构建好数组,我便调用我的子程序,然后构建我的图表,并尝试将这些数组传递给所需的图表,然后根据需要导出它们,以使用户窗体显示它们。请参见下面的代码以更好地理解:

if ($result = $mysqli->query("SELECT technischcontact FROM klanten WHERE technischcontact !=''; "))
{
     while ($row = mysqli_fetch_array($result)) 
     {
         $bcc = $row['technischcontact'];
         $bcc .= '; ';
      }
}

现在,解决问题。在将数组添加到图表之前,我使用了Sub mainUserForm(theVar As Integer) Dim myVar As Integer myVar = theVar 'first get the date range Call getDataRange(False, 0, 0, myVar) 'next the arrays If myVar = 1 Or myVar = 2 Then Call mainArrays(False) Else Call mainArrays(True) End If 'next build required charts Call buildChartsMain(myVar) End Sub 行,但是,图表上显示的数据是完全不正确的(它考虑了我的整个数据集而不是范围)。同样,当我打印数组的值时,它们是正确的,因此构建过程似乎并没有错,但是不知何故,某些东西没有按预期的方式工作。我已将所有必需的变量声明为Debug.Print Join(mainArray1)供全局使用。我将在下面插入我的代码块(不确定在此分析中需要什么,因为在图表部分之前,一切似乎都能正常运行,我认为这里会优先考虑。请参见下面的代码:

Public

打印时数组中的值是:

Sub buildChartsMain(myVar As Integer) Application.ScreenUpdating = False Dim mainChart1 As Shape, mainChart2 As Shape Dim myChartTitle As String Dim fileName1 As String, fileName2 As String myChartTitle = "My Chart Title" Set mainChart1 = wsRawData.Shapes.AddChart2(Left:=0, Top:=0, Width:=288, Height:=222) Set mainChart2 = wsRawData.Shapes.AddChart2(Left:=288, Top:=0, Width:=288, Height:=222) With mainChart1.Chart If myVar = 1 Or myVar = 2 Then Debug.Print Join(mainArray1) .SeriesCollection.NewSeries .SeriesCollection(1).Values = mainArray1() .SeriesCollection(1).XValues = mainDateArray() Else .SeriesCollection.NewSeries .SeriesCollection(1).Values = mainArray3() .SeriesCollection(1).XValues = Array("J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D") End If .ChartType = xlColumnClustered .ChartStyle = 209 .HasTitle = True .chartTitle.Text = myChartTitle .ChartArea.Font.Color = vbWhite .HasLegend = False .Axes(xlCategory).MajorGridlines.Delete End With With mainChart2.Chart If myVar = 1 Or myVar = 2 Then Debug.Print Join(mainArray2) .SeriesCollection.NewSeries .SeriesCollection(1).Values = mainArray2() Else .SeriesCollection.NewSeries .SeriesCollection(1).Values = mainArray4() End If .ChartType = xlBarClustered .ChartStyle = 209 .HasTitle = True .chartTitle.Text = myChartTitle .ChartArea.Font.Color = vbWhite .HasLegend = False .Axes(xlCategory).MajorGridlines.Delete .Axes(xlCategory).ReversePlotOrder = True .Axes(xlValue).TickLabelPosition = xlHigh End With fileName1 = "G:\...\TempExcelCharts\" & Format(Date, "ddmmyyyy") & "MGi.jpg" fileName2 = "G:\...\TempExcelCharts\" & Format(Date, "ddmmyyyy") & "MTi.jpg" mainChart1.Chart.Export fileName:=fileName1, filterName:="JPG" mainChart2.Chart.Export fileName:=fileName2, filterName:="JPG" Call deleteAllCharts Application.ScreenUpdating = True End Sub

但是图表最终看起来像这样,显然这是不正确的:

enter image description here

如果任何人都可以想到我的逻辑问题和/或我使用的方法问题,请提供帮助!我无法为自己的生活设法弄清为什么会有差异!

预先感谢 院长

1 个答案:

答案 0 :(得分:0)

如果您删除括号并把它转过来怎么办

low = -100
high = 100 # these two are assumed values

x = [2, 3, 7, 1, 6]
y = [3, 5, 2, 7, 3]
k = 4

def p(val):
    sum_zi = 0  # sum of possible zi
    for idx in range(len(x)):
        if x[idx]*y[idx] > val:
            diff = x[idx]*y[idx] - val
            zi = (diff + y[idx] - 1) // y[idx]
            sum_zi += zi
        else:
            diff = x[idx]*y[idx] - val
            zi = diff // y[idx]
            sum_zi += zi
    return sum_zi <= k

while low < high:
    mid = (low + high)//2
    if p(mid):
        high = mid
    else:
        low = mid+1

print("Min possible max value", low)
# output = 10

进入此

nlog(range of bounds)