简体   繁体   中英

Identifying and counting elements of the same value in an array Excel VBA

I would like to count the numbers of "c" inside my array txt. The transition for 1 to 0 means a new cycle, thats why i put a "c" whenever this happen. In this code I get type mismatch when i try to count the "c". Thank you for the help.

Sub CopyColumn()
Dim finalrow As Long
Dim i As Variant
ReDim arrayciclos(0)
Dim str As String
Dim ciclos As Variant

finalrow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To finalrow
 arrayciclos(UBound(arrayciclos)) = Range("J" & i)
 ReDim Preserve arrayciclos(UBound(arrayciclos) + 1)
Next i


For i = LBound(arrayciclos) To UBound(arrayciclos)
    txt = txt & arrayciclos(i) ' & vbCrLf
  Next i

  MsgBox txt


Do While InStr(txt, "10")
    txt = Replace(txt, "10", "c")
    Loop
MsgBox txt


ciclos = 0: i = 0
For i = 0 To finalrow
    If txt(i) = "c" Then ' i have Type Mismatch here
        ciclos = ciclos + 1
    End If
Next

MsgBox (ciclos)


End Sub

I think you are using too many loops. If you want to loop through the array txt To count the number of "c" then you only need to do

        Dim MyArray As Variant
        dim count as integer
        'specify your range. For example
        MyArray = Range("BS27:DS50000")

        'Loop through and find all occurrences of "c"
        For i = LBound(MyArray) To UBound(MyArray)
          For j = LBound(MyArray, 2) To UBound(MyArray, 2)
             If MyArray(i, j) = "c" Then count = count + 1
          Next j
        Next i

        msgbox count

If "c" is not single within each cell then replace If MyArray(i, j) = "c" with If MyArray(i, j) = "c" and put asterix within both sides of "c" like asterix c asterix. This will be a character match

Based on your posted code, the txt variable is not explicitly declared. In addition, the implicit declaration of variant becomes a type of string when you use the second loop to concatenate the values together with txt = txt & arrayciclos(I) . You should modify the code to include the Option Explicit statement to check for undeclared variables. In addition you should use the Mid function to perform the checks for the "c" character in your final loop.

Option Explicit   ' <-- added to check for undeclared variables

Sub CopyColumn()
Dim finalrow As Long
Dim i As Variant
ReDim arrayciclos(0)
Dim str As String   ' <-- unused variable problaby should have been named txt in your example
Dim ciclos As Variant

finalrow = Cells(Rows.Count, 1).End(xlUp).Row
finalrow = 9
For i = 2 To finalrow
 arrayciclos(UBound(arrayciclos)) = Range("J" & i)
 ReDim Preserve arrayciclos(UBound(arrayciclos) + 1)
Next i


For i = LBound(arrayciclos) To UBound(arrayciclos)
    str = str & arrayciclos(i) ' & vbCrLf
  Next i

  MsgBox str


Do While InStr(str, "10")
    str = Replace(str, "10", "c")
    Loop
MsgBox str


ciclos = 0: i = 0
For i = 0 To finalrow
    If Mid(str, i + 1, 1) = "c" Then ' <-- changed to Mid(string, start, len)
        ciclos = ciclos + 1
    End If
Next

MsgBox (ciclos)


End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM