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
 
Hi Mumps,

the VBA code sent by you works great!
You saved me al lot of time and effort!
Thanks again!

Best regards,
Liviu
 
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

Forum statistics

Threads
1,214,849
Messages
6,121,925
Members
449,056
Latest member
denissimo

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