VBA code for adding data from a sheet to 2 different sheets

Atlantis764

New Member
Joined
Jan 10, 2022
Messages
17
Office Version
  1. 2019
Platform
  1. Windows
Hi all,

I'm trying to make some changes in a VBA code and I need your help.

The idea from which I initially started with this project was the following:
I made a User form in excel (Home sheet) in which I entered some data for a number of experts at the end of each month (the fields from the User Form were exactly the dates from the top row of the Database) for each expert at the end of each month.

Every time I save the data in the User form, they are automatically written as a new row in the Database sheet, the values from the "Worked hours" column to the correct position in Database1 and the values from the "Amount" column to the correct position in Database2.

I managed to make the VBA code so that all the steps above work correctly.

After about a year of working with this file and several hundred recordings in it, I realized that it is a repetitive work at the end of each month and that this activity is very time-consuming.

Reaching this moment (I admit that I should have thought faster) I thought that I could finish the job much faster if I actually copy-paste the previous data from the Database - rows previously entered (without entering the data in the User form) and just enter the data from the columns “Month”, “Worked hours” and “Amount” manually.

Is it possible to have a VBA code that every time I am manually add a new entry to Database sheet (not with the help of the User form) automatically search and add the values from Column H "Worked hours" in the corresponding cell from Database1 sheet and the values from Column I "Amount" in the corresponding cell from Database2 sheet-cell made with the same color?

What I'm trying to achieve is to have a VBA code that works and has the same result if I use the User form (the code already works) or if I enter the data manually in the Database sheet.

Another thing I encountered while working was the following: I deleted an entire row from the Database sheet, but the amounts initially added to the Database 1 and Database2 remained there. Do you think it is possible to help me with this change in the VBA code so that when I delete an amount from one column or another (or the entire row in Database), that amount is also deleted from the related database (Worked Hours in Database1 and Amount in Database 2)?

Thank you in advance for all your help!

Database_2023.xlsm
ABCDEFGHIJK
1S.No.YearMonthDateNameActivitySub-activityWorked hoursAmountUser Name
212023January31/01/2023JohnA.1A.1.21001000Liviu Popescu11/03/2023 15:43
322023March31/03/2023AnneA.3A.3.150500Liviu Popescu11/03/2023 15:57
432023February28/02/2023MarkA.2A.2.2125750Liviu Popescu11/03/2023 16:00
522023February28/02/2023AnneA.4A.4.2
Database


Database_2023.xlsm
ABCDEFGHIJK
1NameActivitySub-activity11/2212/2201/2302/2303/2304/2305/2306/23
2JohnA.1A.1.1
3JohnA.1A.1.2100
4JohnA.2A.2.1
5JohnA.2A.2.2
6JohnA.3A.3.1
7JohnA.3A.3.2
8JohnA.4A.4.1
9JohnA.4A.4.2
10MarkA.1A.1.1
11MarkA.1A.1.2
12MarkA.2A.2.1
13MarkA.2A.2.2125
14MarkA.3A.3.1
15MarkA.3A.3.2
16MarkA.4A.4.1
17MarkA.4A.4.2
18AnneA.1A.1.1
19AnneA.1A.1.2
20AnneA.2A.2.1
21AnneA.2A.2.2
22AnneA.3A.3.150
23AnneA.3A.3.2
24AnneA.4A.4.1
25AnneA.4A.4.2
Database1


Database_2023.xlsm
ABCDEFGHIJK
1NameActivitySub-activity11/2212/2201/2302/2303/2304/2305/2306/23
2JohnA.1A.1.1
3JohnA.1A.1.21000
4JohnA.2A.2.1
5JohnA.2A.2.2
6JohnA.3A.3.1
7JohnA.3A.3.2
8JohnA.4A.4.1
9JohnA.4A.4.2
10MarkA.1A.1.1
11MarkA.1A.1.2
12MarkA.2A.2.1
13MarkA.2A.2.2750
14MarkA.3A.3.1
15MarkA.3A.3.2
16MarkA.4A.4.1
17MarkA.4A.4.2
18AnneA.1A.1.1
19AnneA.1A.1.2
20AnneA.2A.2.1
21AnneA.2A.2.2
22AnneA.3A.3.1500
23AnneA.3A.3.2
24AnneA.4A.4.1
25AnneA.4A.4.2
Database2


