I have a folder with many workbooks, which are pretty much the same layout. They include most of the codes as well as similar values, which you can see in column H. Let's have a look at the example below: I have the values falling under the code of 1682, within which I might have the Uplift code 1128U, 1126U, or other or simply empty value. I need to calculate all the values belonging to the given code (column A) and uplift the code (column H) at once. These values will come from all the workbooks in the given directory. The values, you can see here are just from one workbook. I have another 45 workbooks with other values than seen in the screen below and I would like to sum them up. How can I do it with the code below?
where in the H column are different values assigned to the code in column A.
I would like to have the correct sums calculated for the particular values in column H, which fall within the code as shown in column A. These values in column H might repeat across other codes in column A, as you see below in the case of codes 1687 and 4032 but they must represent individual records.
The problem is, that the simple condition, shown below:
Sub SearchFolders_Cables()
Dim wbAct As Workbook, pathMainWb As String, fldrPath As String
Dim BOM As String, scrUpdt, WsOut As Worksheet, colFiles As Collection, f As Object
Dim xBol As Boolean, wb As Workbook, ws As Worksheet, arrWs
Dim matchedCells As Collection, Cell, numHits As Long, summRow As Long
Set WsCalc = Workbooks("BoM calculator v4.1G.xlsm")
Set wbAct = ActiveWorkbook
pathMainWb = wbAct.FullName '<<<<
On Error GoTo ErrHandler
fldrPath = UserSelectFolder("Select a folder")
If Len(fldrPath) = 0 Then Exit Sub
'get all files in the selected folder
Set colFiles = GetFileMatches(fldrPath, "*.xls*", False) 'False=no subfolders
If colFiles.Count = 0 Then
MsgBox "No Excel files found in selected folder"
Exit Sub
End If
'///////////////////////////CALCULATION EACH CODE FROM ALL FILES IN DIRECTORY/////////////////////////////
lRow = Cells(Rows.Count, 1).End(xlUp).Row
For Each iCell In Range("A2:A" & lRow).Cells
'-----------------------------------CASE 1----------------------------------------
If iCell.EntireRow.Range("H1").Value = "" Then
'BOM = InputBox("The current BoM Code is...", "BoM Calculator v1.1", (iCell.Value))
BOM = iCell.Value
scrUpdt = Application.ScreenUpdating
Application.ScreenUpdating = False
Set WsOut = ThisWorkbook.Worksheets("SUMMARY")
WsOut.UsedRange.Delete 'Clearing out previous records (very important!!!)
summRow = 1
'sheet names to scan
arrWs = Array("Cable Work Order")
WsOut.Cells(summRow, 1).Resize(1, 6).Value = Array("Workbook", "Worksheet", _
"Cell", "Text in Cell", "Values corresponding", "Uplift Code")
For Each f In colFiles
xBol = (f.Path = pathMainWb) 'file already open?
If xBol Then
Set wb = wbAct
Else
Set wb = Workbooks.Open(FileName:=f.Path, UpdateLinks:=0, _
ReadOnly:=True, AddToMRU:=False)
End If
For Each ws In wb.Worksheets
'are we interested in this sheet?
If Not IsError(Application.Match(ws.Name, arrWs, 0)) Then
Set matchedCells = FindAll(ws.UsedRange, BOM) 'get all cells with bom
If matchedCells.Count > 0 Then
For Each Cell In matchedCells
If Cell.EntireRow.Range("H1").Value = "" Then
summRow = summRow + 1
WsOut.Cells(summRow, 1).Resize(1, 6).Value = _
Array(wb.Name, ws.Name, Cell.Address, Cell.Value, _
Cell.EntireRow.Range("F1").Value, Cell.EntireRow.Range("H1").Value)
numHits = numHits + 1
End If
Next Cell 'next match
End If 'any bom matches
End If 'matched sheet name
Next ws
If Not xBol Then wb.Close False 'need to close this workbook?
Next f
With WsOut
Dim lastrow As Long
.Columns("A:F").EntireColumn.AutoFit
lastrow = WsOut.Cells(WsOut.Rows.Count, "E").End(xlUp).Row 'AutoSum all values
.Range("E" & lastrow + 1).Value = WorksheetFunction.Sum(WsOut.Range("E2:E" & lastrow + 1))
End With
With ThisWorkbook.Worksheets("Cable Work Order").Range("O1")
.Font.Color = RGB(240, 240, 240)
.Value = WsOut.Range("E" & lastrow + 1).Value
End With
For i = 2 To Excel.WorksheetFunction.CountA(Range("A:A")) 'Autocopy sum value
If Range("A" & i).Value = BOM Then
If Range("H" & i).Value = "" Then
Range("O1").Copy
With Range("F" & i)
.PasteSpecial xlPasteValues
.Font.Bold = True
.Font.Color = vbBlue
End With
End If
End If
Next i
'------------------------------------------------------------Case 2----------------------------------------------------------------------------------'
ElseIf iCell.EntireRow.Range("H1").Value = "1126U" Then
'BOM = InputBox("The current BoM Code is...", "BoM Calculator v1.1", (iCell.Value))
BOM = iCell.Value
scrUpdt = Application.ScreenUpdating
Application.ScreenUpdating = False
Set WsOut = ThisWorkbook.Worksheets("SUMMARY")
WsOut.UsedRange.Delete 'Clearing out previous records (very important!!!)
summRow = 1
'sheet names to scan
arrWs = Array("Cable Work Order")
WsOut.Cells(summRow, 1).Resize(1, 6).Value = Array("Workbook", "Worksheet", _
"Cell", "Text in Cell", "Values corresponding", "Uplift Code")
For Each f In colFiles
xBol = (f.Path = pathMainWb) 'file already open?
If xBol Then
Set wb = wbAct
Else
Set wb = Workbooks.Open(FileName:=f.Path, UpdateLinks:=0, _
ReadOnly:=True, AddToMRU:=False)
End If
For Each ws In wb.Worksheets
'are we interested in this sheet?
If Not IsError(Application.Match(ws.Name, arrWs, 0)) Then
Set matchedCells = FindAll(ws.UsedRange, BOM) 'get all cells with bom
If matchedCells.Count > 0 Then
For Each Cell In matchedCells
If Cell.EntireRow.Range("H1").Value = "1126U" Then
summRow = summRow + 1
WsOut.Cells(summRow, 1).Resize(1, 6).Value = _
Array(wb.Name, ws.Name, Cell.Address, Cell.Value, _
Cell.EntireRow.Range("F1").Value, Cell.EntireRow.Range("H1").Value)
numHits = numHits + 1
End If
Next Cell 'next match
End If 'any bom matches
End If 'matched sheet name
Next ws
If Not xBol Then wb.Close False 'need to close this workbook?
Next f
With WsOut
Dim lastrow2 As Long
.Columns("A:F").EntireColumn.AutoFit
lastrow2 = WsOut.Cells(WsOut.Rows.Count, "E").End(xlUp).Row 'AutoSum all values
.Range("E" & lastrow2 + 1).Value = WorksheetFunction.Sum(WsOut.Range("E2:E" & lastrow2 + 1))
End With
With ThisWorkbook.Worksheets("Cable Work Order").Range("O1")
.Font.Color = RGB(240, 240, 240)
.Value = WsOut.Range("E" & lastrow2 + 1).Value
End With
For i = 2 To Excel.WorksheetFunction.CountA(Range("A:A")) 'Autocopy sum value
If Range("A" & i).Value = BOM Then
If Range("H" & i).Value = "1127U" Then
Range("O1").Copy
With Range("F" & i)
.PasteSpecial xlPasteValues
.Font.Bold = True
.Font.Color = vbGreen
End With
End If
End If
Next i
'------------------------------------------------------------Case 3----------------------------------------------------------------------------------'
ElseIf iCell.EntireRow.Range("H1").Value = "1127U" Then
'BOM = InputBox("The current BoM Code is...", "BoM Calculator v1.1", (iCell.Value))
BOM = iCell.Value
scrUpdt = Application.ScreenUpdating
Application.ScreenUpdating = False
Set WsOut = ThisWorkbook.Worksheets("SUMMARY")
WsOut.UsedRange.Delete 'Clearing out previous records (very important!!!)
summRow = 1
'sheet names to scan
arrWs = Array("Cable Work Order")
WsOut.Cells(summRow, 1).Resize(1, 6).Value = Array("Workbook", "Worksheet", _
"Cell", "Text in Cell", "Values corresponding", "Uplift Code")
For Each f In colFiles
xBol = (f.Path = pathMainWb) 'file already open?
If xBol Then
Set wb = wbAct
Else
Set wb = Workbooks.Open(FileName:=f.Path, UpdateLinks:=0, _
ReadOnly:=True, AddToMRU:=False)
End If
For Each ws In wb.Worksheets
'are we interested in this sheet?
If Not IsError(Application.Match(ws.Name, arrWs, 0)) Then
Set matchedCells = FindAll(ws.UsedRange, BOM) 'get all cells with bom
If matchedCells.Count > 0 Then
For Each Cell In matchedCells
If Cell.EntireRow.Range("H1").Value = "1127U" Then
summRow = summRow + 1
WsOut.Cells(summRow, 1).Resize(1, 6).Value = _
Array(wb.Name, ws.Name, Cell.Address, Cell.Value, _
Cell.EntireRow.Range("F1").Value, Cell.EntireRow.Range("H1").Value)
numHits = numHits + 1
End If
Next Cell 'next match
End If 'any bom matches
End If 'matched sheet name
Next ws
If Not xBol Then wb.Close False 'need to close this workbook?
Next f
With WsOut
Dim lastrow3 As Long
.Columns("A:F").EntireColumn.AutoFit
lastrow = WsOut.Cells(WsOut.Rows.Count, "E").End(xlUp).Row 'AutoSum all values
.Range("E" & lastrow3 + 1).Value = WorksheetFunction.Sum(WsOut.Range("E2:E" & lastrow3 + 1))
End With
With ThisWorkbook.Worksheets("Cable Work Order").Range("O1")
.Font.Color = RGB(240, 240, 240)
.Value = WsOut.Range("E" & lastrow3 + 1).Value
End With
For i = 2 To Excel.WorksheetFunction.CountA(Range("A:A")) 'Autocopy sum value
If Range("A" & i).Value = BOM Then
If Range("H" & i).Value = "1127U" Then
Range("O1").Copy
With Range("F" & i)
.PasteSpecial xlPasteValues
.Font.Bold = True
.Font.Color = vbMagenta
End With
End If
End If
Next i
ElseIf iCell.EntireRow.Range("H1").Value = "1128U" Then '------------------------------------------------------------Case 4----------------------------------------------------------------------------------'
'BOM = InputBox("The current Uplift BoM Code is...", "BoM Calculator v1.1", (iCell.Value))
BOM = iCell.Value
scrUpdt = Application.ScreenUpdating
Application.ScreenUpdating = False
Set WsOut = ThisWorkbook.Worksheets("SUMMARY")
WsOut.UsedRange.Delete 'Clearing out previous records (very important!!!)
summRow = 1
'sheet names to scan
arrWs = Array("Cable Work Order")
WsOut.Cells(summRow, 1).Resize(1, 6).Value = Array("Workbook", "Worksheet", _
"Cell", "Text in Cell", "Values corresponding", "Uplift Code")
For Each f In colFiles
xBol = (f.Path = pathMainWb) 'file already open?
If xBol Then
Set wb = wbAct
Else
Set wb = Workbooks.Open(FileName:=f.Path, UpdateLinks:=0, _
ReadOnly:=True, AddToMRU:=False)
End If
For Each ws In wb.Worksheets
'are we interested in this sheet?
If Not IsError(Application.Match(ws.Name, arrWs, 0)) Then
Set matchedCells = FindAll(ws.UsedRange, BOM) 'get all cells with bom
If matchedCells.Count > 0 Then
For Each Cell In matchedCells
If Cell.EntireRow.Range("H1").Value = "1128U" Then
summRow = summRow + 1
WsOut.Cells(summRow, 1).Resize(1, 6).Value = _
Array(wb.Name, ws.Name, Cell.Address, Cell.Value, _
Cell.EntireRow.Range("F1").Value, Cell.EntireRow.Range("H1").Value)
numHits = numHits + 1
End If
Next Cell 'next match
End If 'any bom matches
End If 'matched sheet name
Next ws
If Not xBol Then wb.Close False 'need to close this workbook?
Next f
With WsOut
Dim lastrow4 As Long
.Columns("A:F").EntireColumn.AutoFit
lastrow2 = WsOut.Cells(WsOut.Rows.Count, "E").End(xlUp).Row 'AutoSum all values
.Range("E" & lastrow4 + 1).Value = WorksheetFunction.Sum(WsOut.Range("E2:E" & lastrow4 + 1))
End With
With ThisWorkbook.Worksheets("Cable Work Order").Range("O1")
.Font.Color = RGB(240, 240, 240)
.Value = WsOut.Range("E" & lastrow4 + 1).Value
End With
For i = 2 To Excel.WorksheetFunction.CountA(Range("A:A")) 'Autocopy sum value
If Range("A" & i).Value = BOM Then
If Range("H" & i).Value = "1128U" Then
Range("O1").Copy
With Range("F" & i)
.PasteSpecial xlPasteValues
.Font.Bold = True
.Font.Color = vbRed
End With
End If
End If
Next I
End If
Next iCell
MsgBox "Process completed"
ExitHandler:
Application.ScreenUpdating = scrUpdt
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation
Resume ExitHandler
End Sub
I am getting the wrong values as you can see below:
In the standard case, when considering the existence of value in column H or not, I have two separate records, which is fine. The situation is complicated when the code is expanded. Then I can get the value for a third record, but the second record is still the same whereas the value should be deducted by the value of record 3.
Is there a way of setting the conditions for any value, which varies from each other?
I was thinking about multiple or conditions Compress multiple OR-conditions in VBA code
but they don't work in my case. Will the Case - switch option be better for example? Or Nested if conditions? I need to make sure, that the code treats every single value in the H column differently from each other. So maybe this approach could be helpful: Quicker way to get all unique values of a column in VBA?