Copie to

brankscaffold

New Member
Joined
Jun 15, 2022
Messages
18
Office Version
  1. 365
Platform
  1. Windows
I got this one off the net, but I can't get it to work quite right.

now only 2 lines are copied and always the same

VBA Code:
Sub rowcopy()
Const FirstRow = 2
Dim rij As Long
Dim n As Long
Dim src As Worksheet
Dim trg As Worksheet
Dim SrcRow As Long
Dim lastRow As Long
                        Set src = Sheets("Ridon 22")
                        Set trg = Sheets("Asindo 22")
                                                        Application.ScreenUpdating = False

                        rij = trg.[A65536].End(xlUp).Row

For n = 5 To Blad1.[A65536].End(xlUp).Row
If Cells(n, "AE").Value = "Asindo" Then
Range(Cells(n, "A"), Cells(n, "AZ")).Copy
trg.Cells(rij, "A").PasteSpecial
 
VBA Code:
ElseIf src.Cells(n, "B" And "E" And "H" And "AD").Value <> src.Cells(n - 1, "B" And "E" And "H" And "AD").Value Then
                OKToCopy = "Y"
          '  ElseIf src.Cells(n, "E").Value <> src.Cells(n - 1, "E").Value Then
          '      OKToCopy = "Y"
          '  ElseIf src.Cells(n, "H").Value <> src.Cells(n - 1, "H").Value Then
          '      OKToCopy = "Y"
           ' ElseIf src.Cells(n, "AD").Value <> src.Cells(n - 1, "AD").Value Then
          '      OKToCopy = "Y"
you're the best.
I made 1 line of everything with AND in it, so that it transfers the data, for example, the same invoice number but a different name. he didn't transfer that at first and now he does
 
Upvote 0

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
I made 1 line of everything with AND in it
I had considered that but someone suggested once that it would be faster (and I don't by how much) if it didn't have to evaluate all the if expressions, which it has to when you join them with and AND. So the aim I guess is to put the most likely to be the decider first, so it only has to do the others infrequently.

Thanks for letting me know it is working for you and glad I could help.
 
Upvote 0
you're the best.
So, the answer post is #20. Therefore, I changed the marked solution accordingly.

@brankscaffold - As far as I know, the Cells property requires either column index number or column letters as a string (like 2 or "B"). You should be getting an error with the following usage unless you have an On Error statement that will silence the error.

VBA Code:
src.Cells(n, "B" And "E" And "H" And "AD").Value

Note: I've never seen such usage, and it shouldn't work unless the operands are either boolean or numeric that could be cast as an operand which will only return the wrong column number in the second case. However, I am very much interested to learn if this is a known method that I've never seen before.

However, if you'd like to shorten the code, then you can use OR operator as shown below:
VBA Code:
If n = FirstRow Or _
            src.Cells(n, "B").Value <> src.Cells(n - 1, "B").Value Or _
            src.Cells(n, "E").Value <> src.Cells(n - 1, "E").Value Or _
            src.Cells(n, "H").Value <> src.Cells(n - 1, "H").Value Or _
            src.Cells(n, "AD").Value <> src.Cells(n - 1, "AD").Value Then
    OKToCopy = "Y"
Else
    OKToCopy = "N"
End If
 
Upvote 0
So, the answer post is #20. Therefore, I changed the marked solution accordingly.

@brankscaffold - As far as I know, the Cells property requires either column index number or column letters as a string (like 2 or "B"). You should be getting an error with the following usage unless you have an On Error statement that will silence the error.

VBA Code:
src.Cells(n, "B" And "E" And "H" And "AD").Value

Note: I've never seen such usage, and it shouldn't work unless the operands are either boolean or numeric that could be cast as an operand which will only return the wrong column number in the second case. However, I am very much interested to learn if this is a known method that I've never seen before.

However, if you'd like to shorten the code, then you can use OR operator as shown below:
VBA Code:
If n = FirstRow Or _
            src.Cells(n, "B").Value <> src.Cells(n - 1, "B").Value Or _
            src.Cells(n, "E").Value <> src.Cells(n - 1, "E").Value Or _
            src.Cells(n, "H").Value <> src.Cells(n - 1, "H").Value Or _
            src.Cells(n, "AD").Value <> src.Cells(n - 1, "AD").Value Then
    OKToCopy = "Y"
Else
    OKToCopy = "N"
End If
with 4 of the same data it is a duplicate and they will not copy or move. if there is a difference, these must be copied / moved.
if I enter your code (maybe I did it wrong) then it only creates 1 sheet and copies only 1 line to it

VBA Code:
ElseIf src.Cells(n, "B" And "E" And "H" And "AD").Value <> src.Cells(n - 1, "B" And "E" And "H" And "AD").Value Then

no matter what I try this line works so far, and luckily I don't get a message yet

thanks for the input
 
Upvote 0
So, the answer post is #20. Therefore, I changed the marked solution accordingly.

@brankscaffold - As far as I know, the Cells property requires either column index number or column letters as a string (like 2 or "B"). You should be getting an error with the following usage unless you have an On Error statement that will silence the error.

VBA Code:
src.Cells(n, "B" And "E" And "H" And "AD").Value

Note: I've never seen such usage, and it shouldn't work unless the operands are either boolean or numeric that could be cast as an operand which will only return the wrong column number in the second case. However, I am very much interested to learn if this is a known method that I've never seen before.

However, if you'd like to shorten the code, then you can use OR operator as shown below:
VBA Code:
If n = FirstRow Or _
            src.Cells(n, "B").Value <> src.Cells(n - 1, "B").Value Or _
            src.Cells(n, "E").Value <> src.Cells(n - 1, "E").Value Or _
            src.Cells(n, "H").Value <> src.Cells(n - 1, "H").Value Or _
            src.Cells(n, "AD").Value <> src.Cells(n - 1, "AD").Value Then
    OKToCopy = "Y"
Else
    OKToCopy = "N"
End If
as you said my adjustment worked temporarily.
thanks for the input
 
Upvote 0
I also need a check for already copied data on the already existing sheets also with the same search equations.

can you help point me in the right direction
Or should I create a new topic for this?
 
Upvote 0
You would need to create a new topic for that.

If you simply wanted to check whether was data below the headings on the target sheet, that would be fairly straight forward but
with the same search equations.
indicates you want some sort of criteria applied, the won't be a trivial exercise.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,874
Members
452,363
Latest member
merico17

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