Automatically transfer data from one sheet to another in the next available row with VBA code

atari

New Member
Joined
Jan 29, 2021
Messages
31
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. MacOS
I need help!! I am awesome at formulas but I am still not familiar with VBA language. I need some help building a code that will automatically move certain data to another tab once something is added into a specific cell.

In my sheet, data will be entered in columns B and C in several sections on the Budget tab. I would like to be able to have people mark "Yes" into column D to "Add to 5-Yr Budget?", then have the data in B-C automatically move over to the next available row in the corresponding section on the "5-Year" tab. Please let me know if you need to see the actual Excel sheet.

I cannot seem to be able to upload an image of the tabs I am working in. It keeps telling me the file is too big even when I save as JEPG or PNG. I wouldn't mind sending via private message if that's allowed. I would love to show the example of what I am trying to do.

Any help would be so much greatly appreciated!
 
Ok, I think I was able to post the mini-sheet into the test section, but it did not look right on my end. I will try it here as well to see if you can see it.

Budget Test.xlsm
I
28
Budget
 
Upvote 0

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Nope. Looks like you only posted one cell (I28).

That is what the Test Forum is for - to test it out and make sure that you have it working correctly before testing the "good" image to the real thread.

Note this line of the instructions:
Open your workbook and select the range that you want to include in your question, then click the Mini Sheet button in the Capture Range group.
So make sure you select that whole range you want to include before clicking the button.
 
Upvote 0
I added this in to have someone help with the coding. I copied and pasted the code that Joe4 provided me, but I know I did not do something right. Even the mini-sheet doesn't look right to me.

I need the data on the Budget tab to in cells B:C to transfer over to the 5-Year tab in the next available row within the corresponding sections only if something is entered in column D on the Budget tab.

Budget Test.xlsm
ABCDE
1
2Code / Description Budget Add to 5 Yr Budget?
3Category1
4Item 1$ 25,000Yes
5Item 2$ 5,000
6
7
8
9
10Category2
11Item 1$ 15,000
12Item 2$ 2,500
13
14
15
16
17Category3
18Item 1$ 1,000
19Item 2$ 5,000
20Item 3$ 1,200
21
22
23
24
25
Budget


Budget Test.xlsm
ABCD
1
2Code / Description Budget
3Category1
4
5
6
7
8
9
10Category2
11
12
13
14
15
16
17Category3
18
19
20
21
22
23
5-Year
 
Last edited:
Upvote 0
Got it. Duh... Ok, I added it to the Test thread called Budget Sheet Test if you wouldn't mind taking a look.
 
Upvote 0
Got it. Duh... Ok, I added it to the Test thread called Budget Sheet Test if you wouldn't mind taking a look.
Sorry, that is not quite what I meant. The "Test Here" is simply a playground for you to test out the tools and your posting skills before posting to the actual thread, so your real thread is not jumbled up with a bunch of bad posts/attempts. Once you have it figured out there, then you can use those techniques and post them here. I went ahead and moved your post here, as posts in the "Test Here" forum are only temporary and deleted after 7 days.

OK, that gives a better indication of what you are trying to do. However, the code that we will have to come up with has to be VERY SPECIFIC to your actual structure.
We need to make sure that this is actually your EXACT structure, and not some oversimplified example (otherwise the code I come up with will probably work for your sample data, but not your real data).
So we need to confirm the following things:

1. Are there only/exactly 3 categories, as shown?

2. Do where the rows/categories are found on each sheet match exactly (i.e. "Category1" found in cell A3 on BOTH sheets, "Category2" found in cell A10 on BOTH sheets, etc.)

3. Are there lines for up to EXACTLY 5 lines items under each category (no more/no less)?
 
Upvote 0
Sorry, that is not quite what I meant. The "Test Here" is simply a playground for you to test out the tools and your posting skills before posting to the actual thread, so your real thread is not jumbled up with a bunch of bad posts/attempts. Once you have it figured out there, then you can use those techniques and post them here. I went ahead and moved your post here, as posts in the "Test Here" forum are only temporary and deleted after 7 days.

OK, that gives a better indication of what you are trying to do. However, the code that we will have to come up with has to be VERY SPECIFIC to your actual structure.
We need to make sure that this is actually your EXACT structure, and not some oversimplified example (otherwise the code I come up with will probably work for your sample data, but not your real data).
So we need to confirm the following things:

1. Are there only/exactly 3 categories, as shown?

2. Do where the rows/categories are found on each sheet match exactly (i.e. "Category1" found in cell A3 on BOTH sheets, "Category2" found in cell A10 on BOTH sheets, etc.)

