Count occurrences for unknown values

cadlum

New Member
Joined
Apr 15, 2013
Messages
16
So i have a list of laptops spanning multiple worksheets. I have code that will go through and look for how many of each type of laptop there is. Right now, the code is static and will only search for the types of laptops i tell it to (hard coded into vba). This code works great. But now I am wanting to improve upon it.

For my improvements, I would like to have the code be able to look at the types columns, find new types, search the whole workbook for the type, and count how many it finds, then repeat for a new type.

I assume this is possible but it is a bit out of my skill level right now.

Thanks in advance for the help!

My current code:
Code:
Sub compTypes()


'Declaring variables
Dim wb2 As Workbook, sh As Worksheet, NewSh As Worksheet, i As Long
Dim E4300 As Integer, E4310 As Integer, E6320 As Integer, E6400 As Integer, E6410 As Integer
Dim E6420 As Integer, E6430 As Integer, O780 As Integer, O790 As Integer, O7010 As Integer
Dim FilePath As String, FileName1 As String


'Checks to make sure the file and path are loaded
If IsEmpty(Range("G1").Value) Or IsEmpty(Range("G2").Value) Then
    Call updateFileLocations.UpdateFileLoc
End If


'Read in file name and location into variables
FileName1 = Range("G1").Value
FilePath = Range("G2").Value


'Open inventory list sheet
Workbooks.Open FileName:=FilePath & FileName1


'Create a new temp sheet and setting inventory lists to variables
Set wb2 = Workbooks(FileName1)
Set NewSh = ThisWorkbook.Sheets.Add(After:=Sheets(ThisWorkbook.Sheets.Count))


'Labelling new temp sheet
NewSh.Name = "Computer Types"


'Labelling cells for table format
With NewSh
    .Range("A1") = "Computer Type"
    .Range("B1") = "Amount"
    .Range("A2") = "Dell Latitude E4300"
    .Range("A3") = "Dell Latitude E4310"
    .Range("A4") = "Dell Latitude E6320"
    .Range("A5") = "Dell Latitude E6400"
    .Range("A6") = "Dell Latitude E6410"
    .Range("A7") = "Dell Latitude E6420"
    .Range("A8") = "Dell Latitude E6430"
    .Range("A9") = "Dell Optiplex 780"
    .Range("A10") = "Dell Optiplex 790"
    .Range("A11") = "Dell Optiplex 7010"
    .Range("A13") = "Total Deployed"
End With


'Creating array of workbooks being used
wb = Array(wb2)


'A loop that counts how many different types of machines exist
For i = LBound(wb) To UBound(wb)
    For Each sh In wb(i).Sheets
        If Application.CountA(sh.Range("G4:G103")) > 0 Then
            E4300 = E4300 + Application.WorksheetFunction.CountIf(sh.Range("G4:G103"), "4300")
            E4310 = E4310 + Application.WorksheetFunction.CountIf(sh.Range("G4:G103"), "4310")
            E6320 = E6320 + Application.WorksheetFunction.CountIf(sh.Range("G4:G103"), "6320")
            E6400 = E6400 + Application.WorksheetFunction.CountIf(sh.Range("G4:G103"), "6400")
            E6410 = E6410 + Application.WorksheetFunction.CountIf(sh.Range("G4:G103"), "6410")
            E6420 = E6420 + Application.WorksheetFunction.CountIf(sh.Range("G4:G103"), "6420")
            E6430 = E6430 + Application.WorksheetFunction.CountIf(sh.Range("G4:G103"), "6430")
            O780 = O780 + Application.WorksheetFunction.CountIf(sh.Range("G4:G103"), "780")
            O790 = O790 + Application.WorksheetFunction.CountIf(sh.Range("G4:G103"), "790")
            O7010 = O7010 + Application.WorksheetFunction.CountIf(sh.Range("G4:G103"), "7010")
            'Adds current count of each variable to temp sheet
            With NewSh
                .Range("B2") = E4300
                .Range("B3") = E4310
                .Range("B4") = E6320
                .Range("B5") = E6400
                .Range("B6") = E6410
                .Range("B7") = E6420
                .Range("B8") = E6430
                .Range("B9") = O780
                .Range("B10") = O790
                .Range("B11") = O7010
            End With
        End If
    Next
