該代碼是用戶窗體,用戶有三個選項,一個小於70,000平方英尺,一個在70,000到150,000平方英尺之間,一個大於150,000平方英尺。根據用戶選擇的代碼,然後按照上述設置的要求計算所有建築物,並將其打印到Excel電子表格中。For Next:下標超出範圍
我得到下標超出範圍的錯誤。
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」中「Energy Of Different Buildings workbook」的範圍。
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
什麼行導致錯誤? (如果這是您的文字代碼,由於缺少星號,您在「DIM EEE_Oshawa」行上有錯字。) – DougM
您需要告訴我們哪條線路會產生錯誤。 –
工作表(「UserForm」)。選擇代碼末尾附近。編譯器可能無法找到的編碼中還有其他錯誤嗎?即時通訊新vba,我本來應該使用matlab,但由於用戶友好選擇這個,而不是 –