+ Reply to Thread
Results 1 to 7 of 7

Copy & Paste Cell Values based on conditions to separate sheets from primary sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    12-01-2017
    Location
    New Jersey
    MS-Off Ver
    2016
    Posts
    63

    Copy & Paste Cell Values based on conditions to separate sheets from primary sheet

    Hello,
    I am stuck on finishing this code. The Input sheet is "Qlik_VT11 that has job numbers in column A. There are 4 output sheets defined below in the code with each having 2 conditions on the inout sheet that determine where the job number will get copied and pasted to. The primary sheet should loop until complete based on Column A. All output sheets will get pasted into Column A with each entry placed under the last. I started code from parts I found online and added the conditions into If & IfElse statements. I'm not sure I have the code correct for looping through the primary sheet and do not have code to place each entry in the output shttes into the next row down. Need some help

    Sub Shipments()
    
    Dim LR As Long, i As Long
        With Sheets("Qlik_VT11")
            LR = .Range("A" & Rows.Count).End(xlUp).Row
            
            'erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    
            For i = 1 To LR
                With .Range("A" & i)
                    If Cells(i, 3).Value = "NW" And Cells(i, 9).Value = "Performance Plastics" Then
    
                        Sheets("Qlik_VT11").Range("A1:A1").Copy Destination:=Sheets("Performance Plastics").Range("A2:A2")
                        
                    ElseIf Cells(i, 3).Value = "NW" And Cells(i, 9).Value = "Specialty Products" Then
    
                        Sheets("Qlik_VT11").Range("A1:A1").Copy Destination:=Sheets("Specialty Products").Range("A2:A2")
                        
                    ElseIf Cells(i, 3).Value = "NW" And Cells(i, 9).Value = "Material Sciences" Then
    
                        Sheets("Qlik_VT11").Range("A1:A1").Copy Destination:=Sheets("Material Sciences").Range("A2:A2")
                        
                    ElseIf Cells(i, 3).Value = "NW" And Cells(i, 9).Value = "Corteva & Performance Materials" Then
    
                        Sheets("Qlik_VT11").Range("A1:A1").Copy Destination:=Sheets("Corteva & Perf Materials").Range("A2:A2")
                        
                    End If
                End With
            Next i
        End With
    Last edited by spyac; 07-02-2018 at 10:14 PM.

  2. #2
    Valued Forum Contributor than_gold's Avatar
    Join Date
    10-17-2017
    Location
    Coimbatore India
    MS-Off Ver
    Office 365
    Posts
    646

    Re: Copy & Paste Cell Values based on conditions to separate sheets from primary sheet

    Hello Welcome to the forum, Spyac,
    Could you please warp your code as per forum rule#3
    https://www.excelforum.com/forum-rul...rum-rules.html
    Regards,
    Thangavel D

    Appreciate the help? CLICK *

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Copy & Paste Cell Values based on conditions to separate sheets from primary sheet

    Try this after you edit your original post and wrap your code in CODE tags (it's a forum rule).

    The code below is not tested.

    Sub Shipments()
        
        Dim i As Long
        
        With Sheets("Qlik_VT11")
            For i = 1 To .Range("A" & Rows.Count).End(xlUp).Row
                If .Cells(i, 3).Value = "NW" Then
                    Select Case .Cells(i, 9).Value
                        Case "Performance Plastics", "Specialty Products", "Material Sciences"
                            .Range("A" & i).Copy Destination:=Sheets(.Cells(i, 9).Value).Range("A" & Rows.Count).End(xlUp).Offset(1)
                        Case "Corteva & Performance Materials"
                            .Range("A" & i).Copy Destination:=Sheets("Corteva & Perf Materials").Range("A" & Rows.Count).End(xlUp).Offset(1)
                    End Select
                End If
            Next i
        End With
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  4. #4
    Registered User
    Join Date
    12-01-2017
    Location
    New Jersey
    MS-Off Ver
    2016
    Posts
    63

    Re: Copy & Paste Cell Values based on conditions to separate sheets from primary sheet

    Hello, I tested the code and it works properly. While testing I found that it had stopped due to blank values in Column9. Once I removed them it ran all of the way through. Would it be easy to add another case statement to copy any items missing this value to a separate sheet and continue to loop until done. In the future if this null error existed, the macro would stop. I have never used case statements, but they do look efficient. I can create aother sheet called Shipment Review.

  5. #5
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Copy & Paste Cell Values based on conditions to separate sheets from primary sheet

    It doesn't make sense that blank cells in column 9 would cause it to fail. It should just ignore them. What exactly happens when it fails? Do you get an error? If yes, what is the error description and what line of code is highlighted when you click the Debug button on the error dialog?

    This will copy the blanks to Shipment Review

    Sub Shipments()
        
        Dim i As Long
        
        With Sheets("Qlik_VT11")
            For i = 1 To .Range("A" & Rows.Count).End(xlUp).Row
                If .Cells(i, 3).Value = "NW" Then
                    Select Case .Cells(i, 9).Value
                        Case "Performance Plastics", "Specialty Products", "Material Sciences"
                            .Range("A" & i).Copy Destination:=Sheets(.Cells(i, 9).Value).Range("A" & Rows.Count).End(xlUp).Offset(1)
                        Case "Corteva & Performance Materials"
                            .Range("A" & i).Copy Destination:=Sheets("Corteva & Perf Materials").Range("A" & Rows.Count).End(xlUp).Offset(1)
                        Case Else
                            .Range("A" & i).Copy Destination:=Sheets("Shipment Review").Range("A" & Rows.Count).End(xlUp).Offset(1)
                    End Select
                End If
            Next i
        End With
    End Sub

  6. #6
    Registered User
    Join Date
    12-01-2017
    Location
    New Jersey
    MS-Off Ver
    2016
    Posts
    63

    Re: Copy & Paste Cell Values based on conditions to separate sheets from primary sheet

    Hi AllphaFrog,

    Correction - This was not a Null, It was an #N/A from a lookup value in column 9. I changed the formula for
    the column to display errors as blank and it will remove any chance that the code will stop.

    The error displayed was
    Run-time error '13': Type mismatch
    On Line: Case "Performance Plastics", "Specialty Products", "Material Sciences".

    The additional line of code should capture these exceptons

  7. #7
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Copy & Paste Cell Values based on conditions to separate sheets from primary sheet

    Thanks for the feedback and you're welcome.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Copy Paste From Rawdata Sheets to Multiple Sheets Based on Sheet names
    By farrukh in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-16-2017, 11:01 PM
  2. [SOLVED] Copy Paste a Range of Values from a Different Sheet Based on Value Entered in Cell
    By hamidxa in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 03-13-2015, 04:53 PM
  3. Copy Values to separate sheet, locate value in list and paste 6 cells to the left
    By drknot in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-16-2014, 10:16 AM
  4. Need to select data based on conditions in a separate sheet then return the values
    By scottmcclean in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-14-2014, 03:39 PM
  5. [SOLVED] Macro to copy and paste based on criteria to 11 separate sheets
    By msmith7113 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-11-2013, 02:30 AM
  6. Copy and paste data to separate sheets based on mutiple criteria again
    By Ricker090 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 07-13-2011, 02:23 PM
  7. Copy and paste data to separate sheets based on mutiple criteria
    By Ricker090 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-21-2011, 03:48 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1