Next


'Creating temp sheet's own workbook and formatting the sheet
ThisWorkbook.Sheets("Computer Types").Copy
ActiveSheet.Columns.AutoFit
ActiveSheet.Range("B13").Select
'Add a formula at the end to do a total of all machines
ActiveCell.FormulaR1C1 = "=SUM(R[-11]C:R[-2]C)"
ActiveSheet.Range("A14").Select


'Saving new workbook and deleting temp
ActiveWorkbook.SaveAs FilePath & "\Reports\Computer Types " & Format(Date, "mmm-yyyy") & ".xlsx"
Application.DisplayAlerts = False
ThisWorkbook.Sheets("Computer Types").Delete
Application.DisplayAlerts = True


'Closing inventory list
Workbooks(FileName1).Close SaveChanges:=False
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
OK, first of all I notice that you declare most of, but not all of your variables. Such as wb is not declared before use. This could easily lead to errors as wb and wb2 are nearly equal in name but not in type. Make a habit of declaring all variables. Better still, go (in VBA editor) to Tools/Options and tcik the 'Require Variable Decleration' tickbox. Now VBA editor will start every new module with
Code:
Option Explicit
which tells it to check.

Olso in your for...next loops, put the variable behind next to make it clear for yourself what is looping:
Code:
for i = 1 to 100
    ...
next i

I would modify the count loop to read the G4:G103 range into an array, and then check each type and count in another array. below is a possible way of doing so. Doing this in an array rather than reading each cell in turn makes a huge speed difference


