对于下一个:下标超出范围

该代码是用户表单,用户有三个选项,一个小于7万平方英尺,一个在7万到15万平方英尺之间,一个大于15万平方英尺。 根据用户select的代码,然后按照上述设置的要求计算所有build筑物,并将其打印到Excel电子表格中。

我得到下标超出范围的错误。

Option Explicit Private Sub CommandButton1_Click() 'OK button Dim Oshawa_Square_Feet_R As Range Dim Oshawa_Electricity_R As Range Dim Oshawa_Natural_Gas_R As Range Dim Oshawa_Size As Integer Workbooks("Energy Consumption of Different Buildings").Activate Worksheets("DurhamRegionSchools").Activate Set Oshawa_Square_Feet_R = Workbooks("Energy Consumption of Different Buildings").Sheets("DurhamRegionSchools").Range("Oshawa_Square_Feet") Set Oshawa_Electricity_R = Workbooks("Energy Consumption of Different Buildings").Sheets("DurhamRegionSchools").Range("Oshawa_Electricity") Set Oshawa_Natural_Gas_R = Workbooks("Energy Consumption of Different Buildings").Sheets("DurhamRegionSchools").Range("Oshawa_Natural_Gas") Oshawa_Size = Workbooks("Energy Consumption of Different Buildings").Sheets("DurhamRegionSchools").Range("Oshawa_Square_Feet").Count 'Oshawa_Size = Oshawa_Square_Feet.Count Dim Net_Durham_SquareFeet As Double 'For square feet of schools in durham that are less than 70,000 square feet, there was supposed to be more than one city. Dim Net_Durham_NaturalGas As Double 'Natural Gas of the schools that are less than 70,000 sqaure feet. Dim Net_Durham_Electricity As Double 'Electricity of the schools that are less than 70,000 sqaure feet. Dim NNet_Durham_SquareFeet As Double 'For square feet of schools in durham that are greater than 70,000 but less than 150,000 square feet square feet, there was supposed to be more than one city. Dim NNet_Durham_NaturalGas As Double 'Natural Gas of the schools that are greater than 70,000 and less than 150,000 sqaure feet. Dim NNet_Durham_Electricity As Double 'Electricity of the schools that are greater than 70,000 and less than 150,000 sqaure feet. Dim NNNet_Durham_SquareFeet As Double 'For square feet of schools in durham that are greater than 150,000 square feet square feet, there was supposed to be more than one city. Dim NNNet_Durham_NaturalGas As Double 'Natural Gas of the schools that are greater than 150,000 sqaure feet. Dim NNNet_Durham_Electricity As Double 'Electricity of the schools that are greater than 150,000 sqaure feet. Dim c_Oshawa As Double 'Square feet less than 70,000 Dim cc_Oshawa As Double 'Square feet between 70,000 and 150,000 square feet Dim ccc_Oshawa As Double 'Square feet greater than 150,000 square feet Dim E_Oshawa As Double 'Electricity for schools less than 70,000 Dim EE_Oshawa As Double 'Between 70,000 and 150,000 Dim EEE_Oshawa As Double 'Greater than 150,000 Dim G_Oshawa As Double 'Natural Gas for schools less than 70,000 Dim GG_Oshawa As Double 'Natural Gas between 70,000 and 150,000 Dim GGG_Oshawa As Double 'Greater than 150,000 Dim i_O As Long 'Dim Oshawa_Y As Double ???? Dim Oshawa_Cell As Range Dim c_FinalDisplay As Double Dim E_FinalDisplay As Double Dim G_FinalDIsplay As Double For i_O = 1 To i_O = Oshawa_Size For Each Oshawa_Cell In Oshawa_Square_Feet_R Next Oshawa_Cell If (Oshawa_Cell < 70000) Then 'Should I use .Value ????? c_Oshawa = c_Oshawa + Oshawa_Cell E_Oshawa = E_Oshawa + Oshawa_Electricity_R.Cells(i_O).Value G_Oshawa = G_Oshawa + Oshawa_Natural_Gas_R.Cells(i_O).Value End If If (Oshawa_Cell >= 70000 And Oshawa_Cell < 150000) Then cc_Oshawa = c_Oshawa + Oshawa_Cell EE_Oshawa = E_Oshawa + Oshawa_Electricity_R.Cells(i_O).Value GG_Oshawa = G_Oshawa + Oshawa_Natural_Gas_R.Cells(i_O).Value End If If (Oshawa_Cell >= 150000) Then ccc_Oshawa = c_Oshawa + Oshawa_Cell EEE_Oshawa = E_Oshawa + Oshawa_Electricity_R.Cells(i_O).Value GGG_Oshawa = G_Oshawa + Oshawa_Natural_Gas_R.Cells(i_O).Value End If Next i_O Net_Durham_SquareFeet = c_Oshawa Net_Durham_NaturalGas = E_Oshawa Net_Durham_Electricity = G_Oshawa NNet_Durham_SquareFeet = cc_Oshawa NNet_Durham_NaturalGas = EE_Oshawa NNet_Durham_Electricity = GG_Oshawa NNNet_Durham_SquareFeet = ccc_Oshawa NNNet_Durham_NaturalGas = GGG_Oshawa NNNet_Durham_Electricity = EEE_Oshawa If CheckBox1.Value = True Then c_FinalDisplay = c_FinalDisplay + Net_Durham_SquareFeet E_FinalDisplay = E_FinalDisplay + Net_Durham_Electricity G_FinalDIsplay = G_FinalDIsplay + Net_Durham_NaturalGas Else c_FinalDisplay = c_FinalDisplay + 0 E_FinalDisplay = E_FinalDisplay + 0 G_FinalDIsplay = G_FinalDIsplay + 0 End If If CheckBox2.Value = True Then c_FinalDisplay = c_FinalDisplay + NNet_Durham_SquareFeet E_FinalDisplay = E_FinalDisplay + NNet_Durham_Electricity G_FinalDIsplay = G_FinalDIsplay + NNet_Durham_NaturalGas Else c_FinalDisplay = c_FinalDisplay + 0 E_FinalDisplay = E_FinalDisplay + 0 G_FinalDIsplay = G_FinalDIsplay + 0 End If If CheckBox3.Value = True Then c_FinalDisplay = c_FinalDisplay + NNNet_Durham_SquareFeet E_FinalDisplay = E_FinalDisplay + NNNet_Durham_Electricity G_FinalDIsplay = G_FinalDIsplay + NNNet_Durham_NaturalGas Else c_FinalDisplay = c_FinalDisplay + 0 E_FinalDisplay = E_FinalDisplay + 0 G_FinalDIsplay = G_FinalDIsplay + 0 End If Worksheets("UserForm").Select Range("B5").Value = squarefeet_FinalDisplay Range("B6").Value = Electricity_FinalDisplay Range("B7").Value = Gas_FinalDIsplay MsgBox "The results are in cells B5 to B7" End Sub 

