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]
 

Some videos you may like

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
40,849
Office Version
365
Platform
Windows
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
 

Try_Hard

New Member
Joined
Jul 1, 2019
Messages
3
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?
 

Try_Hard

New Member
Joined
Jul 1, 2019
Messages
3
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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
40,849
Office Version
365
Platform
Windows
Glad you sorted it & thanks for the feedback
 

Watch MrExcel Video

Forum statistics

Threads
1,099,378
Messages
5,468,265
Members
406,576
Latest member
mbehring

This Week's Hot Topics

Top