So now your code becomes:

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> compTypes()<br><br><br>    <SPAN style="color:#007F00">'Declaring variables</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> wb2 <SPAN style="color:#00007F">As</SPAN> Workbook, sh <SPAN style="color:#00007F">As</SPAN> Worksheet, NewSh <SPAN style="color:#00007F">As</SPAN> Worksheet, i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> E4300 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, E4310 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, E6320 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, E6400 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, E6410 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> E6420 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, E6430 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, O780 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, O790 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, O7010 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> FilePath <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, FileName1 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <br>    <br>    <SPAN style="color:#007F00">'Checks to make sure the file and path are loaded</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> IsEmpty(Range("G1").Value) <SPAN style="color:#00007F">Or</SPAN> IsEmpty(Range("G2").Value) <SPAN style="color:#00007F">Then</SPAN><br>        <SPAN style="color:#00007F">Call</SPAN> updateFileLocations.UpdateFileLoc<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <br>    <br>    <SPAN style="color:#007F00">'Read in file name and location into variables</SPAN><br>    FileName1 = Range("G1").Value<br>    FilePath = Range("G2").Value<br>    <br>    <br>    <SPAN style="color:#007F00">'Open inventory list sheet</SPAN><br>    Workbooks.Open Filename:=FilePath & FileName1<br>    <br>    <br>    <SPAN style="color:#007F00">'Create a new temp sheet and setting inventory lists to variables</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> wb2 = Workbooks(FileName1)<br>    <SPAN style="color:#00007F">Set</SPAN> NewSh = ThisWorkbook.Sheets.Add(After:=Sheets(ThisWorkbook.Sheets.Count))<br>    <br>    <br>    <SPAN style="color:#007F00">'Labelling new temp sheet</SPAN><br>    NewSh.Name = "Computer Types"<br>    <br>    <br>    <SPAN style="color:#007F00">'Labelling cells for table format</SPAN><br>    <SPAN style="color:#00007F">With</SPAN> NewSh<br>        .Range("A1") = "Computer Type"<br>        .Range("B1") = "Amount"<br>        .Range("A2") = "Dell Latitude E4300"<br>        .Range("A3") = "Dell Latitude E4310"<br>        .Range("A4") = "Dell Latitude E6320"<br>        .Range("A5") = "Dell Latitude E6400"<br>        .Range("A6") = "Dell Latitude E6410"<br>        .Range("A7") = "Dell Latitude E6420"<br>        .Range("A8") = "Dell Latitude E6430"<br>        .Range("A9") = "Dell Optiplex 780"<br>        .Range("A10") = "Dell Optiplex 790"<br>        .Range("A11") = "Dell Optiplex 7010"<br>        .Range("A13") = "Total Deployed"<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <br>    <br>    <SPAN style="color:#007F00">'Creating array of workbooks being used</SPAN><br>    wb = Array(wb2)<br>    <br>          <SPAN style="color:#00007F">Dim</SPAN> aTypes <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>, aCount <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>, bFound <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN><br>          <SPAN style="color:#00007F">Dim</SPAN> k <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, j <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>          <br>          <SPAN style="color:#00007F">ReDim</SPAN> aCount(1 <SPAN style="color:#00007F">To</SPAN> 2, 1 <SPAN style="color:#00007F">To</SPAN> 1)<br>        <SPAN style="color:#007F00">'  ...</SPAN><br>    <br>    <SPAN style="color:#007F00">'A loop that counts how many different types of machines exist</SPAN><br>    <SPAN style="color:#00007F">For</SPAN> i = <SPAN style="color:#00007F">LBound</SPAN>(wb) <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(wb)<br>        <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> sh <SPAN style="color:#00007F">In</SPAN> wb(i).Sheets<br>            <br>            aTypes = Range("G4:G103")<br>            <br>            <SPAN style="color:#007F00">'Put first type in array</SPAN><br>            aCount(1, 1) = aTypes(1, 1)<br>            <SPAN style="color:#00007F">For</SPAN> k = <SPAN style="color:#00007F">LBound</SPAN>(aTypes) <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(aTypes)<br>                bFound = <SPAN style="color:#00007F">False</SPAN><br>                <SPAN style="color:#00007F">If</SPAN> aTypes(k, 1) <> vbNullString <SPAN style="color:#00007F">Then</SPAN><br>                    <SPAN style="color:#00007F">For</SPAN> j = <SPAN style="color:#00007F">LBound</SPAN>(aCount, 2) <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(aCount, 2)<br>                        <SPAN style="color:#00007F">If</SPAN> aCount(1, j) = aTypes(k, 1) <SPAN style="color:#00007F">Then</SPAN><br>                            aCount(2, j) = aCount(2, j) + 1<br>                            bFound = <SPAN style="color:#00007F">True</SPAN><br>                            <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">For</SPAN><br>                        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>                    <SPAN style="color:#00007F">Next</SPAN> j<br>                    <SPAN style="color:#00007F">If</SPAN> bFound = <SPAN style="color:#00007F">False</SPAN> <SPAN style="color:#00007F">Then</SPAN>  <SPAN style="color:#007F00">' new type</SPAN><br>                            <SPAN style="color:#007F00">' expand count array for new name, keep existing info</SPAN><br>                        <SPAN style="color:#00007F">ReDim</SPAN> <SPAN style="color:#00007F">Preserve</SPAN> aCount(1 <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(aCount, 1), 1 <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(aCount, 2) + 1)<br>     <SPAN style="color:#007F00">'                  ReDim Preserve myArray(UBound(myArray, 1), <SPAN style="color:#00007F">UBound</SPAN>(myArray, 2) + 1)</SPAN><br>                        <SPAN style="color:#007F00">'add new name to array and add one for score</SPAN><br>                        aCount(1, <SPAN style="color:#00007F">UBound</SPAN>(aCount, 2)) = aTypes(k, 1)<br>                        aCount(2, UBound(aCount, 2)) = 1<br>                    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>            <SPAN style="color:#00007F">Next</SPAN> k<br>                        <br>            <br>            <br>            <br><SPAN style="color:#007F00">'            If Application.CountA(sh.Range("G4:G103")) > 0 Then</SPAN><br><SPAN style="color:#007F00">'                E4300 = E4300 + Application.WorksheetFunction.CountIf(sh.Range("G4:G103"), "4300")</SPAN><br><SPAN style="color:#007F00">'                E4310 = E4310 + Application.WorksheetFunction.CountIf(sh.Range("G4:G103"), "4310")</SPAN><br><SPAN style="color:#007F00">'                E6320 = E6320 + Application.WorksheetFunction.CountIf(sh.Range("G4:G103"), "6320")</SPAN><br><SPAN style="color:#007F00">'                E6400 = E6400 + Application.WorksheetFunction.CountIf(sh.Range("G4:G103"), "6400")</SPAN><br><SPAN style="color:#007F00">'                E6410 = E6410 + Application.WorksheetFunction.CountIf(sh.Range("G4:G103"), "6410")</SPAN><br><SPAN style="color:#007F00">'                E6420 = E6420 + Application.WorksheetFunction.CountIf(sh.Range("G4:G103"), "6420")</SPAN><br><SPAN style="color:#007F00">'                E6430 = E6430 + Application.WorksheetFunction.CountIf(sh.Range("G4:G103"), "6430")</SPAN><br><SPAN style="color:#007F00">'                O780 = O780 + Application.WorksheetFunction.CountIf(sh.Range("G4:G103"), "780")</SPAN><br><SPAN style="color:#007F00">'                O790 = O790 + Application.WorksheetFunction.CountIf(sh.Range("G4:G103"), "790")</SPAN><br><SPAN style="color:#007F00">'                O7010 = O7010 + Application.WorksheetFunction.CountIf(sh.Range("G4:G103"), "7010")</SPAN><br><SPAN style="color:#007F00">'                'Adds current count of each variable to temp sheet</SPAN><br><SPAN style="color:#007F00">'                With NewSh</SPAN><br><SPAN style="color:#007F00">'                    .Range("B2") = E4300</SPAN><br><SPAN style="color:#007F00">'                    .Range("B3") = E4310</SPAN><br><SPAN style="color:#007F00">'                    .Range("B4") = E6320</SPAN><br><SPAN style="color:#007F00">'                    .Range("B5") = E6400</SPAN><br><SPAN style="color:#007F00">'                    .Range("B6") = E6410</SPAN><br><SPAN style="color:#007F00">'                    .Range("B7") = E6420</SPAN><br><SPAN style="color:#007F00">'                    .Range("B8") = E6430</SPAN><br><SPAN style="color:#007F00">'                    .Range("B9") = O780</SPAN><br><SPAN style="color:#007F00">'                    .Range("B10") = O790</SPAN><br><SPAN style="color:#007F00">'                    .Range("B11") = O7010</SPAN><br><SPAN style="color:#007F00">'                End With</SPAN><br><SPAN style="color:#007F00">'            End If</SPAN><br>        <SPAN style="color:#00007F">Next</SPAN> sh<br>    <SPAN style="color:#00007F">Next</SPAN> i<br>    <br>    Range("B2").Resize(UBound(aCount, 2), 2).Value = Application.Transpose(aCount)<br>    <br>    <SPAN style="color:#007F00">'In printing out the types and count in the line above, this is done on order _<br>      of appearance, so you need to find a way to order your headings in column A _<br>      in accordance.</SPAN><br>    <br>    <SPAN style="color:#007F00">'Creating temp sheet's own workbook and formatting the sheet</SPAN><br>    ThisWorkbook.Sheets("Computer Types").Copy<br>    ActiveSheet.Columns.AutoFit<br>    ActiveSheet.Range("B13").Select<br>    <SPAN style="color:#007F00">'Add a formula at the end to do a total of all machines</SPAN><br>    ActiveCell.FormulaR1C1 = "=SUM(R[-11]C:R[-2]C)"<br>    ActiveSheet.Range("A14").Select<br>    <br>    <br>    <SPAN style="color:#007F00">'Saving new workbook and deleting temp</SPAN><br>    ActiveWorkbook.SaveAs FilePath & "\Reports\Computer Types " & Format(Date, "mmm-yyyy") & ".xlsx"<br>    Application.DisplayAlerts = <SPAN style="color:#00007F">False</SPAN><br>    ThisWorkbook.Sheets("Computer Types").Delete<br>    Application.DisplayAlerts = <SPAN style="color:#00007F">True</SPAN><br>    <br>    <br>    <SPAN style="color:#007F00">'Closing inventory list</SPAN><br>    Workbooks(FileName1).Close SaveChanges:=<SPAN style="color:#00007F">False</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
Thank you for the code. I will start testing with it to let you know how it goes and if i have any questions. Also, sorry for the sloppiness of not declaring stuff properly. I try to do that as much as possible so thanks for the tip on the option explicit.
 
