VBA newbie needs help

adugent

New Member
Joined
Apr 28, 2022
Messages
4
Hey y'all! VBA newbie here... I'm more familiar with R but trying to do something for work in Excel. I want to automatically have an entire row of data copied to a new sheet based off a value that is input in column D. My range is A-N and column names are in row 1. I have a master sheet and then 9 additional sheets, all labeled '1', '2', '3', etc.

What I'm trying to do is copy an entire row of data from the 'master' sheet to the respective numerical sheet based off of the number that is put in column d on the master sheet. I have a drop down in column d with labels 1-9 listed. So, for example, if 2 is entered into column d, the data on the 'master' sheet would be automatically copied to the next row available in sheet '2', and if 9 is entered into column d, the data on the 'master' sheet would be automatically copied to the next row available in sheet '9', etc. I need this code for all sheets labeled 1-9.

I do not want the rows deleted once copied to their respective sheets, but rather, want them to all stay on the master sheet.

Thank you SO much for any and all help you're able to offer! It is greatly appreciated!!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Welcome to the MrExcel forum!

Open a copy of your workbook. From your master sheet, right click on the tab on the bottom and select "View Code". Paste this code in the window that opens:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim lr as long
    
    If Intersect(Target, Range("D:D")) Is Nothing Then Exit Sub
    On Error GoTo BadName:
    
    Set newsht = Worksheets(Target.Value & "")
    lr = newsht.Cells(Rows.Count, "A").End(xlUp).Row + 1
    newsht.Cells(lr, "A").Resize(, 14).Value = Cells(Target.Row, "A").Resize(, 14).Value
    
BadName:
End Sub

Go back to Excel and try it out! This copies values but not formatting, but we can do that too if you want. Let me know how it works.
 
Upvote 0
Thank you so much, Eric! I'm getting this error when trying to run the code
1651179830396.png

Any idea what to do from here? I really appreciate your patience and willingness to help an newb.
 
Upvote 0
Welcome to the MrExcel forum!

Open a copy of your workbook. From your master sheet, right click on the tab on the bottom and select "View Code". Paste this code in the window that opens:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim lr as long
   
    If Intersect(Target, Range("D:D")) Is Nothing Then Exit Sub
    On Error GoTo BadName:
   
    Set newsht = Worksheets(Target.Value & "")
    lr = newsht.Cells(Rows.Count, "A").End(xlUp).Row + 1
    newsht.Cells(lr, "A").Resize(, 14).Value = Cells(Target.Row, "A").Resize(, 14).Value
   
BadName:
End Sub

Go back to Excel and try it out! This copies values but not formatting, but we can do that too if you want. Let me know how it works.
Thank you so much, Eric! I'm getting this error when trying to run the code
1651179830396.png


Any idea what to do from here? I really appreciate your patience and willingness to help an newb.
 
Upvote 0
It sounds like you have some stray text before the Private Sub line. Make sure there's nothing else on the sheet other than what I gave you. If you want to put a comment in, make sure it starts with a ' character. You'll know it's a comment because it changes color (usually green).
 
Upvote 0
Welcome to the MrExcel forum!

Open a copy of your workbook. From your master sheet, right click on the tab on the bottom and select "View Code". Paste this code in the window that opens:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim lr as long
   
    If Intersect(Target, Range("D:D")) Is Nothing Then Exit Sub
    On Error GoTo BadName:
   
    Set newsht = Worksheets(Target.Value & "")
    lr = newsht.Cells(Rows.Count, "A").End(xlUp).Row + 1
    newsht.Cells(lr, "A").Resize(, 14).Value = Cells(Target.Row, "A").Resize(, 14).Value
   
BadName:
End Sub

Go back to Excel and try it out! This copies values but not formatting, but we can do that too if you want. Let me know how it works.
It sounds like you have some stray text before the Private Sub line. Make sure there's nothing else on the sheet other than what I gave you. If you want to put a comment in, make sure it starts with a ' character. You'll know it's a comment because it changes color (usually green).
I deleted the code, closed out, and restarted, and unfortunately I'm still having the same issue. I'm going to play around with it and see if I can't figure out what is wrong. Thanks again!!
 
Upvote 0

Forum statistics

Threads
1,215,772
Messages
6,126,814
Members
449,339
Latest member
Cap N

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