VBA Code:
Sub Submit_Data()

    Dim sh As Worksheet
    Dim sh1 As Worksheet
    Dim sh2 As Worksheet
    Dim iRow As Long, colno As Integer, iCol As Long, rowno As Integer
    Dim iRow1 As Long, colno1 As Integer, iCol1 As Integer, reqdRow As Integer
    Dim iRow2 As Long, colno2 As Integer, iCol2 As Integer, reqdRow1 As Integer
    Set sh = ThisWorkbook.Sheets("Database")
    Set sh1 = ThisWorkbook.Sheets("Database1")
    Set sh2 = ThisWorkbook.Sheets("Database2")
    iRow = [Counta(Database!A:A)] + 1
    iCol = Sheets("Database").Cells(1, Columns.Count).End(xlToLeft).Column - 1
    iRow1 = [Counta(Database1!A:A)] + 1
    iCol1 = Sheets("Database1").Cells(1, Columns.Count).End(xlToLeft).Column - 1
    iRow2 = [Counta(Database2!A:A)] + 1
    iCol2 = Sheets("Database2").Cells(1, Columns.Count).End(xlToLeft).Column - 1
    
    Application.ScreenUpdating = False
    With sh
        .Cells(iRow, 1) = iRow - 1
        .Cells(iRow, 2) = UserForm1.CmbYear.Value
        .Cells(iRow, 3) = UserForm1.CmbMonth.Value
        .Cells(iRow, 4) = UserForm1.txtDate.Value
        .Cells(iRow, 5) = UserForm1.CmbName.Value
        .Cells(iRow, 6) = UserForm1.CmbActivity.Value
        .Cells(iRow, 7) = UserForm1.CmbSubActivity.Value
        .Cells(iRow, 8) = UserForm1.TxtHours.Value
        .Cells(iRow, 9) = UserForm1.txtAmount.Value
        .Cells(iRow, 10) = Application.UserName
        .Cells(iRow, 11) = Format([Now()], "DD-MMM-YYYY HH:MM:SS")
    End With

    With sh1
        For rowno = 2 To iRow1
            If .Cells(rowno, 1) = UserForm1.CmbName.Value And .Cells(rowno, 2) = UserForm1.CmbActivity.Value And .Cells(rowno, 3) = UserForm1.CmbSubActivity.Value Then
                reqdRow = rowno
                Exit For
            End If
        Next
        For colno = 4 To iCol1
            If UserForm1.CmbMonth.Value = Format(.Cells(1, colno), "MMMM") And _
            UserForm1.CmbYear.Value = Format(.Cells(1, colno), "YYYY") Then
            .Cells(reqdRow, colno) = UserForm1.TxtHours.Value
            End If
        Next
        .Cells(iRow, iCol1 + 3) = Application.UserName
    End With
    
    With sh2
        For rowno = 2 To iRow1
            If .Cells(rowno, 1) = UserForm1.CmbName.Value And .Cells(rowno, 2) = UserForm1.CmbActivity.Value And .Cells(rowno, 3) = UserForm1.CmbSubActivity.Value Then
                reqdRow = rowno
                Exit For
            End If
        Next
        For colno = 4 To iCol1
            If UserForm1.CmbMonth.Value = Format(.Cells(1, colno), "MMMM") And _
            UserForm1.CmbYear.Value = Format(.Cells(1, colno), "YYYY") Then
            .Cells(reqdRow, colno) = UserForm1.txtAmount.Value
            End If
        Next
        .Cells(iRow, iCol2 + 3) = Application.UserName
    End With
 
    Application.ScreenUpdating = True
 
    MsgBox "Date incarcate cu succes!"

