This code works great for counting text for 2 or more rows, but I get a "RunTime Error 13, type mismatch" error when there is only one row of data.

VBAProIWish

Well-known Member
Joined
Jul 6, 2009
Messages
1,027
Office Version
  1. 365
Platform
  1. Windows
Hello All,

This code works great for counting text for 2 or more rows, but I get a "RunTime Error 13, type mismatch" error when there is only one row of data.


Code:
Sub aaa_test_macro_01()
 
Sheets("Cans, O").Select


'CREATE A WORKSHEET NAMED "TOTALS" AND PUT TEXT TOTALS IN THERE
'This macro counts the number of times a word appears in selected columns and then creates
'a totals column with the text count of each column


'Sub Add_Text_Totals()
Dim rData As Range, rFind As Range
Dim vTotals() As Variant, vInput() As Variant, vItems As Variant
Dim i As Long, j As Long, n As Long


Application.ScreenUpdating = False
Set DataSheet = ActiveSheet


If Not SheetExists("Totals") Then Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Totals"
DataSheet.Activate
vItems = Array("Shipping Status")  'Items to be searched -  adjust to suit


For j = LBound(vItems) To UBound(vItems)
    Set rFind = Cells.Find(what:=vItems(j), LookAt:=xlWhole)
    Set rData = Range(rFind.Offset(1), Cells(Rows.Count, rFind.Column).End(xlUp))




    vInput = rData.Value
    
    On Error Resume Next
        rData.SpecialCells(xlCellTypeBlanks).Interior.ColorIndex = 0
    On Error GoTo 0
    
    ReDim vTotals(1 To UBound(vInput, 1), 1 To 2)
               
    With CreateObject("Scripting.Dictionary")
    .CompareMode = vbTextCompare
        For i = 1 To UBound(vInput, 1)
            If Not .exists(vInput(i, 1)) Then
                n = n + 1
                vTotals(n, 1) = vInput(i, 1)
                vTotals(n, 2) = vTotals(n, 2) + 1
                .Add vInput(i, 1), n
            ElseIf .exists(vInput(i, 1)) Then
                vTotals(.item(vInput(i, 1)), 2) = vTotals(.item(vInput(i, 1)), 2) + 1
            End If
        Next i
    End With
    
    With Sheets("Totals").Cells(Rows.Count, 1).End(xlUp)(2)
        .Value = vItems(j) & " totals"
        .Offset(, 1).Value = rData.Count
        .Offset(1).Resize(n, 2) = vTotals
        On Error Resume Next
            .Offset(1).Resize(n).SpecialCells(xlCellTypeBlanks) = "Blanks"
        On Error GoTo 0
        .Columns.AutoFit
    End With
    
    n = 0
    Erase vTotals
    Erase vInput
    Set rFind = Nothing
    Set rData = Nothing
Next j


Application.ScreenUpdating = True

End Sub


Function SheetExists(SName As String) As Boolean


On Error Resume Next
SheetExists = CBool(Len(Sheets(SName).Name))


End Function

Is there a way to modify this code so that it works when there is one row of data as well?

Thanks much!
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

Forum statistics

Threads
1,215,200
Messages
6,123,611
Members
449,109
Latest member
Sebas8956

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top