3. Are there lines for up to EXACTLY 5 lines items under each category (no more/no less)?
Thank you for your response. I'm sorry I have not been as clear as I could have been and didn't quite understand everything you were asking for. Yes, that was a simplified version of my sheet. I was hoping that you could build the structure of the code, then I could go in and manipulate it to meet my needs, but you're right. Let's just get it right.

There are 28 category sections. Each section has 30 rows for potential data aside from the 3rd section called Cap Exp-Clubhouse Redecorating, which has 40 rows. Section rows:
- Section 1 - 17:46
- Section 2 - 50:79
- Section 3 - 83:122
- Section 4 - 126:155
- Section 5 - 159:188
- Section 6 - 192:221
- Section 7 - 225:254
- Section 8 - 258:287
- Section 9 - 291:320
- Section 10 - 324:353
- Section 11 - 357:386
- Section 12 - 390:419
- Section 13 - 424:453
- Section 14 - 457:486
- Section 15 - 490:519
- Section 16 - 523:552
- Section 17 - 556:585
- Section 18 - 589:618
- Section 19 - 623:652
- Section 20 - 656:685
- Section 21 - 689:718
- Section 22 - 722:751
- Section 23 - 755:784
- Section 24 - 788:817
- Section 25 - 821:850
- Section 26 - 854:883
- Section 27 - 887:916
- Section 28 - 919:948

The 5-Year tab was a duplication of the Budget tab with additional columns added in so, yes the sections are identical on each tab. Again, I need the data within Columns B and D moved into the first available rows within the given section, from the Budget tab to the 5-Year tab IF there is something entered in column D on the Budget tab.

I would prefer that column D could have anything entered, like an "X" or a "Yes" but if it makes it easier we can just make it "Yes" and I will add a drop-down so that is the only potential option for data in that column. Here is a mini-sheet shot of the actual sheet:

Template_CapEx Budget Sheet_6_05.25.22.xlsm
ABCDEFGH
1
2*Drop-Down Menu for Cost Codes and Budget Mo.
3*Each category has extra rows - Right click and 'Un-Hide' if needed
4
5CAPITAL IMPROVEMENTS
6
7Total Units:(PROPERTY NAME)
8Total CapEx:$ 1,800
9Total p/Unit:$ -Budget Year:2023
10
11Code / Description Budget Add to 5 Year Budget? CommentsBudget Mo.Pictures?
12
13
14Non-Recurring Capital Expenses
15Cap Exp- Fees
1680000-1100 to 80000-1450
17180000-1300 Sal Cap Exp-General Labor$ 1,800 Yes
18
19
20
21
47Sub-Total$ 1,800
48Cap Exp- A/C-Compressors/New
4980000-1500 to 80000-1600
502
51
52
53
54
80Sub-Total$ -
81Cap Exp- Clubhouse Redecorating
8280000-1700 to 80000-2400
833
84
85
86
87
123Sub-Total$ -
124Cap Exp- Equipment/Major
12580000-2500 to 80000-3100
1264
127
Budget
Cell Formulas
RangeFormula
C8C8=C951
C9C9=IFERROR(C8/C7,0)
C47,C80C47=SUM(C17:C46)
C123C123=SUM(C83:C122)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
G9Expression=G9=""textNO
C7Expression=C7=""textNO
F126:F155Expression=AND($B126<>"",$F126="")textNO
C126:D155Expression=AND($B126<>"",$C126="")textNO
F83:F122Expression=AND($B83<>"",$F83="")textNO
C83:D122Expression=AND($B83<>"",$C83="")textNO
F50:F79Expression=AND($B50<>"",$F50="")textNO
C50:D79Expression=AND($B50<>"",$C50="")textNO
F17:F46Expression=AND($B17<>"",$F17="")textNO
C17:D46Expression=AND($B17<>"",$C17="")textNO
Cells with Data Validation
CellAllowCriteria
B17:B46List=_Fees
B50:B79List=Codes!$D$11:$D$15
B83:B122List=Codes!$D$17:$D$28
B126:B127List=Codes!$D$30:$D$57
F17:F46List=Codes!$O$2:$O$14
F126:F127List=Codes!$O$2:$O$14
F83:F122List=Codes!$O$2:$O$14
F50:F79List=Codes!$O$2:$O$14



