Déplacer une ligne d'une feuille à une autre (Move a row from one sheet to another)

Jakezer

New Member
Joined
Mar 24, 2022
Messages
19
Office Version
  1. 2019
Platform
  1. Windows
Bonjour,

J'utilise ce code pour deplacer une ligne d'une feuille (Sheet1) à une autre (Sheet2)
Le code fonctionne mais il deplace la derniere ligne (la derniere dont il y'a un contenu) dans Sheet1 à la derniere ligne dans Sheet2 (qui est 49 par example)
Disant j'ai 5 lignes dans Sheet1 dont y'a un contenu, il va déplacer la 5eme ligne de Sheet1 à la derniere ligne de Sheet2
Moi je veux qu'il déplace les lignes dont la valeur de leurs cellules D est égale à 0 à Sheet2 par ordre normal commençant de A2 et en descendant
Merci d'avance


VBA Code:
Sub Deplacer()
    Dim rng As Range
    Dim x As Long
    Dim y As Long
    x = Worksheets("Sheet1").UsedRange.Rows.Count
    y = Worksheets("Sheet2").UsedRange.Rows.Count
    If y = 1 Then
       If Application.WorksheetFunction.CountA(Worksheets("Sheet2").UsedRange) = 0 Then y = 0
    End If
    Set rng = Worksheets("Sheet1").Range("A" & x)
    On Error Resume Next
    Application.ScreenUpdating = False
    For x = 1 To rng.Count
        If rng("D1").Offset(x, 0).Value = 0 Then
            rng(x).EntireRow.Cut Destination:=Worksheets("Sheet2").Range("A" & y + 1)
            y = y + 1
        End If
    Next
    Application.ScreenUpdating = True
End Sub
 
Thank you so much bro, it is working correctly, but only when am on sheet1, when i open sheet2 it stops and gives me an error: Procedure "Application.Intersect" failed
and it highlights a line in another code where I used application.intersect to test on checkboxes, any idea why is that happening please?
Did you make this change and are still having the error ?
VBA Code:
            With srcSht
                .Range(.Cells(x, "A"), .Cells(x, "D")).Cut Destination:=destSht.Range("A" & destLastRow + 1)
            End With

The code above is not calling the sub routine OneSheet which calls the function HasCheckbox which is using Intersect nor is it calling HasCheckbox directly.

Apart from the subroutine Deplacer none of your other code explicitly tells Range what sheet to use, so I think if you are not on the right sheet a lot of the code may appear to be working but is reading/writing to the wrong sheet. When it calls the HasCheckbox function though, the Intersect command errors out because in this line
If Not Application.Intersect(rng, cb.TopLeftCell) Is Nothing Then
cb is looking at Sheet1 and rng is looking at the activesheet which you have set to be Sheet2.
 
Upvote 0

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Have you considered using Autofilter to copy (and delete) all the "0" rows en masse?

VBA Code:
Option Explicit
Sub jakezer()
    Dim ws1 As Worksheet, ws2 As Worksheet, lr As Long
    Set ws1 = Sheet1
    Set ws2 = Sheet2
    lr = ws2.Cells(Rows.Count, 1).End(3).Row + 1
 
    With ws1.Cells(1).CurrentRegion
        .AutoFilter 4, "0"
        .Offset(1).Resize(.Rows.Count - 1).Copy ws2.Cells(lr, 1)
        .Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
        .AutoFilter
    End With
End Sub
This didnt work for me, it moved all rows not only the ones containing 0 in range D, btw how can i make this code work on all columns (without keeping whats in range E) please?
 
Last edited:
Upvote 0
This didnt work for me, it moved all rows not only the ones containing 0 in range D, btw how can i make this code work on all columns (without keeping whats in range E) please?
I don't quite understand. In post #2 you said "I want it to move the rows whose value of their D cells equals 0 to Sheet2 in normal order starting from A2 and going down" - and the code I provided does just that based on the sample data you provided.

Before running the code, sheet1:
jakezer.xlsb
ABCDE
1SubjectDateSenderStateVerification
2a6/03/2022sender1
3b23/03/2022sender0
4c28/03/2022sender1
5d28/03/2022sender2
6
Sheet1


sheet2:
jakezer.xlsb
ABCDE
1SubjectDateSenderStateVerification
2
Sheet2


After running the code, sheet1:
jakezer.xlsb
ABCDE
1SubjectDateSenderStateVerification
2a6/03/2022sender1
3c28/03/2022sender1
4d28/03/2022sender2
5
Sheet1


sheet2:
jakezer.xlsb
ABCDE
1SubjectDateSenderStateVerification
2b23/03/2022sender0
3
Sheet2


Anyhow, to copy just columns A-D, change this line of code:

VBA Code:
.Offset(1).Resize(.Rows.Count - 1).Copy ws2.Cells(lr, 1)

to this:

VBA Code:
.Offset(1).Resize(.Rows.Count - 1,4).Copy ws2.Cells(lr, 1)
 
Upvote 0
Solution
Thanks guys, both solutions work, but in my case i think i have to edit them a little so they can fit perfectly with my work
I posted another thread about this explaining my case with more details
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

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