Copy Rows to Different Tabs

matthewrlong

New Member
Joined
Jan 18, 2017
Messages
11
Hello Everyone!

I'm new to this forum and just hope I'm doing things correctly!

The current issue I am faced with is how to populate a row, depending on the condition typed in the end cell, to the corresponding tab.

For example:
Row 2 thru Row 5 have information in cells A-N. In cell "O2" I would like to type a letter (C, L, H) and depending on the letter typed in "O2" the row information will be moved to its corresponding sheet. The name of the sheets we have setup are CNC Ticket, Lumber Ticket, Hardware Ticket.

I am using a excel 2016
All help is appreciated!
ML
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Possibly...
Code:
Sub not_tested()
    Dim a As Integer
    Dim ws As Worksheet
    
    With Sheet1
        For a = 2 To 5
            Select Case Cells(a, 15)
                Case Is = "C"
                ws = Worksheets("CNC Ticket")
                Case Is = "L"
                ws = Worksheets("Lumber Ticket")
                Case Is = "H"
                ws = Worksheets("Hardware Ticket")
            End Select
            .Range(.Cells(a, 1), Cells(a, 14)).Copy ws.Cells(.UsedRange.Rows.Count + 1, 1)
        Next a
    End With
End Sub
 
Upvote 0
I'm not sure I have inserted the code correctly. I just simply copied pasted and changed SUB to Private Sub Populate_Click()
I hope all of this isn't confusing as I'm a basic user of Excel and a novice to forum use.

I'll try and build an example spreadsheet below since I can upload the file itself

This table represents "Sheet 1". The amount of rows my vary from 1 to 100 (just depends on the job)
I have a "Populate" button put in place to perform the VBA action (if that makes sense)