End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your Database sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Enter a value in column H or column I and press the ENTER key.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Intersect(Target, Range("H:I")) Is Nothing Then Exit Sub
    Dim vDB As Variant, vDB1 As Variant, vDB2 As Variant, vDate1 As Variant, vDate2 As Variant
    Dim col As Long, lCol1 As Long, lcol2 As Long, v As Variant, i As Long, ii As Long, val1 As String, val2 As String
    Application.ScreenUpdating = False
    With Sheets("Database1")
        lCol1 = .Cells(1, .Columns.Count).End(xlToLeft).Column
        vDate1 = .Range("D1").Resize(, lCol1 - 3).Value
        vDB1 = .Range("A2", .Range("A" & .Rows.Count).End(xlUp)).Resize(, 3).Value
    End With
    With Sheets("Database2")
        lcol2 = .Cells(1, .Columns.Count).End(xlToLeft).Column
        vDate2 = .Range("D1").Resize(, lcol2 - 3).Value
        vDB2 = .Range("A2", .Range("A" & .Rows.Count).End(xlUp)).Resize(, 3).Value
    End With
    Select Case Target.Column
        Case Is = 8
            vDB = Range("B" & Target.Row).Resize(, 6).Value
            val1 = vDB(1, 2) & "|" & vDB(1, 1)
            For Each v In vDate1
                i = i + 1
                val2 = MonthName(Month(CDate(v))) & "|" & Year(CDate(v))
                If val1 = val2 Then
                    col = i + 3
                    Exit For
                End If
            Next v
            val1 = vDB(1, 4) & "|" & vDB(1, 6)
            For ii = LBound(vDB1) To UBound(vDB1)
                val2 = vDB1(ii, 1) & "|" & vDB1(ii, 3)
                If val1 = val2 Then
                    Sheets("Database1").Cells(ii + 1, col) = Target.Value
                    Exit For
                End If
            Next ii
        Case Is = 9
            vDB = Range("B" & Target.Row).Resize(, 6).Value
            val1 = vDB(1, 2) & "|" & vDB(1, 1)
            For Each v In vDate2
                i = i + 1
                val2 = MonthName(Month(CDate(v))) & "|" & Year(CDate(v))
                If val1 = val2 Then
                    col = i + 3
                    Exit For
                End If
            Next v
            val1 = vDB(1, 4) & "|" & vDB(1, 6)
            For ii = LBound(vDB2) To UBound(vDB2)
                val2 = vDB2(ii, 1) & "|" & vDB2(ii, 3)
                If val1 = val2 Then
                    Sheets("Database2").Cells(ii + 1, col) = Target.Value
                    Exit For
                End If
            Next ii
    End Select
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi Mumps,

Thank you for all your support!
The VBA code works great!

Best regards,
Liviu
 
Upvote 0
You are very welcome. :)
Hi Mumps,