Template_CapEx Budget Sheet_6_05.25.22.xlsm
ABCDEFGHIJKLMN
1
2*Drop-Down Menu for Cost Codes and Budget Mo.
3*Each category has extra rows - Right click and 'Un-Hide' if needed
4
55 YEAR CAPEX PLAN
6
7Total Units:0(PROPERTY NAME)
8Total CapEx:$ -
9Total p/Unit:$ -5 Year Range:2024 - 2028
10
11Code / Description Budget Useful LifeAgePreferred YearComments Recommended Year to Complete Pictures?
1220242025202620272028
13
14Non-Recurring Capital Expenses
15Cap Exp- Fees
1680000-1100 to 80000-1450
17 
18 
19 
20 
21 
47Sub-Total$ -$ -$ -$ -$ -$ -
48
49Cap Exp- A/C-Compressors/New
5080000-1500 to 80000-1600
51 
52 
53 
54 
55 
81Sub-Total$ -
82
83Cap Exp- Clubhouse Redecorating
8480000-1700 to 80000-2400
85 
86 
87 
88 
89 
125Sub-Total$ -
126
127Cap Exp- Equipment/Major
5-Year
Cell Formulas
RangeFormula
J7J7=Budget!E7
C7C7=Budget!C7
C8C8=C975
C9C9=IFERROR(C8/C7,0)
L9L9=IF(AA6=1,CONCATENATE(AA5," - ",AB5),CONCATENATE(AA6," - ",AB6))
H12H12=IF(AA6=1,AA5,AA6)
I12:L12I12=H12+1
H17:L21,H85:L89,H51:L55H17=IF($C17="","",IF($AB17=H$12,$C17,0))
H47:L47,C81,C47H47=SUM(H17:H46)
D17:D21,D85:D89,D51:D55D17=(IF($B17="","",IF(VLOOKUP($B17,Codes!$D$4:$F$474,3,0)="","N/A",VLOOKUP($B17,Codes!$D$4:$F$474,3,0))))
C125C125=SUM(C85:C124)
Named Ranges
NameRefers ToCells
_Fees=Codes!$D$4:$D$9D85:D89, D51:D55, D17:D21
'5-Year'!B_AC='5-Year'!$B$51D51
'5-Year'!B_Clubhouse='5-Year'!$B$85D85
'5-Year'!B_Fees='5-Year'!$B$17D17
Cells with Conditional Formatting
CellConditionCell FormatStop If True
C7Expression=C7=""textNO
C17:C46,C51:C80,C85:C124,C129:C158,C163:C192,C197:C226,C231:C260,C265:C294,C299:C328,C333:C362,C367:C396,C401:C430,C435:C464,C469:C498,C503:C532,C537:C566,C571:C600,C605:C634,C639:C668,C673:C702,C707:C736,C741:C770,C775:C804,C809:C838,C843:C872,C877:C906Expression=AND($B17<>"",$C17="")textNO
Cells with Data Validation
CellAllowCriteria
B17:B46List=_Fees
B85:B124List=Codes!$D$17:$D$28
B51:B80List=Codes!$D$11:$D$15
 
Upvote 0
I don't know why it's showing the cells with Formulas, Conditional Formatting and Data Validation.... I'm sure you can ignore all that. I just wanted to show you a snippet of how my sheet actually looks.
 
Upvote 0
Sorry, just re-read what I wrote above and I made a typo. When I said "Again, I need the data within Columns B and D moved into the first available rows within the given section, from the Budget tab to the 5-Year tab IF there is something entered in column D on the Budget tab." I meant that I need the data in Columns B and C copied over, not B and D.
 
Upvote 0
OK, try this code:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range
    Dim cell As Range
    Dim r1 As Long
    Dim r2 As Long
    
    Set rng = Intersect(Target, Range("D:D"))
    
'   Exit sub if no updates made in column D
    If rng Is Nothing Then Exit Sub
    
'   Check column D updated
    For Each cell In rng
'       Check to see if value set to Yes
        If UCase(cell) = "YES" Then
'           Get row to copy from
            r1 = cell.Row
'           Get row to copy to
            If Sheets("Add to 5-Yr Budget").Cells(r1 - 1, "B") <> "" Then
                r2 = r1
            Else
                r2 = Sheets("Add to 5-Yr Budget").Cells(r1, "B").End(xlUp).Row + 1
            End If
'           Copy columns B and C to next available row on "Add to 5-Yr Budget" sheet
            Range(Cells(r1, "B"), Cells(r1, "C")).Copy Sheets("Add to 5-Yr Budget").Cells(r2, "B")
        End If
    Next cell

End Sub
 
Upvote 0
Solution
On the Budget sheet, I have column D asking if they want to "Add to 5 Yr Budget" but the tab itself it just named "5-Year". I updated that in the code you posted and it's giving me the following error. Do I need to make this a module or something?:

1653586384626.png
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,428
Members
448,896
Latest member
MadMarty

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