Results 1 to 2 of 2

VBA: Creat items and add to a database.

Threaded View

  1. #1
    Registered User
    Join Date
    07-25-2023
    Location
    Den Haag
    MS-Off Ver
    365
    Posts
    1

    VBA: Creat items and add to a database.

    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
    Last edited by AliGW; 07-25-2023 at 09:46 AM. Reason: Code tags added. Please read the forum rules.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 4
    Last Post: 07-22-2014, 10:17 PM
  2. how do i creat a database to track stock
    By okpoch in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-22-2006, 09:50 AM
  3. multiple items in database
    By Peter in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 03:05 AM
  4. multiple items in database
    By Peter in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 02:05 AM
  5. [SOLVED] multiple items in database
    By Peter in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-05-2005, 11:05 PM
  6. [SOLVED] multiple items in database
    By Peter in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-15-2005, 04:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1