Copy and paste depending on cell criteria

michud08

New Member
Joined
Sep 30, 2022
Messages
4
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Good evening all,

I have done a bit of VBA coding in the past but not had any formal training. I have had success from adapting other code I found and lots of trial and error!

I am trying to find a bit of code that will copy data from 2 cells but only if a criteria is met in another cell. So basically, when I select "closed" from column E, I want Serial No & Part No to be copied to the other sheet. My table will have rows added to it over time so needs to be dynamic. Hopefully the screenshots will help clarify.

Any help is really appreciated. I can't seem to figure this out at all.

Data sheet:-
dynamic copy of specific cells.xlsm
ABCDEFGHIJ
1Serial NoDescriptionPart numberorderedcomplete?
2001fairingA1234YES
3002access coverB1234NOWhen closed is selected from the drop down in column E I want VBA code to copy "Serial No' & "Part No"of that line to "completed Labels" sheet in the respective boxes.
4003drainC1234YES
5004coverD1234YES
6
7
8
9
10
11
12
Data
Cells with Data Validation
CellAllowCriteria
E2:E5Listclosed, open


Completed Labels Sheet:-
dynamic copy of specific cells.xlsm
ABCDEF
1
2DELIVER TO LOGISTICS
3Serial NoI
4Part no
5
Completed Labels
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your Data sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Make a selection in column E. The macro assumes that "completed Labels" sheet has the header in row 1 and the data starts in row 2. Also, if you have any merged cells, unmerge them. You should avoid using merged cells because they almost always cause problems for macros.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range) 
    If Target.CountLarge > 1 Then Exit Sub
    If Target.Column <> 5 Then Exit Sub
    Application.ScreenUpdating = False
    If Target = "closed" Then
        With Sheets("completed Labels")
            .Cells(2, Columns.Count).End(xlToLeft).Offset(0, 1) = Target.Offset(, -4)
            .Cells(3, Columns.Count).End(xlToLeft).Offset(0, 1) = Target.Offset(, -2)
        End With
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
mumps,

This is brilliant. Thank you so much, its saved me loads of time.

I have changed the lines that reference where to paste as it wasn't working quite right. Made a small change to the following code and works perfect (in green) I just commented your lines out as I may be able to use them later. The cells on the "completed labels" sheet are static and will always be B3 & B4.

Cheers again, you saved me a headache! Hope this might be of use to someone else.

If Target = "closed" Then
With Sheets("completed Labels")
.Range("B3") = Target.Offset(, -4)
.Range("B4") = Target.Offset(, -2)

' .Cells(2, Columns.Count).End(xlToLeft).Offset(0, 1) = Target.Offset(, -4)
' .Cells(3, Columns.Count).End(xlToLeft).Offset(0, 1) = Target.Offset(, -2)
End With
 
Upvote 0

Forum statistics

Threads
1,215,014
Messages
6,122,697
Members
449,092
Latest member
snoom82

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