I've been writing a code that uses 3 workbooks - but I am having issues with the final output.
- workbook 1 (wb1 - this workbook - where the macro is run on - and the final code will be displayed)
- workbook 2 (wb2) which is a customer database for product orders
- workbook 3 (wb3) which is a reference file for weights (to be manipulated in workbook 2)
wb1 opens up wb2 and wb3, cross-references (using VLOOKUP) the weights in wb3, copies them over to the corresponding customer address in wb2, then multiples the weights by the quantity ordered in wb2's address line.
The entire code works as I planned, except for the final output. wb2 now has the final weights in column Q.
- All that is left is for the "PO Number" in wb1 (column K) to lookup the multiple "PO Number"s in wb2 (column C as well)
- Sum wb2's weights (column Q) where there is a match
- Return that sum back to wb1. I've tried sumif, but to no avail.
Here is the final output code (it returns no values at the moment), with the entire code posted below for reference.
'Enter in the weights data into the final sheet
tempCount = 0
lastCount = lastRow1
For tempCount = 1 To lastCount
Set lookFor = wb1.Sheets(1).Cells(tempCount + 1, 11) ' value to find
Set lookForRange = wb2.Sheets(1).Range("$C$2:$C$" & lastRow2) 'Range of values to lookup
Set srchRange = wb2.Sheets(1).Range("$Q$7:$Q$" & lastRow2)
wb1.Sheets(1).Activate
ActiveSheet.Cells(tempCount + 1, 12).Value = Application.WorksheetFuction.SumIf(lookForRange, lookFor, srchRange)
On Error Resume Next
Next
Below is the entire code for reference.
'Define workbooks
Dim wb2FileName As Variant
Dim wb3FileName As Variant
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim wb3 As Workbook
'Count last rows in columns
Dim lastRow1 As Long
Dim lastRow2 As Long
Dim lastRow3 As Long
'Variables
Dim lookFor As Range
Dim lookForRange As Range
Dim srchRange As Range
Dim tempCount As Integer
Dim lastCount As Integer
'Open up all workbooks to work on
Set wb1 = ThisWorkbook
wb2FileName = Application.GetOpenFilename(FileFilter:="Microsoft Excel Workbooks (*.csv;*.xls;*.xlsx;*.xlsm),*.csv;*.xls;*.xlsx;*.xlsm", Title:="Customer Order Data Worksheet", MultiSelect:=False)
If wb2FileName <> False Then
Workbooks.Open Filename:=wb2FileName
End If
Set wb2 = Workbooks.Open(wb2FileName)
wb3FileName = Application.GetOpenFilename(FileFilter:="Microsoft Excel Workbooks (*.csv;*.xls;*.xlsx;*.xlsm),*.csv;*.xls;*.xlsx;*.xlsm", Title:="Source Reference File (Weights)", MultiSelect:=False)
If wb3FileName <> False Then
Workbooks.Open Filename:=wb3FileName
End If
Set wb3 = Workbooks.Open(wb3FileName)
'Find the last row in the customer data workbook and the source weights workbook
wb2.Sheets(1).Activate
lastRow2 = ActiveSheet.Cells(Rows.Count, 3).End(xlUp).Row
wb3.Sheets(1).Activate
lastRow3 = ActiveSheet.Cells(Rows.Count, 3).End(xlUp).Row
'Use VLOOKUP to enter in weights from the reference sheet into the customer order data sheet, then multiply by the quantity
tempCount = 0
lastCount = lastRow2
For tempCount = 1 To lastCount
Set lookFor = wb2.Sheets(1).Cells(tempCount + 6, 10) ' value to find
Set srchRange = wb3.Sheets(1).Range("$B$2:$C$" & lastRow3) 'source
wb2.Sheets(1).Activate
ActiveSheet.Cells(tempCount + 6, 16).Value = Application.WorksheetFunction.VLookup(lookFor, srchRange, 2, False)
ActiveSheet.Cells(tempCount + 6, 17).Value = ActiveSheet.Cells(tempCount + 6, 11).Value * ActiveSheet.Cells(tempCount + 6, 16).Value
On Error Resume Next
Next
'Delete top 5 rows from the final sheet and insert new header
wb1.Sheets(1).Activate
ActiveSheet.Rows("1:5").Delete
ActiveSheet.Cells(1, 12).Value = "Weights"
'Find the last row on the final sheet
lastRow1 = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
'Enter in the weights data into the final sheet
tempCount = 0
lastCount = lastRow1
For tempCount = 1 To lastCount
Set lookFor = wb1.Sheets(1).Cells(tempCount + 1, 11) ' value to find
Set lookForRange = wb2.Sheets(1).Range("$C$2:$C$" & lastRow2) 'Range of values to lookup
Set srchRange = wb2.Sheets(1).Range("$Q$7:$Q$" & lastRow2)
wb1.Sheets(1).Activate
ActiveSheet.Cells(tempCount + 1, 12).Value = Application.WorksheetFuction.SumIf(lookForRange, lookFor, srchRange)
Next