I'm sorry to bother you again, but it seems that I need your help again.
I made some changes in Database1 and Database2 and I would be interested if you could help me (I tried but I can't manage it) with modifying the VBA code so that it only runs in a certain area of the two sheets (in this case, to run only in the range P26:W49 - from row 26 and from to column 16).
I try to give an example as detailed as possible:
- for the entry no 7 from Database sheet I have 2023, March, John, A.3, A.3.2 - the value from H8 cell must be added in cell T31 from Database1 (with yellow in both sheets) - 2023, March, John, A.3, A.3.2 is also in cell L7 but there I will add the number of allocated hours.
- for the entry no 7 from Database sheet I have 2023, March, John, A.3, A.3.2 - the value from I8 cell must be added in cell T31 from Database2 (with green in both sheets) - 2023, March, John, A.3, A.3.2 is also in cell L7 but there I will add the number of allocated budget.
Thanks again for your help!
Database_2023_modified.xlsm
ABCDEFGHIJK
1S.No.YearMonthDateNameActivitySub-activityWorked hoursAmountUser Name
212023January31/01/2023JohnA.1A.1.21001000Liviu Popescu11/03/2023 15:43
322023March31/03/2023AnneA.3A.3.150500Liviu Popescu11/03/2023 15:57
432023February28/02/2023MarkA.2A.2.2125750Liviu Popescu11/03/2023 16:00
522023February28/02/2023AnneA.4A.4.2200850
652022December31/12/2022MarkA.1A.1.275650Liviu Popescu11/03/2023 23:34
762023April30/04/2023AnneA.2A.2.15005000
872023March31/03/2023JohnA.3A.3.2
Database

Database_2023_modified.xlsm
ABCDEFGHIJKLMNOPQRSTUVW
1NameActivitySub-activityAdditional field 1Additional field 2Additional field 3Additional field 4Bgt 01/11/2022Bgt 01/12/2022Bgt 01/01/2023Bgt 01/02/2023Bgt 01/03/2023Bgt 01/04/2023Bgt 01/05/2023Bgt 01/06/202311/2212/2201/2302/2303/2304/2305/2306/23
2JohnA.1A.1.1
3JohnA.1A.1.2120
4JohnA.2A.2.1
5JohnA.2A.2.2
6JohnA.3A.3.1
7JohnA.3A.3.2
8JohnA.4A.4.1
9JohnA.4A.4.2
10MarkA.1A.1.1
11MarkA.1A.1.280
12MarkA.2A.2.1
13MarkA.2A.2.2125
14MarkA.3A.3.1
15MarkA.3A.3.2
16MarkA.4A.4.1
17MarkA.4A.4.2
18AnneA.1A.1.1
19AnneA.1A.1.2
20AnneA.2A.2.1525
21AnneA.2A.2.2
22AnneA.3A.3.1100
23AnneA.3A.3.2
24AnneA.4A.4.1
25AnneA.4A.4.2220
26JohnA.1A.1.1
27JohnA.1A.1.2100
28JohnA.2A.2.1
29JohnA.2A.2.2
30JohnA.3A.3.1
31JohnA.3A.3.2
32JohnA.4A.4.1
33JohnA.4A.4.2
34MarkA.1A.1.1
35MarkA.1A.1.275
36MarkA.2A.2.1
37MarkA.2A.2.2125
38MarkA.3A.3.1
39MarkA.3A.3.2
40MarkA.4A.4.1
41MarkA.4A.4.2
42AnneA.1A.1.1
43AnneA.1A.1.2
44AnneA.2A.2.1500
45AnneA.2A.2.2
46AnneA.3A.3.150
47AnneA.3A.3.2
48AnneA.4A.4.1
49AnneA.4A.4.2200
Database1

Database_2023_modified.xlsm
ABCDEFGHIJKLMNOPQRSTUVW
1NameActivitySub-activityAdditional field 1Additional field 2Additional field 3Additional field 4Bgt 01/11/2022Bgt 01/12/2022Bgt 01/01/2023Bgt 01/02/2023Bgt 01/03/2023Bgt 01/04/2023Bgt 01/05/2023Bgt 01/06/202311/2212/2201/2302/2303/2304/2305/2306/23
2JohnA.1A.1.1
3JohnA.1A.1.21100
4JohnA.2A.2.1
5JohnA.2A.2.2
6JohnA.3A.3.1
7JohnA.3A.3.2
8JohnA.4A.4.1
9JohnA.4A.4.2
10MarkA.1A.1.1
11MarkA.1A.1.2700
12MarkA.2A.2.1
13MarkA.2A.2.2800
14MarkA.3A.3.1
15MarkA.3A.3.2
16MarkA.4A.4.1
17MarkA.4A.4.2
18AnneA.1A.1.1
19AnneA.1A.1.2
20AnneA.2A.2.15000
21AnneA.2A.2.2
22AnneA.3A.3.1525
23AnneA.3A.3.2
24AnneA.4A.4.1
25AnneA.4A.4.21000
26JohnA.1A.1.1
27JohnA.1A.1.21000
28JohnA.2A.2.1
29JohnA.2A.2.2
30JohnA.3A.3.1
31JohnA.3A.3.2
32JohnA.4A.4.1
33JohnA.4A.4.2
34MarkA.1A.1.1
35MarkA.1A.1.2650
36MarkA.2A.2.1
37MarkA.2A.2.2750
38MarkA.3A.3.1
39MarkA.3A.3.2
40MarkA.4A.4.1
41MarkA.4A.4.2
42AnneA.1A.1.1
43AnneA.1A.1.2
44AnneA.2A.2.15000
45AnneA.2A.2.2
46AnneA.3A.3.1500
47AnneA.3A.3.2
48AnneA.4A.4.1
49AnneA.4A.4.2850
Database2
 
Upvote 0
Try:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Intersect(Target, Range("H:I")) Is Nothing Then Exit Sub
    Dim vDB As Variant, vDB1 As Variant, vDB2 As Variant, vDate1 As Variant, vDate2 As Variant
    Dim col As Long, lCol1 As Long, lcol2 As Long, v As Variant, i As Long, ii As Long, val1 As String, val2 As String
    Application.ScreenUpdating = False
    With Sheets("Database1")
        lCol1 = .Cells(1, .Columns.Count).End(xlToLeft).Column
        vDate1 = .Range("P1").Resize(, lCol1 - 15).Value
        vDB1 = .Range("A26", .Range("A" & .Rows.Count).End(xlUp)).Resize(, 3).Value
    End With
    With Sheets("Database2")
        lcol2 = .Cells(1, .Columns.Count).End(xlToLeft).Column
        vDate2 = .Range("P1").Resize(, lcol2 - 15).Value
        vDB2 = .Range("A26", .Range("A" & .Rows.Count).End(xlUp)).Resize(, 3).Value
    End With
    Select Case Target.Column
        Case Is = 8
            vDB = Range("B" & Target.Row).Resize(, 6).Value
            val1 = vDB(1, 2) & "|" & vDB(1, 1)
            For Each v In vDate1
                i = i + 1
                val2 = MonthName(Month(CDate(v))) & "|" & Year(CDate(v))
                If val1 = val2 Then
                    col = i + 15
                    Exit For
                End If
            Next v
            val1 = vDB(1, 4) & "|" & vDB(1, 6)
            For ii = LBound(vDB1) To UBound(vDB1)
                val2 = vDB1(ii, 1) & "|" & vDB1(ii, 3)
                If val1 = val2 Then
                    Sheets("Database1").Cells(ii + 25, col) = Target.Value
                    Exit For
                End If
            Next ii
        Case Is = 9
            vDB = Range("B" & Target.Row).Resize(, 6).Value
            val1 = vDB(1, 2) & "|" & vDB(1, 1)
            For Each v In vDate2
                i = i + 1
                val2 = MonthName(Month(CDate(v))) & "|" & Year(CDate(v))
                If val1 = val2 Then
                    col = i + 15
                    Exit For
                End If
            Next v
            val1 = vDB(1, 4) & "|" & vDB(1, 6)
            For ii = LBound(vDB2) To UBound(vDB2)
                val2 = vDB2(ii, 1) & "|" & vDB2(ii, 3)
                If val1 = val2 Then
                    Sheets("Database2").Cells(ii + 25, col) = Target.Value
                    Exit For
                End If
            Next ii
    End Select
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Try:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Intersect(Target, Range("H:I")) Is Nothing Then Exit Sub
    Dim vDB As Variant, vDB1 As Variant, vDB2 As Variant, vDate1 As Variant, vDate2 As Variant
    Dim col As Long, lCol1 As Long, lcol2 As Long, v As Variant, i As Long, ii As Long, val1 As String, val2 As String
    Application.ScreenUpdating = False
    With Sheets("Database1")
        lCol1 = .Cells(1, .Columns.Count).End(xlToLeft).Column
        vDate1 = .Range("P1").Resize(, lCol1 - 15).Value
        vDB1 = .Range("A26", .Range("A" & .Rows.Count).End(xlUp)).Resize(, 3).Value
    End With
    With Sheets("Database2")
        lcol2 = .Cells(1, .Columns.Count).End(xlToLeft).Column
        vDate2 = .Range("P1").Resize(, lcol2 - 15).Value
        vDB2 = .Range("A26", .Range("A" & .Rows.Count).End(xlUp)).Resize(, 3).Value
    End With
    Select Case Target.Column
        Case Is = 8
            vDB = Range("B" & Target.Row).Resize(, 6).Value
            val1 = vDB(1, 2) & "|" & vDB(1, 1)
            For Each v In vDate1
                i = i + 1
                val2 = MonthName(Month(CDate(v))) & "|" & Year(CDate(v))
                If val1 = val2 Then
                    col = i + 15
                    Exit For
                End If
            Next v
            val1 = vDB(1, 4) & "|" & vDB(1, 6)
            For ii = LBound(vDB1) To UBound(vDB1)
                val2 = vDB1(ii, 1) & "|" & vDB1(ii, 3)
                If val1 = val2 Then
                    Sheets("Database1").Cells(ii + 25, col) = Target.Value
                    Exit For
                End If
            Next ii
        Case Is = 9
            vDB = Range("B" & Target.Row).Resize(, 6).Value
            val1 = vDB(1, 2) & "|" & vDB(1, 1)
            For Each v In vDate2
                i = i + 1
                val2 = MonthName(Month(CDate(v))) & "|" & Year(CDate(v))
                If val1 = val2 Then
                    col = i + 15
                    Exit For
                End If
            Next v
            val1 = vDB(1, 4) & "|" & vDB(1, 6)
            For ii = LBound(vDB2) To UBound(vDB2)
                val2 = vDB2(ii, 1) & "|" & vDB2(ii, 3)
                If val1 = val2 Then
                    Sheets("Database2").Cells(ii + 25, col) = Target.Value
                    Exit For
                End If
            Next ii
    End Select
    Application.ScreenUpdating = True
End Sub
Hi Mumps,

Thank you again for all your help and support!
This VBA code also works great!
I will look at the VBA code sent by you and try to see what changes I will have to make in the future if I need to change the target range (I am sure that will have to add more rows and columns).

Best regards,
Liviu
 
Upvote 0
Hi again Mumps,

a new day and a new problem that I ran into and I can't solve it, again.
I modified the work file and the VBA code sent by you (as far as I could see, I modified it well according to the changes in the excel file-at least I hope I did it well).
I added in the Database a new column - column H "Net/Gross" where I would like to select if the values are for Net or Gross.
Consequently, this column was added to the other 2 worksheets (Database1 for hours and Database2 for amount). So now I have for each expert, in addition to activity and sub-activity, this division between net and gross.
I manually entered record no 8 in the Database and the amounts were added to the corresponding cells in Database1 and Database2 for the "Net" row (with yellow) - the amounts were 300 and 3000.
When I manually entered the next record (the same data, the only difference being gross instead of net), instead of putting the values in the blue cells (for the "Gross" row), it replaced the amounts from the previous record and put the values in the yellow cells.
Can you help me with a solution for this problem as well?

Many thanks!

Database_2023_15.03.2023.xlsm
ABCDEFGHIJKLMN
1S.No.YearMonthDateNameAdditional field 1Additional field 2Net/GrossActivitySub-activityWorked hoursAmountUser Name
212022December30-Dec-2022JohnaasssNetA.2A.2.21001000Liviu Popescu14/03/2023 15:56
322022December30-Dec-2022JohndddfffGrossA.2A.2.250500Liviu Popescu14/03/2023 15:57
432023January31/01/2023AnnedddtttGrossA.3A.3.21251250Liviu Popescu14/03/2023 16:05
542023January31-Jan-2023AnneddffggggNetA.3A.3.22002000
652023February28-Feb-2023MarkggghhhGrossA.4A.4.21501500Liviu Popescu15/03/2023 10:00
762023February28/02/2023MarkssfrfNetA.4A.4.278780Liviu Popescu15/03/2023 10:02
872023May31-May-2023AnnefffdddNetA.2A.2.11501500Liviu Popescu15/03/2023 11:09
982023April30-Apr-2023JohnfffffffffNetA.4A.4.13003000
1092023April30-Apr-2023JohnfffffffffGrossA.4A.4.12002000
Database


Database_2023_15.03.2023.xlsm
ABCDEFGHIJKLMNOPQRSTUVW
1NameActivitySub-activityNet/GrossAdditional field 1Additional field 2Additional field 4Bgt 01/11/2022Bgt 01/12/2022Bgt 01/01/2023Bgt 01/02/2023Bgt 01/03/2023Bgt 01/04/2023Bgt 01/05/2023Bgt 01/06/202311/2212/2201/2302/2303/2304/2305/2306/23
2JohnA.1A.1.1Net
3JohnA.1A.1.1Gross
4JohnA.1A.1.2Net
5JohnA.1A.1.2Gross
6JohnA.2A.2.1Net
7JohnA.2A.2.1Gross
8JohnA.2A.2.2Net
9JohnA.2A.2.2Gross
10JohnA.3A.3.1Net
11JohnA.3A.3.1Gross
12JohnA.3A.3.2Net
13JohnA.3A.3.2Gross
14JohnA.4A.4.1Net
15JohnA.4A.4.1Gross
16JohnA.4A.4.2Net
17JohnA.4A.4.2Gross
18MarkA.1A.1.1Net
19MarkA.1A.1.1Gross
20MarkA.1A.1.2Net
21MarkA.1A.1.2Gross
22MarkA.2A.2.1Net
23MarkA.2A.2.1Gross
24MarkA.2A.2.2Net
25MarkA.2A.2.2Gross
26MarkA.3A.3.1Net
27MarkA.3A.3.1Gross
28MarkA.3A.3.2Net
29MarkA.3A.3.2Gross
30MarkA.4A.4.1Net
31MarkA.4A.4.1Gross
32MarkA.4A.4.2Net
33MarkA.4A.4.2Gross
34AnneA.1A.1.1Net
35AnneA.1A.1.1Gross
36AnneA.1A.1.2Net
37AnneA.1A.1.2Gross
38AnneA.2A.2.1Net
39AnneA.2A.2.1Gross
40AnneA.2A.2.2Net
41AnneA.2A.2.2Gross
42AnneA.3A.3.1Net
43AnneA.3A.3.1Gross
44AnneA.3A.3.2Net
45AnneA.3A.3.2Gross
46AnneA.4A.4.1Net
47AnneA.4A.4.1Gross
48AnneA.4A.4.2Net
49AnneA.4A.4.2Gross
50JohnA.1A.1.1Net
51JohnA.1A.1.1Gross
52JohnA.1A.1.2Net
53JohnA.1A.1.2Gross
54JohnA.2A.2.1Net
55JohnA.2A.2.1Gross
56JohnA.2A.2.2Net100
57JohnA.2A.2.2Gross50
58JohnA.3A.3.1Net
59JohnA.3A.3.1Gross
60JohnA.3A.3.2Net
61JohnA.3A.3.2Gross
62JohnA.4A.4.1Net200
63JohnA.4A.4.1Gross
64JohnA.4A.4.2Net
65JohnA.4A.4.2Gross
66MarkA.1A.1.1Net
67MarkA.1A.1.1Gross
68MarkA.1A.1.2Net
69MarkA.1A.1.2Gross
70MarkA.2A.2.1Net
71MarkA.2A.2.1Gross
72MarkA.2A.2.2Net
73MarkA.2A.2.2Gross
74MarkA.3A.3.1Net
75MarkA.3A.3.1Gross
76MarkA.3A.3.2Net
77MarkA.3A.3.2Gross
78MarkA.4A.4.1Net
79MarkA.4A.4.1Gross
80MarkA.4A.4.2Net78
81MarkA.4A.4.2Gross150
82AnneA.1A.1.1Net
83AnneA.1A.1.1Gross
84AnneA.1A.1.2Net
85AnneA.1A.1.2Gross
86AnneA.2A.2.1Net150
87AnneA.2A.2.1Gross
88AnneA.2A.2.2Net
89AnneA.2A.2.2Gross
90AnneA.3A.3.1Net
91AnneA.3A.3.1Gross
92AnneA.3A.3.2Net200
93AnneA.3A.3.2Gross125
94AnneA.4A.4.1Net
95AnneA.4A.4.1Gross
96AnneA.4A.4.2Net
97AnneA.4A.4.2Gross
Database1


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Intersect(Target, Range("K:L")) Is Nothing Then Exit Sub
    Dim vDB As Variant, vDB1 As Variant, vDB2 As Variant, vDate1 As Variant, vDate2 As Variant
    Dim col As Long, lCol1 As Long, lcol2 As Long, v As Variant, i As Long, ii As Long, val1 As String, val2 As String
    Application.ScreenUpdating = False
    With Sheets("Database1")
        lCol1 = .Cells(1, .Columns.Count).End(xlToLeft).Column
        vDate1 = .Range("P1").Resize(, lCol1 - 15).Value
        vDB1 = .Range("A50", .Range("A" & .Rows.Count).End(xlUp)).Resize(, 3).Value
    End With
    With Sheets("Database2")
        lcol2 = .Cells(1, .Columns.Count).End(xlToLeft).Column
        vDate2 = .Range("P1").Resize(, lcol2 - 15).Value
        vDB2 = .Range("A50", .Range("A" & .Rows.Count).End(xlUp)).Resize(, 3).Value
    End With
    Select Case Target.Column
        Case Is = 11
            vDB = Range("B" & Target.Row).Resize(, 9).Value
            val1 = vDB(1, 2) & "|" & vDB(1, 1)
            For Each v In vDate1
                i = i + 1
                val2 = MonthName(Month(CDate(v))) & "|" & Year(CDate(v))
                If val1 = val2 Then
                    col = i + 15
                    Exit For
                End If
            Next v
            val1 = vDB(1, 4) & "|" & vDB(1, 9)
            For ii = LBound(vDB1) To UBound(vDB1)
                val2 = vDB1(ii, 1) & "|" & vDB1(ii, 3)
                If val1 = val2 Then
                    Sheets("Database1").Cells(ii + 49, col) = Target.Value
                    Exit For
                End If
            Next ii
        Case Is = 12
            vDB = Range("B" & Target.Row).Resize(, 9).Value
            val1 = vDB(1, 2) & "|" & vDB(1, 1)
            For Each v In vDate2
                i = i + 1
                val2 = MonthName(Month(CDate(v))) & "|" & Year(CDate(v))
                If val1 = val2 Then
                    col = i + 15
                    Exit For
                End If
            Next v
            val1 = vDB(1, 4) & "|" & vDB(1, 9)
            For ii = LBound(vDB2) To UBound(vDB2)
                val2 = vDB2(ii, 1) & "|" & vDB2(ii, 3)
                If val1 = val2 Then
                    Sheets("Database2").Cells(ii + 49, col) = Target.Value
                    Exit For
                End If
            Next ii
    End Select
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Intersect(Target, Range("K:L")) Is Nothing Then Exit Sub
    Dim vDB As Variant, vDB1 As Variant, vDB2 As Variant, vDate1 As Variant, vDate2 As Variant
    Dim col As Long, lCol1 As Long, lcol2 As Long, v As Variant, i As Long, ii As Long, val1 As String, val2 As String
    Application.ScreenUpdating = False
    With Sheets("Database1")
        lCol1 = .Cells(1, .Columns.Count).End(xlToLeft).Column
        vDate1 = .Range("P1").Resize(, lCol1 - 15).Value
        vDB1 = .Range("A50", .Range("A" & .Rows.Count).End(xlUp)).Resize(, 4).Value
    End With
    With Sheets("Database2")
        lcol2 = .Cells(1, .Columns.Count).End(xlToLeft).Column
        vDate2 = .Range("P1").Resize(, lcol2 - 15).Value
        vDB2 = .Range("A50", .Range("A" & .Rows.Count).End(xlUp)).Resize(, 4).Value
    End With
    Select Case Target.Column
        Case Is = 11
            vDB = Range("B" & Target.Row).Resize(, 9).Value
            val1 = vDB(1, 2) & "|" & vDB(1, 1)
            For Each v In vDate1
                i = i + 1
                val2 = MonthName(Month(CDate(v))) & "|" & Year(CDate(v))
                If val1 = val2 Then
                    col = i + 15
                    Exit For
                End If
            Next v
            val1 = vDB(1, 4) & "|" & vDB(1, 7) & vDB(1, 9)
            For ii = LBound(vDB1) To UBound(vDB1)
                val2 = vDB1(ii, 1) & "|" & vDB1(ii, 4) & vDB1(ii, 3)
                If val1 = val2 Then
                    Sheets("Database1").Cells(ii + 49, col) = Target.Value
                    Exit For
                End If
            Next ii
        Case Is = 12
            vDB = Range("B" & Target.Row).Resize(, 9).Value
            val1 = vDB(1, 2) & "|" & vDB(1, 1)
            For Each v In vDate2
                i = i + 1
                val2 = MonthName(Month(CDate(v))) & "|" & Year(CDate(v))
                If val1 = val2 Then
                    col = i + 15
                    Exit For
                End If
            Next v
            val1 = vDB(1, 4) & "|" & vDB(1, 7) & vDB(1, 9)
            For ii = LBound(vDB2) To UBound(vDB2)
                val2 = vDB2(ii, 1) & "|" & vDB2(ii, 4) & vDB1(ii, 3)
                If val1 = val2 Then
                    Sheets("Database2").Cells(ii + 49, col) = Target.Value
                    Exit For
                End If
            Next ii
    End Select
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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