Run code that updates databas ewhen file is saved

GerardoLopez22

New Member
Joined
Jul 23, 2018
Messages
2
So, I am fairly new to VBA and I am having a difficult time with this. I have a template that is to be modified and will be saved and create a new file for each modification (is a template for work shift information). For every different file saved, the main database needs to be updated, so every time it should insert the data in a new row.

NOTE: that only values from specific cells need to be updated on specific cells of the database.

Currently I have this code, but when I save it runs and pastes the same value for all the lines indicated in the loop. How can I make it stop after just one row and for it to know that on the next save it should continue in the following row?

Code:
Private Sub update_Database(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim lastRow As Long
    Dim wbData As Workbook
    Dim wsData As Worksheet
    Dim wsSource As Worksheet
    
    Set wsSource = ThisWorkbook.ActiveSheet
    Set wbData = Workbooks.Open("C:\Users\uia87497\Desktop\OEE Files\OEE_Machine_Shift\OEE_Calculation.xlsx")
    Set wsData = wbData.Sheets("OEE - Shift")
    
    For lastRow = 3 To 96
        With wsData
            .Cells(lastRow, 6).Value2 = wsSource.Range("H5").Value2
            .Cells(lastRow, 13).Value2 = wsSource.Range("P5").Value2
            .Cells(lastRow, 15).Value2 = wsSource.Range("D43").Value2
            .Cells(lastRow, 16).Value2 = wsSource.Range("J43").Value2
            .Cells(lastRow, 18).Value2 = wsSource.Range("S43").Value2
            .Cells(lastRow, 19).Value2 = wsSource.Range("P43").Value2
            .Cells(lastRow, 21).Value2 = wsSource.Range("V43").Value2
            .Cells(lastRow, 22).Value2 = wsSource.Range("G43").Value2
        End With
    Next lastRow
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi & welcome to MrExcel
How about
Code:
Private Sub update_Database(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim lastRow As Long
    Dim wbData As Workbook
    Dim wsData As Worksheet
    Dim wsSource As Worksheet
    
    Set wsSource = ThisWorkbook.ActiveSheet
    Set wbData = Workbooks.Open("C:\Users\uia87497\Desktop\OEE Files\OEE_Machine_Shift\OEE_Calculation.xlsx")
    Set wsData = wbData.Sheets("OEE - Shift")
    
      With wsData
         lastRow = .Range("F" & Rows.Count).End(xlUp).Offset(1).Row
         .Cells(lastRow, 6).Value2 = wsSource.Range("H5").Value2
         .Cells(lastRow, 13).Value2 = wsSource.Range("P5").Value2
         .Cells(lastRow, 15).Value2 = wsSource.Range("D43").Value2
         .Cells(lastRow, 16).Value2 = wsSource.Range("J43").Value2
         .Cells(lastRow, 18).Value2 = wsSource.Range("S43").Value2
         .Cells(lastRow, 19).Value2 = wsSource.Range("P43").Value2
         .Cells(lastRow, 21).Value2 = wsSource.Range("V43").Value2
         .Cells(lastRow, 22).Value2 = wsSource.Range("G43").Value2
      End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,191
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top