Copy row based on data validation value in cells in specific column

Try_Hard

New Member
Joined
Jul 1, 2019
Messages
3
Hello everybody.

I am wet behind the ears with VBA/macro's in excel.
I have a spreadsheet that I use to input information, but I want to copy specific rows from that "active" sheet to other sheets based on the values in the cells of colume"b".
The rows will be copied to different sheets based on the value. At this time I get the information to be copied, but it copies the information to all sheets.
Also, I have only been able to get the macro to look at only 1 cell, and not to all the cells in the column.
I know it is long, cause for every sheet I repeat the same basic formula.
Thanks for any help.

Here is my macro so far:

Code:
[COLOR=#0000ff]Sub[/COLOR] Trail_copy()
[COLOR=#008000]'[/COLOR]
[COLOR=#008000]' Macro4 Macro[/COLOR]
[COLOR=#008000]'R1=("B5:B1000")[/COLOR]
[COLOR=#008000]
[/COLOR]
[COLOR=#008000]'If Range("R1").Value("76.0.0") Then[/COLOR]
    Sheets("76.0.0").Select
    Range("A1").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Range("A1").Select
    Sheets("Punch list").Select
    Range("A9:L9").Select
    Selection.Copy
    Sheets("76.0.0").Select
    ActiveSheet.Paste
    ActiveCell.Offset(1, 0).Range("A1").Select
[COLOR=#008000]'IF Range("B3:B500").Value (76.0.1) THEN[/COLOR]
    Sheets("76.0.1").Select
    Range("A1").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Range("A1").Select
    Sheets("Punch list").Select
    Range("A9:L9").Select
    Selection.Copy
    Sheets("76.0.1").Select
    ActiveSheet.Paste
    ActiveCell.Offset(1, 0).Range("A1").Select
[COLOR=#008000]'IF Range("B3:B500").Value (76.0.2) THEN[/COLOR]
    Sheets("76.0.2").Select
    Range("A1").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Range("A1").Select
    Sheets("Punch list").Select
    Range("A9:L9").Select
    Selection.Copy
    Sheets("76.0.2").Select
    ActiveSheet.Paste
    ActiveCell.Offset(1, 0).Range("A1").Select
[COLOR=#008000]'IF Range("B3:B500").Value (76.0.3) THEN[/COLOR]
    Sheets("76.0.3").Select
    Range("A1").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Range("A1").Select
    Sheets("Punch list").Select
    Range("A9:L9").Select
    Selection.Copy
    Sheets("76.0.3").Select
    ActiveSheet.Paste
    ActiveCell.Offset(1, 0).Range("A1").Select
[COLOR=#008000]'IF Range("B3:B500").Value (76.0.4) THEN[/COLOR]
    Sheets("76.0.4").Select
    Range("A1").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Range("A1").Select
    Sheets("Punch list").Select
    Range("A9:L9").Select
    Selection.Copy
    Sheets("76.0.4").Select
    ActiveSheet.Paste
    ActiveCell.Offset(1, 0).Range("A1").Select
[COLOR=#008000]'IF Range("B3:B500").Value (76.0.5) THEN[/COLOR]
    Sheets("76.0.5").Select
    Range("A1").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Range("A1").Select
    Sheets("Punch list").Select
    Range("A9:L9").Select
    Selection.Copy
    Sheets("76.0.5").Select
    ActiveSheet.Paste
    ActiveCell.Offset(1, 0).Range("A1").Select
[COLOR=#008000]'IF Range("B3:B500").Value (76.2.1) THEN[/COLOR]
    Sheets("76.2.1").Select
    Range("A1").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Range("A1").Select
    Sheets("Punch list").Select
    Range("A9:L9").Select
    Selection.Copy
    Sheets("76.2.1").Select
    ActiveSheet.Paste
    ActiveCell.Offset(1, 0).Range("A1").Select
[COLOR=#008000]'IF Range("B3:B500").Value (76.2.2) THEN[/COLOR]
    Sheets("76.2.2").Select
    Range("A1").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Range("A1").Select
    Sheets("Punch list").Select
    Range("A9:L9").Select
    Selection.Copy
    Sheets("76.2.2").Select
    ActiveSheet.Paste
    ActiveCell.Offset(1, 0).Range("A1").Select
[COLOR=#008000]'IF Range("B3:B500").Value (76.6) THEN[/COLOR]
    Sheets("76.6").Select
    Range("A1").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Range("A1").Select
    Sheets("Punch list").Select
    Range("A9:L9").Select
    Selection.Copy
    Sheets("76.6").Select
    ActiveSheet.Paste
    ActiveCell.Offset(1, 0).Range("A1").Select


[COLOR=#0000ff]End Sub[/COLOR]
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hi & welcome to MrExcel.
How about
Code:
Sub TryHard()
   Dim Cl As Range
   Dim ws As Worksheet
   Dim Ky As Variant
   
   Set ws = Sheets("Punch list")
   With CreateObject("scripting.dictionary")
      For Each Cl In ws.Range("B5", ws.Range("B" & Rows.Count).End(xlUp))
         .Item(Cl.Value) = Empty
      Next Cl
      For Each Ky In .Keys
         ws.Range("A4:L4").AutoFilter 2, Ky
         ws.AutoFilter.Range.Offset(1).EntireRow.Copy Sheets(Ky).Range("A" & Rows.Count).End(xlUp).Offset(1)
      Next Ky
      ws.AutoFilterMode = False
   End With
End Sub
 
Upvote 0
Hi & thank you Fluff.

I tried the code, it creates an auto filter in row 4 from column A-L, but run into a problem on the following step.
Code:
ws.AutoFilter.Range.Offset(1).EntireRow.Copy Sheets(Ky).Range("A" & Rows.Count).End(xlUp).Offset(1)
The problem reads "run time error '9': Subscript out of range"

I apologize for any misunderstandings.
How can I change the range in my code to look at column"B" and then select only the rows of the cells with specific value and then copy to sheet with the same name as the cell value?
 
Upvote 0
Sorry to "bump", think I have solved it!:biggrin:
Code:
Sub Copy_SubSystem()

    Sheets("Punch list").Select
    ActiveSheet.Range("$A$2:$L$15").AutoFilter Field:=2, Criteria1:="76.0.0"
    Range("A3:L1617").Select
    Selection.Copy
    Sheets("76.0.0").Select
    Range("A1:L1").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Range("A1").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Range("A1:L1").Select
    Sheets("Punch list").Select
    Range("A1:L1").Select
    ActiveSheet.Range("$A$2:$L$15").AutoFilter Field:=2

End Sub

Thank you Fluff, it was actually your post, which made a filter, that led me to the solution.

I have to say, that macro recorder is also a great tool. Finally managed to work with it.
 
Upvote 0
Glad you sorted it & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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