Upvote 0
Sorry for the delay. I finally got a chance to test the code. I am getting a blank worksheet (well the counts are blank). Since this code is a bit over my head, I am having trouble debugging it. Could you help describe what the code is doing or let me know where this might be failing? Thanks
 
Upvote 0
Can you post your spreadsheet on dropbox or similar? Just to get the columns etc 100% correct
 
Upvote 0
So i had a moment to play with the code a bit longer. I saw that the for loops are picking up on the different types of computers that I have. From the looks of it, everything in the for loops Is functioning properly. After all the for loops, you had the code of:
Code:
 Range("B2").Resize(UBound(aCount, 2), 2).Value = Application.Transpose(aCount)
I found that if I added 'NewSh.' to the beginning of it, it at least got some output on the correct sheet. Now the output I am getting isn't printing out properly. The B column comes up with only the first type it found. Then the C column comes up with some counts. The first row is a higher number and the rest of the rows are just 1. My thinking is that this line needs to be a for loop to be able to print things out properly, is that right?

For my files themselves, I can't upload them because of some privacy stuff, but here is the headers and a sample line:
ABCDEFGHIJKLMN
SERVICE TAGShipped DateLease EndWarranty thruWarranty TypeTypeDrive #LicenseUSERVPN ExpirationDEPTWindowsOther
XXXXXXX5/31/20135/31/20165/31/2016Gold6410XX-XXX1First Last5/31/2013BusinessWindows 7Notes