[TABLE="class: grid, width: 800, align: left"]
<tbody>[TR]
[TD][/TD]
[TD]a[/TD]
[TD]b[/TD]
[TD]c[/TD]
[TD]d[/TD]
[TD]e[/TD]
[TD]f[/TD]
[TD]g[/TD]
[TD]h[/TD]
[TD]i[/TD]
[TD]j[/TD]
[TD]k[/TD]
[TD]l[/TD]
[TD]m[/TD]
[TD]n[/TD]
[TD]o[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Job[/TD]
[TD]Cabinet[/TD]
[TD]QTY[/TD]
[TD]Mat[/TD]
[TD]Th[/TD]
[TD]W[/TD]
[TD]L[/TD]
[TD]Part Des[/TD]
[TD]Note[/TD]
[TD]Edge[/TD]
[TD]E2[/TD]
[TD]E3[/TD]
[TD]E4[/TD]
[TD]Pid[/TD]
[TD]WS[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]16600[/TD]
[TD]100[/TD]
[TD]1[/TD]
[TD]mdf[/TD]
[TD].75[/TD]
[TD]12[/TD]
[TD]12[/TD]
[TD]PLATE[/TD]
[TD]Sand[/TD]
[TD]Wht[/TD]
[TD]2mm[/TD]
[TD]3mm[/TD]
[TD]4mm[/TD]
[TD]100-Plate[/TD]
[TD]C[/TD]
[/TR]
</tbody>[/TABLE]








This table represents "CNC Ticket". The information would transfer from Sheet 1 @ B2-N2 and land in Sheet "CNC Ticket" @ B6-N6.


[TABLE="class: grid, width: 800, align: left"]
<tbody>[TR]
[TD][/TD]
[TD]a[/TD]
[TD]b[/TD]
[TD]c[/TD]
[TD]d[/TD]
[TD]e[/TD]
[TD]f[/TD]
[TD]g[/TD]
[TD]h[/TD]
[TD]i[/TD]
[TD]j[/TD]
[TD]k[/TD]
[TD]l[/TD]
[TD]m[/TD]
[TD]n[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD][/TD]
[TD]Cabinet[/TD]
[TD]QTY[/TD]
[TD]Mat[/TD]
[TD]Th[/TD]
[TD]W[/TD]
[TD]L[/TD]
[TD]Part Des[/TD]
[TD]Note[/TD]
[TD]Edge[/TD]
[TD]E2[/TD]
[TD]E3[/TD]
[TD]E4[/TD]
[TD]PID[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD][/TD]
[TD]100[/TD]
[TD]1[/TD]
[TD]mdf[/TD]
[TD].75[/TD]
[TD]12[/TD]
[TD]12[/TD]
[TD]Plate[/TD]
[TD]Sand[/TD]
[TD]Wht[/TD]
[TD]2mm[/TD]
[TD]3mm[/TD]
[TD]4mm[/TD]
[TD]100-Plate[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]














Again your help is greatly appreciated!
ML
 
Upvote 0
Try this...
Code:
Option Explicit

Sub not_tested()
    Dim a As Integer
    Dim ws As String
    
    With Worksheets("Sheet 1")
        For a = 2 To .Cells(.Rows.Count, 2).End(xlUp).Row
            Select Case .Cells(a, 15).Value
                Case Is = "C"
                    ws = "CNC Ticket"
                Case Is = "L"
                    ws = "Lumber Ticket"
                Case Is = "H"
                    ws = "Hardware Ticket"
            End Select
            .Range("B" & a & ":N" & a).Copy Worksheets(ws).Range("B" & Rows.Count).End(xlUp).Offset(1)
        Next a
    End With
End Sub
 
Upvote 0
Dangelor,

Who ever you are............. YOU ARE AWESOME!

Sorry for the delay in response. I often wish there were 3 of me!


I do have one more question for you. If you are able to get around to it I would appreciate it..

I know this is set up to be used on a per sheet basis. Is there a way to make a workbook option for this.

We have many different items which get there own sheet tabs based on the inventory number.
So lets say out inventory is 900,901,902,903,904,905. Each of those numbers gets a tab in our job workbook depending on which ones we use.

Example Job:
A request for items 900, 901, & 903 from job #16000 comes in. We would only need to create sheet tabs for those specific items.
How would you make a general or "global" VBA button?

Thank you
ML
 
Upvote 0
Is this what you wanted?
Code:
Sub again_not_tested()
    'this should cycle through all worksheets in a workbook
    
    Dim a As Integer
    Dim ws As String
    Dim sh As Worksheet
    
    For Each sh In ThisWorkbook.Worksheets
        If sh.Name <> "CNC Ticket" Then
            If sh.Name <> "Lumber Ticket" Then
                If sh.Name <> "Hardware Ticket" Then
                    With sh
                        For a = 2 To .Cells(.Rows.count, 2).End(xlUp).Row
                            Select Case .Cells(a, 15).Value
                                Case Is = "C"
                                ws = "CNC Ticket"
                                Case Is = "L"
                                ws = "Lumber Ticket"
                                Case Is = "H"
                                ws = "Hardware Ticket"
                            End Select
                            .Range("B" & a & ":N" & a).Copy Worksheets(ws).Range("B" & Rows.count).End(xlUp).Offset(1)
                        Next a
                    End With
                End If
            End If
        End If
    Next sh
End Sub
 
Upvote 0
So I am just now finally able to get back around to this project. My company reallocated me to another project for the last month.

When I run this a "Run-time error 9, subscript out of range" comes up. When I debug it highlights the following line

.Range("B" & a & ":N" & a).Copy Worksheets(ws).Range("B" & Rows.Count).End(xlUp).Offset(1)

Thanks again for your help. If I could upload the workbook I would.
 
Upvote 0
Well, there are two variables in that line of code and either or both could be the cause of the error.

Run the code again and when the error stops it, hover your mouse over the "a" and the "ws" and let me know the value of each.
 
Upvote 0
I would guess that one of the rows in one of the worksheets doesn't have a value of "C", "L" or "H" in column 15 ("O").
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,123
Members
452,381
Latest member
Nova88

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