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

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.
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)

abcdefghijklmno
1JobCabinetQTYMatThWLPart DesNoteEdgeE2E3E4PidWS
2166001001mdf.751212PLATESandWht2mm3mm4mm100-PlateC

<tbody>
</tbody>








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


abcdefghijklmn
1
2
3
4
5CabinetQTYMatThWLPart DesNoteEdgeE2E3E4PID
61001mdf.751212PlateSandWht2mm3mm4mm100-Plate
7

<tbody>
</tbody>














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,214,599
Messages
6,120,447
Members
448,966
Latest member
DannyC96

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