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:
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