Hello,
I am trying to set up a macro that creates a new product into our product list and the adds these all to our main database which is where we store information on our technicians certification level on those products. So I have a list of technicians, products which also includes the default training level and request of 0 & 0, and then the overall database.
I have another sheet where people can enter in the new product franchise, name and ID and this will add it to the Product List and this is working perfectly well.
But now I need to create a new row in the main database for each technician on this new product, which currently is 99 rows.
The only issue I am having at the moment is that when it is pulling the names into the database this goes well but I need it to pull the last line from the products sheet (which will be the newest one) but it always goes for the one just above it. Is there anything wrong with this code?
Sub AddToProductTableWithConditions()
Dim wsAddData As Worksheet
Dim wsProductList As Worksheet
Dim wsTechnicians As Worksheet
Dim wsDatabase As Worksheet
Dim productListTable As ListObject
Dim techTable As ListObject
Dim dbTable As ListObject
Dim newRow As ListRow
' Set references to the worksheets
Set wsAddData = ThisWorkbook.Sheets("Add Data")
Set wsProductList = ThisWorkbook.Sheets("Product List")
Set wsTechnicians = ThisWorkbook.Sheets("Technicians")
Set wsDatabase = ThisWorkbook.Sheets("Database")
' Set a reference to the ProductTable on Product List sheet
Set productListTable = wsProductList.ListObjects("ProductTable")
' Find the last row in ProductTable before adding a new row
Dim lastRow As Long
lastRow = productListTable.ListRows.Count
' Add a new row to ProductTable
Set newRow = productListTable.ListRows.Add
' Copy the values from cells H6, I6, and J6 on "Add Data" sheet to ProductTable
newRow.Range(1).Value = wsAddData.Range("H6").Value ' Column A
newRow.Range(2).Value = wsAddData.Range("I6").Value ' Column B
newRow.Range(3).Value = wsAddData.Range("J6").Value ' Column C
' Set columns D and E to have a value of "0"
newRow.Range(4).Value = 0 ' Column D
newRow.Range(5).Value = 0 ' Column E
' Check conditions and update columns D and E accordingly
If newRow.Range(1).Value = "Condition1" Then
newRow.Range(4).Value = 10
ElseIf newRow.Range(2).Value = "Condition2" Then
newRow.Range(5).Value = 20
End If
' Loop through each row in TechTable and copy values
Set techTable = wsTechnicians.ListObjects("TechTable")
Dim techRow As ListRow
' Find the last row in Database table
Set dbTable = wsDatabase.ListObjects("DataSource")
Dim dbLastRow As Long
dbLastRow = dbTable.ListRows.Count
For Each techRow In techTable.ListRows
' Add a new row to Database table
Set newRow = dbTable.ListRows.Add
' Copy columns A to C from TechTable to columns A to C in Database
newRow.Range(1).Value = techRow.Range(1).Value ' Column A
newRow.Range(2).Value = techRow.Range(2).Value ' Column B
newRow.Range(3).Value = techRow.Range(3).Value ' Column C
' Copy columns A to E from ProductTable to columns D to H in Database
newRow.Range(4).Value = wsProductList.Range("A" & lastRow + 1).Value ' Column D
newRow.Range(5).Value = wsProductList.Range("B" & lastRow + 1).Value ' Column E
newRow.Range(6).Value = wsProductList.Range("C" & lastRow + 1).Value ' Column F
newRow.Range(7).Value = wsProductList.Range("D" & lastRow + 1).Value ' Column G
newRow.Range(8).Value = wsProductList.Range("E" & lastRow + 1).Value ' Column H
Next techRow
' Display a message box for successful data addition
MsgBox "Data added successfully to Database with conditions and combined with TechTable.", vbInformation + vbOKOnly, "Data Addition Result"
End Sub
Bookmarks