2017-08-10 202 views
0

我是VBA新手,在下面的代码中出现类型不匹配错误。类型不匹配错误

我得到整数j的错误。我已将其更改为所有其他数据类型,但仍相同。

Private Sub CommandButton3_Click() 

    Dim rng1 As Range, rng2 As Range, i As Integer, j As Integer, k As Integer 

    Set rng1 = Range("A:A") 
    Set rng2 = Range("B:B") 

    j = Application.WorksheetFunction.Lookup(2, 1/(rng1 <> ""), rng1) 
    k = Application.WorksheetFunction.Match(j, rng1, 0) 

    i = 0 

    For i = i + 1 To k 
     If Cells(i, 1) Mod 2 = 0 Then 
      Cells(i, 2) = "Even" 
     Else: Cells(i, 2) = "Odd" 
     End If 
    Next i 

    MsgBox "There are " & Application.WorksheetFunction.CountIf(rng2, "Even") _ 
     & " Even and " & Application.WorksheetFunction.CountIf(rng2, "Odd") & " Odd numbers" 

End Sub 
+2

不能阵列比较字符串(即'rng1 <>“”'无效并且会导致类型不匹配)。 – YowE3K

+0

谢谢。这意味着我只能使用最后一行功能。请说明如何在VBA中使用此公式,因为在Excel中可以正常工作,但在VBA中不能正常工作 – Roumya

+0

如果我正确读取代码,则试图找到包含最后一列占用的列上的值的第一行A.这是正确的吗?然后,您正在设法计算出至此为止出现了多少个偶数以及多少个奇数 - 但是您希望从列中排除最后一个值的第一次出现以外的任何内容。是否正确?因此,如果单元格A1:A9包含“1,2,2.7,3,4,5,1,2,3”,则您试图计算1个偶数和3个奇数(即仅计数到A4的计数)。那是对的吗? – YowE3K

回答

0

我的评论:

如果我正确地读你的代码,你试图找到一个包含与A列的最后占用的行的值的第一行是正确的吗?然后,您正在设法计算出至此为止出现了多少个偶数以及多少个奇数 - 但是您希望从列中排除最后一个值的第一次出现以外的任何内容。是否正确? ...

你的答案:

前两个你的问题是完全正确的。

示例数据:

enter image description here


如果你真的想找到一个包含与在A列的最后占用的行的值的第一行,即包含第一行3在上面的示例数据中,并确定该点的赔率/平均数,并从计数中排除超出该点的任何数值,然后您可以使用以下代码:

Private Sub CommandButton3_Click()  
    Dim lastUsedRow As Long 
    Dim lastRow As Long 
    Dim i As Long 
    Dim Odds As Long: Odds = 0 
    Dim Evens As Long: Evens = 0 

    With ActiveSheet 

     'Find the last used row in column A (row 9 in example data) 
     lastUsedRow = .Cells(.Rows.Count, "A").End(xlUp).Row 

     'Find first row containing the data from the last used row 
     '(i.e. the first row containing a 3 in the example data, i.e. row 4) 
     lastRow = Application.Match(.Cells(lastUsedRow, "A").Value, .Range("A:A"), 0) 

     'Loop from first row to last row 
     For i = 1 To lastRow 

      'Decide whether the nearest integer to the value in the cell 
      'is odd or even 
      If .Cells(i, "A").Value Mod 2 = 0 Then 
       .Cells(i, "B").Value = "Even" 
       Evens = Evens + 1 
      Else 
       .Cells(i, "B").Value = "Odd" 
       Odds = Odds + 1 
      End If 

     Next i 

     MsgBox "There are " & Evens & " Even and " & Odds & " Odd numbers" 
    End With 
End Sub 

但是,如果你想要做的你的计算,那么所有行,你可以使用下面的代码:

Private Sub CommandButton3_Click()  
    Dim lastUsedRow As Long 
    Dim i As Long 
    Dim Odds As Long: Odds = 0 
    Dim Evens As Long: Evens = 0 

    With ActiveSheet 

     'Find the last used row in column A (row 9 in example data) 
     lastUsedRow = .Cells(.Rows.Count, "A").End(xlUp).Row 

     'Loop from first row to last used row 
     For i = 1 To lastUsedRow 

      'Decide whether the nearest integer to the value in the cell 
      'is odd or even 
      If .Cells(i, "A").Value Mod 2 = 0 Then 
       .Cells(i, "B").Value = "Even" 
       Evens = Evens + 1 
      Else 
       .Cells(i, "B").Value = "Odd" 
       Odds = Odds + 1 
      End If 

     Next i 

     MsgBox "There are " & Evens & " Even and " & Odds & " Odd numbers" 
    End With 
End Sub 
+0

谢谢。我指的是你写的第二个代码。我已经使用它并找到了答案。无论如何感谢教育第一代码也。但是现在我有其他一些疑问。我写了下面的代码。请告诉我在“单元格”和“行”之前使用点 – Roumya

+0

Private Sub CommandButton3_Click() Dim rng1 As Range,rng2 As Range,i As Integer,LastRow As Long Set rng1 = Range(“A:A” ) 集RNG2 =范围( “B:B”) LASTROW =细胞(Rows.Count,1).END(xlUp).Row“发现在第1列 I = 0 的最后一个非空白行有关I = I + 1要LASTROW 如果细胞(I,1)模2 = 0。然后 细胞(I,2)= “连” 否则 细胞(I,2)= “奇” 结束如果 下一I MSGBOX “有” &Application.WorksheetFunction.CountIf(RNG2, “连”)& “偶” &Application.WorksheetFunction.CountIf(RNG2, “奇”)& “奇数” End Sub – Roumya

+0

在'With'块中,任何'.'开头的东西都被认为是指向'With'的对象,因此在'With ActiveSheet'中''Rows'是'ActiveSheet的快捷方式。行“ - 它节省了很多打字。如果您想将对象从“ActiveSheet”更改为“Worksheets(”Sheet4“)”(例如),那么它也变得更容易,因为您只需将'With ActiveSheet'更改为'With Worksheets(“Sheet4”)';你完成了。 – YowE3K