在excel中build立一个二维数组

我试图build立一个学校项目的数组,情景是这样的:

你有一个30英里(20英里)的城市,每英里有20英里(公里)的道路,必须编写代码,根据业务地点和成本分配货物给每个分配中心的最佳位置。

我有一段代码logging了客户端业务的数量,一个logging业务位置的数组以及一个存储每个客户端购买的产品量的数组。

我坚持的地方是,我认为我应该build立一个0到30,0到20(城市规模)的数组,但是我必须根据用户定义的精度(.25,.5, 1和2英里),所以我应该有数组能够存储120乘以80单元的计算值。

不知道是否有道理,这里是要求:具体来说,您的程序应该能够完成以下任务:

  1. 阅读客户数据和用户input的分析解决scheme。 您的计划必须旨在适应客户数量,地点和产品交付量的变化。 分析分辨率的用户选项将为:0.25英里,0.5英里,1英里和2英里。
  2. validation用户input是数字和有效的。
  3. 分析所有可能的配送中心地点(可能与客户地点configuration)的成本,并确定最佳值。
  4. 显示最佳配送中心位置的X和Y值以及相应的最低每周成本。 可能有多个地点具有相同的最低成本。
  5. 显示与最佳值相邻的所有位置的成本,以显示结果的敏感度。

使用的公式是:

与顾客的距离(Di)=abs|x-xi|+|y+yi|

客户成本(Ci)=1/2*Di*Vi (客户产品数量)* Ft

 Ft = 0.03162*y+0.04213*x+0.4462 cost = sum Ci from 1 to number of clients 

下面是我的代码到目前为止,我开始它基本上build立arrays和显示在第二张,所以我可以可视化,但我不能在最终产品。 在debugging它时,它会到达代码行di =并给我一个超出范围的下标。

 Option Explicit Option Base 1 Sub load() Dim Cust!, x#, y#, volume#(), n!, loc#(), i%, vol#, xi#, ci#, di#, j# Dim choices#, val#(), nptsx!, nptsy!, Ft#, lowx!, lowy!, low!, M! Dim costmatrix#(), npts!, cost!() 'find number of customers Cust = 0 Do While Cells(8 + Cust, 1).Value <> "" Cust = Cust + 1 Loop If Cust < 2 Then MsgBox "number of customers must be greater than 1." Exit Sub End If 'establist array of customer locations ReDim loc#(1, Cust) For j = 1 To Cust x = Cells(7 + j, 2) y = Cells(7 + j, 3) Next j ReDim volume#(Cust, 1) For i = 1 To Cust vol = Cells(7 + i, 4) Next i choices = Cells(3, 7).Value nptsx = 30 / choices + 1 nptsy = 20 / choices + 1 '30x20 grid ReDim costmatrix(x To nptsx, y To nptsy) For x = 0 To nptsx - 1 Sheet3.Cells(1, x + 2) = x * choices Next x For y = 0 To nptsy - 1 Sheet3.Cells(2 + y, 1) = y * choices Next y For x = 0 To nptsx - 1 For y = 0 To nptsy - 1 For k = 1 To Cust di = Abs(x * choices - Sheet1.Cells(7 + j, 2)) + Abs(y * choices - Sheet1.Cells(7 + j, 3)) Ft = 0.03162 * Sheet1.Cells(7 + j, 3) + 0.4213 * Sheet1.Cells(7 + j, 2) + 0.4462 ci = 1 / 2 * di * vol * Ft Sheet3.Cells(x + 2, 2 + y) = ci Next k Next y Next x lowx = x lowy = y Range("I9:J:3").Clear Range("I9") = "optimum" Range("J9") = lowx * choices Range("K9") = lowy * choices Range("L9") = low i = 9 If lowy < npts - 1 Then i = i + 1 Cells(1, "I") = "Increment North" Cells(1, "L") = cost(lowx, lowy + 1) End If If lowy > 0 Then i = i + 1 Cells(1, "I") = "Increment South" Cells(1, "L") = cost(lowx, lowy - 1) End If If lowx < npts - 1 Then i = i + 1 Cells(1, "I") = "Increment East" Cells(1, "L") = cost(lowx, lowy + 1) End If If lowx > 0 Then i = i + 1 Cells(1, "I") = "Increment West" Cells(1, "L") = cost(lowx, lowy - 1) End If End Sub 

更新,我已经build立了数组,但我需要弄清楚如何一次为一个单元格中的所有客户端进行计算,将每个客户端的结果一起添加并将其总和放入单元格中,然后转到下一个单元格

当你通过维度loc#

 ReDim loc#(Cust, 2) 

第一个索引必须在1和Cust之间

你有循环

 For k = 1 To Cust x = Cells(7 + k, 2) y = Cells(7 + k, 3) Next k 

在循环运行之后, k值为Cust + 1 ,而不是Cust因为VBA中的for循环首先增加计数器,然后testing是否超出限制。

直到线路再次使用k

 di = Abs(Sheet3.Cells(1, x + 2) - loc(k, 1)) 

在这个阶段kCust + 1 – 比最高允许的下标多一个,因此下标超出范围错误。

在上下文中,我认为你打算在该行和下一行中使用j而不是k 。 我不知道你的代码是否有其他问题,但在这些行中删除k应该有帮助。