代码适用于用户形式,用户有三个选项,一个小于70,000平方英尺,一个介于70,000和150,000平方英尺之间,一个大于150,000平方英尺。根据用户选择的代码,然后根据上述设置要求计算所有建筑物,并将其打印到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”的范围。
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
答案 0 :(得分:1)
循环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...
循环,与Oshawa_Square_Feet
范围内的单元格一样多次。对于范围内的每个单元格,内部For Each
循环已经运行一次。
因此,如果Oshawa_Square_Feet
范围包含20个单元格,则两个循环的效果将总共运行400次,并且对于各种消耗总计的数字将过高20倍。
任何一种类型的循环都可以工作 - For ... Next
在一个范围内有一个循环计数器或For Each ... Next
- 但是在这里使用两者都没有意义。您显然希望使用适当的偏移量引用Oshawa_Electricity_R
和Oshawa_Natural_Gas_R
范围,以便正常的For ... Next
可能更容易使用,如果值都在同一列中。
我认为您的第2和第3 If
次陈述也不正确,并将cc_Oshawa = c_Oshawa + Oshawa_Cell.Value
更改为cc_Oshawa = cc_Oshawa + Oshawa_Cell.Value
等。您总计错误的变量:
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
如果每个范围内的值都不在一列中,那么它有点复杂但并非不可能