下面给出了电子表格“DurhamRegionSchools”和“不同build筑物能源工作簿”中的范围。

 Oshawa Sqaure Feet 43,577.15 30,159.92 31,424.13 50,822.88 25,926.48 41,146.66 43,348.42 59,230.57 29,318.73 166,639.23 143,257.95 39,719.34 55,486.34 12,672.88 57,654.72 29,578.68 37,022.98 22,144.05 22,556.85 32,656.61 57,445.91 17,160.89 173,126.74 54,228.58 163,490.87 133,326.62 57,832.30 64,650.00 130,685.16 39,977.16 56,147.78 45,572.20 37,147.33 33,475.22 Oshawa Electricity 161,460.36 157,343.73 201,128.33 276,713.33 119,088.39 197,258.76 205,980.35 317,256.42 90,166.00 819,712.50 750,168.54 130,499.56 226,040.00 70,139.04 293,712.00 84,400.00 159,026.90 52,608.27 61,126.54 190,676.47 273,366.15 207,657.14 960,699.01 387,919.99 702,110.56 664,230.45 318,636.00 347,876.47 592,539.06 77,760.00 350,664.71 211,457.12 156,720.22 188,041.28 Oshawa Natural Gas 43,688.09 26,201.90 29,260.79 16,485.41 33,441.72 33,064.60 42,997.86 36,572.87 36,019.56 140,409.78 129,017.17 41,155.25 35,377.65 18,023.74 34,075.25 35,228.25 21,203.76 29,995.63 24,783.51 23,770.24 34,763.66 0.00 53,257.44 14,041.96 137,116.06 121,820.85 24,100.18 24,561.11 178,694.83 38,687.54 17,719.73 26,249.28 41,250.24 36,172.77 

