Simplifying a code

Flavien

Board Regular
Joined
Jan 8, 2023
Messages
54
Office Version
  1. 365
Platform
  1. Windows
Hello everyone!

I would like to replace the many lines of code

VBA Code:
WS1.Cells(I, "A").Copy: WS2.Cells(Lig, "A").PasteSpecial Paste:=xlPasteValues

with a range like

VBA Code:
WS1.range(I, "A:Q").Copy: WS2.range(Lig, "A:Q").PasteSpecial Paste:=xlPasteValues

would someone know how to do this please?


VBA Code:
Sub copy_paste()
    Dim Lig, I As Long
    Dim WS1, WS2 As Worksheet
    
    Set WS1 = Worksheets("Feuil1")
    Set WS2 = Worksheets("Feuil2")
    
'   appeler une FNC
    Ext = ThisWorkbook.Worksheets("Accueil").Range("Concat_Num_FNC").Value
  
    Lig = 4
    
    For I = 2 To WS1.Range("A" & Rows.Count).End(xlUp).Row
        If WS1.Cells(I, 1) = Ext Then
            WS1.Cells(I, "A").Copy: WS2.Cells(Lig, "A").PasteSpecial Paste:=xlPasteValues
            WS1.Cells(I, "B").Copy: WS2.Cells(Lig, "B").PasteSpecial Paste:=xlPasteValues
            WS1.Cells(I, "C").Copy: WS2.Cells(Lig, "C").PasteSpecial Paste:=xlPasteValues
            WS1.Cells(I, "D").Copy: WS2.Cells(Lig, "D").PasteSpecial Paste:=xlPasteValues
            WS1.Cells(I, "E").Copy: WS2.Cells(Lig, "E").PasteSpecial Paste:=xlPasteValues
            WS1.Cells(I, "F").Copy: WS2.Cells(Lig, "F").PasteSpecial Paste:=xlPasteValues
            WS1.Cells(I, "G").Copy: WS2.Cells(Lig, "G").PasteSpecial Paste:=xlPasteValues
            WS1.Cells(I, "H").Copy: WS2.Cells(Lig, "H").PasteSpecial Paste:=xlPasteValues
            WS1.Cells(I, "I").Copy: WS2.Cells(Lig, "I").PasteSpecial Paste:=xlPasteValues
            WS1.Cells(I, "J").Copy: WS2.Cells(Lig, "J").PasteSpecial Paste:=xlPasteValues
            WS1.Cells(I, "K").Copy: WS2.Cells(Lig, "K").PasteSpecial Paste:=xlPasteValues
            WS1.Cells(I, "L").Copy: WS2.Cells(Lig, "L").PasteSpecial Paste:=xlPasteValues
            WS1.Cells(I, "M").Copy: WS2.Cells(Lig, "M").PasteSpecial Paste:=xlPasteValues
            WS1.Cells(I, "N").Copy: WS2.Cells(Lig, "N").PasteSpecial Paste:=xlPasteValues
            WS1.Cells(I, "O").Copy: WS2.Cells(Lig, "O").PasteSpecial Paste:=xlPasteValues
            WS1.Cells(I, "P").Copy: WS2.Cells(Lig, "P").PasteSpecial Paste:=xlPasteValues
            WS1.Cells(I, "Q").Copy: WS2.Cells(Lig, "Q").PasteSpecial Paste:=xlPasteValues



        End If
    Next I
End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
VBA Code:
WS1.range(I, "A:Q")
is not a valid range reference.

Write it like this:
VBA Code:
WS1.Range(Cells(I, "A"), Cells(I, "Q"))
 
Upvote 0
Thank you very much Joe4.
I'm not currently at the office, so i have to wait till monday.
Have a nice weekend. See you!
 
Upvote 0
Hello Joe4,

Unfortunately the code does not execute completely, error 1004 is displayed and indicates a problem in the second part of the line
VBA Code:
WS2.Range(Cells(I, "A"), Cells(I, "Q")).PasteSpecial Paste:=xlPasteValues

I've tried different writings but I can't find the solution, any idea, please?
 
Upvote 0
@Flavien try...
VBA Code:
Range(WS2.Cells(Lig, "A"), WS2.Cells(Lig, "Q")).PasteSpecial Paste:=xlPasteValues

or you should be able to just do
VBA Code:
Range(WS1.Cells(I, "A"), WS1.Cells(I, "Q")).Copy
WS2.Cells(Lig, "A").PasteSpecial Paste:=xlPasteValues

Although it looks like your loop will overwrite the destination range as you aren't incrementing Lig on each iteration, you could replace Lig with I + 3

Also you have to explicitly declare variables in VBA
so
VBA Code:
Sub copy_paste()
    Dim Lig, I As Long
    Dim WS1, WS2 As Worksheet

should be

VBA Code:
Sub copy_paste()
    Dim Lig As Long, I As Long
    Dim WS1 As Worksheet, WS2 As Worksheet
 
Last edited:
Upvote 0
Solution
Although it looks like your loop will overwrite the destination range as you aren't incrementing Lig on each iteration, you could replace Lig with I + 3
Thank you, it works!
Yes you're right, FYI, my idea is to copy 1 row saved in a database, make changes to it and replace the old line with the new one (so, no duplicates)
 
Upvote 0

Forum statistics

Threads
1,215,077
Messages
6,122,995
Members
449,094
Latest member
masterms

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