<tbody>
</tbody>

Thanks again for all the help!
 
Upvote 0
I have tested the following code on a workbook with a few sheets structured according to your example and (after commenting out all the file opening and closing lines) it produces a table with the types and the counts.

Writing an array to a sheet is a one write operation, so no looping. That is why arrays are used so often: speed. Same as reading the PC list into the atypes array, only one read. Then the looping and comparing variables can be done on memory from the arrays.

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> compTypes()<br><br><br>    <SPAN style="color:#007F00">'Declaring variables</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> wb2 <SPAN style="color:#00007F">As</SPAN> Workbook, sh <SPAN style="color:#00007F">As</SPAN> Worksheet, NewSh <SPAN style="color:#00007F">As</SPAN> Worksheet<br>    <SPAN style="color:#00007F">Dim</SPAN> FilePath <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, FileName1 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> aTypes <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>, aCount <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>, bFound <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> k <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, j <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>    <br>    <br>    <SPAN style="color:#007F00">'Checks to make sure the file and path are loaded</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> IsEmpty(Range("G1").Value) <SPAN style="color:#00007F">Or</SPAN> IsEmpty(Range("G2").Value) <SPAN style="color:#00007F">Then</SPAN><br>        <SPAN style="color:#00007F">Call</SPAN> updateFileLocations.UpdateFileLoc<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><br><br>    <SPAN style="color:#007F00">'Read in file name and location into variables</SPAN><br>    FileName1 = Range("G1").Value<br>    FilePath = Range("G2").Value<br><br><br>    <SPAN style="color:#007F00">'Open inventory list sheet</SPAN><br>    Workbooks.Open Filename:=FilePath & FileName1<br><br><br>    <SPAN style="color:#007F00">'Create a new temp sheet and setting inventory lists to variables</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> wb2 = Workbooks(FileName1)<br>    <SPAN style="color:#00007F">Set</SPAN> NewSh = ThisWorkbook.Sheets.Add(After:=Sheets(ThisWorkbook.Sheets.Count))<br>    <br>    <SPAN style="color:#007F00">' Stop screen flikker, speed up macro</SPAN><br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    <br>    <SPAN style="color:#007F00">' We set the counting array to take a first type. For each new type _<br>      it will be expanded</SPAN><br>    <SPAN style="color:#00007F">ReDim</SPAN> aCount(1 <SPAN style="color:#00007F">To</SPAN> 2, 1 <SPAN style="color:#00007F">To</SPAN> 1)<br>    <br>    <SPAN style="color:#007F00">'A loop that counts how many different types of machines exist</SPAN><br>        <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> sh <SPAN style="color:#00007F">In</SPAN> wb2.Sheets<br>            <br>            <SPAN style="color:#007F00">' Load the list of PCs into an array</SPAN><br>            aTypes = sh.Range("G4:G103")<br>            <br>            <SPAN style="color:#007F00">'Put first type in array</SPAN><br>            <SPAN style="color:#00007F">If</SPAN> aCount(1, 1) = vbNullString <SPAN style="color:#00007F">Then</SPAN> aCount(1, 1) = aTypes(1, 1)<br>            <br>            <SPAN style="color:#00007F">For</SPAN> k = <SPAN style="color:#00007F">LBound</SPAN>(aTypes) <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(aTypes)<br>                bFound = <SPAN style="color:#00007F">False</SPAN><br>                <SPAN style="color:#00007F">If</SPAN> aTypes(k, 1) <> vbNullString <SPAN style="color:#00007F">Then</SPAN><br>                    <SPAN style="color:#00007F">For</SPAN> j = <SPAN style="color:#00007F">LBound</SPAN>(aCount, 2) <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(aCount, 2)<br>                        <SPAN style="color:#00007F">If</SPAN> aCount(1, j) = aTypes(k, 1) <SPAN style="color:#00007F">Then</SPAN><br>                            aCount(2, j) = aCount(2, j) + 1<br>                            bFound = <SPAN style="color:#00007F">True</SPAN><br>                            <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">For</SPAN><br>                        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>                    <SPAN style="color:#00007F">Next</SPAN> j<br>                    <SPAN style="color:#00007F">If</SPAN> bFound = <SPAN style="color:#00007F">False</SPAN> <SPAN style="color:#00007F">Then</SPAN>  <SPAN style="color:#007F00">' new type</SPAN><br>                            <SPAN style="color:#007F00">' expand count array for new name, keep existing info</SPAN><br>                        <SPAN style="color:#00007F">ReDim</SPAN> <SPAN style="color:#00007F">Preserve</SPAN> aCount(1 <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(aCount, 1), 1 <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(aCount, 2) + 1)<br>                        <br>                        <SPAN style="color:#007F00">'add new name to array and add one for score</SPAN><br>                        aCount(1, <SPAN style="color:#00007F">UBound</SPAN>(aCount, 2)) = aTypes(k, 1)<br>                        aCount(2, <SPAN style="color:#00007F">UBound</SPAN>(aCount, 2)) = 1<br>                    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>            <SPAN style="color:#00007F">Next</SPAN> k<br>                        <br>            <br>        <SPAN style="color:#00007F">Next</SPAN> sh<br>    <br>    <br>    <br>    <SPAN style="color:#00007F">With</SPAN> NewSh<br>    <SPAN style="color:#007F00">'Labelling new temp sheet</SPAN><br>        .Name = "Computer Types"<br>    <SPAN style="color:#007F00">'Labelling cells for table format</SPAN><br>        .Range("A1") = "Computer Type"<br>        .Range("B1") = "Amount"<br>        .Cells(UBound(aCount, 2) + 3, 1) = "Total Deployed"<br>    <SPAN style="color:#007F00">' Add PC names and counts</SPAN><br>        .Range("A2").Resize(UBound(aCount, 2), 2).Value = Application.Transpose(aCount)<br>    <SPAN style="color:#007F00">'Add a formula at the end to do a total of all machines</SPAN><br>        .Cells(UBound(aCount, 2) + 3, 2).FormulaR1C1 = "=SUM(R[-" & <SPAN style="color:#00007F">UBound</SPAN>(aCount, 2) + 2 & "]C:R[-2]C)"<br>        .Columns.AutoFit<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <br>    <SPAN style="color:#007F00">'In printing out the types and count in the line above, this is done on order _<br>      of appearance, so you need to find a way to order your headings in column A _<br>      in accordance.</SPAN><br>    <br>    <SPAN style="color:#007F00">'Creating temp sheet's own workbook</SPAN><br>    wb2.Sheets("Computer Types").Copy<br>    <br><br>    <br>    <SPAN style="color:#007F00">'Saving new workbook and deleting temp</SPAN><br>    ActiveWorkbook.SaveAs FilePath & "\Reports\Computer Types " & Format(Date, "mmm-yyyy") & ".xlsx"<br>    <SPAN style="color:#00007F">With</SPAN> Application<br>        .DisplayAlerts = <SPAN style="color:#00007F">False</SPAN><br>        wb2.Sheets("Computer Types").Delete<br>        .DisplayAlerts = <SPAN style="color:#00007F">True</SPAN><br>        .ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <br>    <br>    <SPAN style="color:#007F00">'Closing inventory list</SPAN><br>    Workbooks(FileName1).Close SaveChanges:=<SPAN style="color:#00007F">False</SPAN><br>    <br>    <SPAN style="color:#007F00">' Clean up resources</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> sh = <SPAN style="color:#00007F">Nothing</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> wb2 = <SPAN style="color:#00007F">Nothing</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> NewSh = <SPAN style="color:#00007F">Nothing</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
In case people come across this code and might want this...

Here is some code to glamour up the output a little. This will resize the columns, label them, add a sum at the bottom, and then sort them (I added the sort).

Code:
UBaCount2 = UBound(aCount, 2)

With NewSh
    'Labelling new temp sheet
    .Name = "Computer Types"
    'Labelling cells for table format
    .Range("A1") = "Computer Type"
    .Range("B1") = "Amount"
    .Cells(UBaCount2 + 3, 1) = "Total Deployed"
    'Add PC names and counts
    .Range("A2").Resize(UBaCount2, 2).Value = Application.Transpose(aCount)
    'Add a formula at the end to do a total of all machines
    .Cells(UBaCount2 + 3, 2).FormulaR1C1 = "=SUM(R[-" & UBaCount2 + 2 & "]C:R[-2]C)"
    .Columns.AutoFit
End With
With NewSh.Sort
    .SortFields.Clear
    .SortFields.Add Key:=Range("A2:A" & UBaCount2 + 1), SortOn:=xlSortOnValues, _
        Order:=xlAscending, DataOption:=xlSortNormal
    .SetRange Range("A1:B" & UBaCount2 + 1)
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
 
Upvote 0

Forum statistics

Threads
1,215,511
Messages
6,125,247
Members
449,217
Latest member
Trystel

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