在VBA for循环中设置变量范围

时间:2016-09-20 16:20:15

标签: vba variables range

我有一个充满0和1值的电子表格表。我需要比较列对和计算两列中值为1的行数。错误的错误指向我将列设置为范围[Set myRange1 = Simul.Range(Cells(1,i),Cells(2,i))] 并且错误是:“对象表的方法范围失败” 我想知道如何修改上面的行以使其工作。 在此先感谢您的有用指南。完整的代码是:

Sub V2()
    Dim myCount As Integer
    Dim myRange2, myRange1 As Range
    Dim Simul As Worksheet 
    myCount = 0
    Set Simul = Sheets("Simul")
    With Simul
        For i = 1 To 5
            Set myRange1 = Simul.Range(Cells(1, i), Cells(2, i))
            Set myRange2 = Simul.Range(Cells(1, i+6), Cells(2, i+6))
        Next i
    End With
    myCount = Application.WorksheetFunction.CountIfs(myRange1, 1, myRange2, 1)
    Worksheets("Simul").Cells(20, 20).Value = myCount
End Sub 

0 个答案:

没有答案