Vba 对于下一步:下标超出范围

Vba 对于下一步:下标超出范围,vba,excel,Vba,Excel,该代码用于userform,用户有三个选项,一个小于70000平方英尺,一个在70000到150000平方英尺之间,一个大于150000平方英尺。根据用户选择的内容,代码应该对上述要求范围内的所有建筑物进行计数,并将其打印到Excel电子表格中 我得到了下标超出范围的错误 Option Explicit Private Sub CommandButton1_Click() 'OK button Dim Oshawa_Square_Feet_R As Range Dim Osha

该代码用于userform,用户有三个选项,一个小于70000平方英尺,一个在70000到150000平方英尺之间,一个大于150000平方英尺。根据用户选择的内容,代码应该对上述要求范围内的所有建筑物进行计数,并将其打印到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

Oshawa\u平方英尺中每个Oshawa\u单元的循环
:下一个Oshawa\u单元
没有任何作用,因为每个…的
下一个…
部分之间没有任何语句。工作循环看起来像:

For Each Oshawa_Cell In Oshawa_Square_Feet_R
    If (Oshawa_Cell < 70000)

' do lots of stuff

    End If
Next Oshawa_Cell

如果每个范围中的值不都在一列中,则会有点复杂,但并非不可能

哪一行导致错误?(如果这是您的文字代码,您在“DIM EEE_Oshawa”行上有一个输入错误,因为缺少星号。)您需要告诉我们哪一行产生了错误。工作表(“UserForm”)。请在代码末尾附近选择。在我的代码中是否还有编译器可能找不到的其他错误?我是vba新手,我本应该使用matlab的,但由于用户友好,我选择了这个,谢谢,我会改正的。它应该被注释掉。是的,这是我的文字代码,本来应该还有六个城市,但它们是重复的,所以我不想包含它。Tim Williams,我已经在上面评论了导致该错误的行。非常感谢大家的帮助!!我是否需要使用数组而不是范围才能在for循环中使用excel电子表格中的一系列值?我的一个同事说我会,或者我的方式会好吗?嘿,barrowc,所以我编辑了我的代码,正如你上面所说的,但现在我得到运行时错误91:object variable或with block variable not set。它突出显示了Oshawa_Cell=Oshawa_Square_Feet_R.Cells(i_O,1)抱歉:分配对象时需要使用
Set
,因此
Set Oshawa_Cell=Oshawa_Square_Feet_R.Cells(i_O,1)
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 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