I am new to vba and have thrown myself in the deep end. I am using excel and am atttempting to write some code in VBA
I have two spreadsheets. The first spreadsheet has a column of ProductNames in them and they are all unique values, Variants. The second spreadsheet has 2 columns, the first column has multiple instances of ProductNames and an associated integer value in another column.
In the other worksheet, want to lookup at the values in the first column, find the exact match and then for every instance of a match, sum the value. Then I want to return the sum back to the adjacent cell in the first worksheet.
I neeed a bit of help on deciding the best way forward, i.e. loops, find, match etc.
Also, if someone can tell me how to delete or edit a posted thread that would be useful.
I have two spreadsheets. The first spreadsheet has a column of ProductNames in them and they are all unique values, Variants. The second spreadsheet has 2 columns, the first column has multiple instances of ProductNames and an associated integer value in another column.
In the other worksheet, want to lookup at the values in the first column, find the exact match and then for every instance of a match, sum the value. Then I want to return the sum back to the adjacent cell in the first worksheet.
I neeed a bit of help on deciding the best way forward, i.e. loops, find, match etc.
Code:
Sub ProductQty()
'
' This macro looks up a cell value and finds a match in another column in another workseet.
' When a match is found the corresponding value in another column in the second worksheet is stored to a variable (count)
' For every match the value is then added to the count and the value is returned to a cell adjacent to the cell value in the first worksheet.
' The macro repeats this process until it reaches the end of the of the column in the first worksheet
Dim wkbk1name As Variant
Dim wkbk2name As Variant
Dim wkbk1 As Workbook
Dim wkbk2 As Workbook
Dim ActWkbk As Workbook
Dim ProductCode As Variant
Dim GetValue As Variant
Dim RunningTotal As Long
Dim Count As Integer
Set wkbk1 = ActiveWorkbook 'Assign worksheet
wkbk2name = Application.GetOpenFilename("Excel files, *.*") 'Open the second worksheet
If wkbk2name = False Then
Exit Sub
End If
Set wkbk2 = Workbooks.Open(Filename:=wkbk2name)
ThisWorkbook.Activate 'Activate the workbook with the ProductCode (unique values)
'Loop 1 should start here
ProductCode = ActiveSheet.Range("A2").Select 'Assign the Active cell value to a variable
RunningTotal = 0 'for each pass the running total is cleared
'loop2 should start here
wkbk2.Worksheets("Sheet1").Activate
GetValue = ActiveSheet.Range("a1", ActiveSheet.Range("a1").End(xlDown)).Select 'Set the search range from A column, 2nd cell to the end of the column
If GetValue = ProductCode Then 'if a match is found then begin the running total
Count = ActiveCell.Offset(2, 0).Select
RunningTotal = RunningTotal + Count '
End If
ActiveCell.Offset(-2, -1).Select 'go back to the next cell below in the column
'when no more data in the column then return value to first worksheet and place RunningTotal into cell
'Loop 2 should end here
wkbk1.Worksheets("Sheet1").Activate
ActiveSheet.Range("B2").Select = RunningTotal
ActiveCell.Offset(-2, -1).Select 'go back 1 column and down one row to the next ProductCode
'Loop1 should end here.
End Sub
Also, if someone can tell me how to delete or edit a posted thread that would be useful.