For Each Oshawa_Cell In Oshawa_Square_Feet_R : Next Oshawa_Cell的循环For Each Oshawa_Cell In Oshawa_Square_Feet_R : Next Oshawa_Cell不会执行任何操作,因为For Each ...Next ...部分之间没有任何语句。 工作循环看起来像这样:

 For Each Oshawa_Cell In Oshawa_Square_Feet_R If (Oshawa_Cell < 70000) ' do lots of stuff End If Next Oshawa_Cell 

正如所写, For i_O = 1 To i_O = Oshawa_Size Next i_O循环在错误的地方。 除了在语义上不正确 – 它应该说For i_O = 1 To Oshawa_Size ,所有这个循环会做的是运行内部For Each...循环多less倍,因为有在Oshawa_Square_Feet范围内的单元格。 内部For Each循环已经为该范围内的每个单元运行一次。

因此,如果Oshawa_Square_Feet范围包含20个单元格,则两个循环的效果将总共运行400次,您为各种消耗累计的数字将会过高20倍。

任何types的循环都可以工作 – For ... Next用循环计数器或For Each ... Next在一个范围内 – 但是在这里使用这两个都没有意义。 您显然希望使用适当的偏移量来引用Oshawa_Electricity_ROshawa_Natural_Gas_R范围,因此如果这些值全部在同一列中,则正常的For ... Next可能更易于使用。

我假设你的第二和第三If陈述也是不正确的,并将cc_Oshawa = c_Oshawa + Oshawa_Cell.Valuecc_Oshawa = cc_Oshawa + Oshawa_Cell.Value等您总计错误的variables:

 For i_O = 1 To Oshawa_Size Set Oshawa_Cell = Oshawa_Square_Feet_R.Cells(i_o, 1) If (Oshawa_Cell.Value < 70000) Then c_Oshawa = c_Oshawa + Oshawa_Cell.Value E_Oshawa = E_Oshawa + Oshawa_Electricity_R.Cells(i_O, 1).Value G_Oshawa = G_Oshawa + Oshawa_Natural_Gas_R.Cells(i_O, 1).Value End If If (Oshawa_Cell.Value >= 70000 And Oshawa_Cell.Value < 150000) Then cc_Oshawa = cc_Oshawa + Oshawa_Cell.Value EE_Oshawa = EE_Oshawa + Oshawa_Electricity_R.Cells(i_O, 1).Value GG_Oshawa = GG_Oshawa + Oshawa_Natural_Gas_R.Cells(i_O, 1).Value End If If (Oshawa_Cell.Value >= 150000) Then ccc_Oshawa = ccc_Oshawa + Oshawa_Cell.Value EEE_Oshawa = EEE_Oshawa + Oshawa_Electricity_R.Cells(i_O, 1).Value GGG_Oshawa = GGG_Oshawa + Oshawa_Natural_Gas_R.Cells(i_O, 1).Value End If Next i_O 

如果每个范围内的值不是全部在一个列中,那么这有点复杂但不是不可能的