VBA | If contains then copy else skip to next
Solved
Kirilov
Posted messages
108
Status
Member
-
Kirilov Posted messages 108 Status Member -
Kirilov Posted messages 108 Status Member -
Hello,
I am working on an Excel file that I need to split across several sheets based on a criterion.
I would like to use a macro that says: If cell AD1=2 then copy/paste row 1 to sheet2 and move to the next one, AND if cell AD1=3 then copy/paste row 1 to sheet3, etc.
All of this for 4 criteria.
But I'm really hopeless at VBA and can't seem to write it.
Another possible difficulty I’ve encountered during copy/paste: some of the cells contain text that is often (frequently) longer than 255 characters, and during copy/pasting they get truncated.
Thank you very much for your help!
Best regards,
Kirilov
Configuration: Windows / Chrome 61.0.3163.100
I am working on an Excel file that I need to split across several sheets based on a criterion.
I would like to use a macro that says: If cell AD1=2 then copy/paste row 1 to sheet2 and move to the next one, AND if cell AD1=3 then copy/paste row 1 to sheet3, etc.
All of this for 4 criteria.
But I'm really hopeless at VBA and can't seem to write it.
Another possible difficulty I’ve encountered during copy/paste: some of the cells contain text that is often (frequently) longer than 255 characters, and during copy/pasting they get truncated.
Thank you very much for your help!
Best regards,
Kirilov
Configuration: Windows / Chrome 61.0.3163.100
5 answers
-
Hello,
For VBA code, you need to know the context precisely, so if you could provide us with an example of the data structure, without personal elements, along with the desired results explained clearly, it would be easier to help you.
To share your workbook, please upload it to https://www.cjoint.com/ (instructions) and post the obtained link here.
--
Always stay calm
Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away. Antoine de Saint-Exupéry -
Hello,
You will find the structure of the file here: http://www.cjoint.com/c/GJziCkcuipV
Thank you for your help,
Kirilov -
Hello,
With this macro you should be able to make your copy.Public Sub copier() Dim lig As Long With Sheets("Matrice") For lig = 2 To .Cells(Rows.Count, "AD").End(xlUp).Row If .Cells(lig, "AD") = 1 Or .Cells(lig, "AD") = 2 Then .Range("A" & lig & ":AN" & lig).Copy _ Destination:=Sheets("VAL_1").Cells(Sheets("VAL_1").Cells(Rows.Count, "A").End(xlUp).Row + 1, 1) End If Next lig End With End Sub
If you run it multiple times, the copy will always be made to the end.
--
Always zen
Perfection is attained, not when there is nothing left to add, but when there is nothing left to take away. Antoine de Saint-Exupéry -
Hello Gbinforme,
I just tested it and unfortunately, it doesn't work.
The macro only copies/pastes the last record that contains the value 1 in Cell AD, excluding all the others. That's the impression it gives me.
Kirilov-
re
it's because your column A is empty, try thisPublic Sub copy() Dim row As Long With Sheets("Matrice") For row = 2 To .Cells(Rows.Count, "AD").End(xlUp).Row If .Cells(row, "AD") = 1 Or .Cells(row, "AD") = 2 Then .Range("A" & row & ":AN" & row).Copy _ Destination:=Sheets("VAL_1").Cells(Sheets("VAL_1").Cells(Rows.Count, "AD").End(xlUp).Row + 1, 1) End If Next row End With End Sub
-
-
THANK YOU VERY MUCH.
It works wonders and is going to save me a ton of time!
Best regards,
Kirilov