Fetch rows provided there is a difference in one of the cells

sofas

Active Member
Joined
Sep 11, 2022
Messages
493
Office Version
  1. 2019
Platform
  1. Windows
Hello. I have on file sheets 1 and 2 sheet 1 column B includes customer names that are sometimes repeated.and from column c to t customer data. I want when writing his name in Sheet2 cell m4 All rows related to the same name are brought to Sheet2 starting from cell b6 provided there is a difference in one of the cells and in case of the same values are ignored
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Very nice it only lacks fetching column c
Use this:

VBA Code:
Sub comparecells_v2()
  Dim i As Long, j As Long, k As Long, m As Long
  Dim a As Variant, b As Variant
  a = Sheets("Sheet1").Range("C6:T" & Sheets("Sheet1").Range("D" & Rows.Count).End(3).Row).Value
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
 
  For i = 1 To UBound(a, 1) - 1 Step 2
    For j = 3 To UBound(a, 2)
      If a(i, j) <> a(i + 1, j) Then
        k = k + 1
        For m = 1 To UBound(a, 2)
          b(k, m) = a(i, m)
          b(k + 1, m) = a(i + 1, m)
        Next
        k = k + 1
        Exit For
      End If
    Next
  Next
  Application.ScreenUpdating = False
  Sheets("Sheet2").Range("C6").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  Application.ScreenUpdating = True
End Sub

--------------
I hope to hear from you soon.
Respectfully
Dante Amor
--------------​

"A bird in the hand is better than hundreds in the air"
"mas vale pájaro en mano que cientos volando"

-dicho popular-​
 
Upvote 1
Solution
17.PNG
 
Upvote 0
To do this we would either need to be sure that when you have a customer multiple times that it appears "the same number of times and in the same order" on both sheets or have a second reference that we can use to match the specific customer line on each sheet, so we have a unique match.
 
Upvote 0
To do this we would either need to be sure that when you have a customer multiple times that it appears "the same number of times and in the same order" on both sheets or have a second reference that we can use to match the specific customer line on each sheet, so we have a unique match.
Maybe I couldn't communicate the idea well. What I want is to copy the data from one sheet to another provided there is a difference between the values of the first and second rows in one of the cells and ignore the clients with the same values in both rows
Because each customer has a row with their real data and a description of the modification.
The goal is to get the customer whose data has been modified
 

Attachments

  • 99999.PNG
    99999.PNG
    41.4 KB · Views: 6
Upvote 0
To do this we would either need to be sure that when you have a customer multiple times that it appears "the same number of times and in the same order" on both sheets or have a second reference that we can use to match the specific customer line on each sheet, so we have a unique match.
Maybe I couldn't communicate the idea well. What I want is to copy the data from one sheet to another provided there is a difference between the values of the first and second rows in one of the cells and ignore the clients with the same values in both rows
Because each customer has a row with their real data and a description of the modification.
The goal is to get the customer whose data has been modified
 
Upvote 0
Considering your last example, the data starts in cell D6.

Try the following macro:

VBA Code:
Sub comparecells()
  Dim i As Long, j As Long, k As Long
  Application.ScreenUpdating = False
  k = 6
  With Sheets("Sheet1")
    For i = 6 To .Range("D" & Rows.Count).End(3).Row Step 2
      For j = 5 To .Cells(i, Columns.Count).End(1).Column
        If .Cells(i, j).Value <> .Cells(i + 1, j) Then
          .Rows(i & ":" & i + 1).Copy Sheets("Sheet2").Range("A" & k)
          k = k + 2
          Exit For
        End If
      Next
    Next
  End With
  Application.ScreenUpdating = True
End Sub


------------------------
This is probably faster:

VBA Code:
Sub comparecells_v2()
  Dim i As Long, j As Long, k As Long, m As Long
  Dim a As Variant, b As Variant
  a = Sheets("Sheet1").Range("D6:T" & Sheets("Sheet1").Range("D" & Rows.Count).End(3).Row).Value
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
  
  For i = 1 To UBound(a, 1) - 1 Step 2
    For j = 1 To UBound(a, 2)
      If a(i, j) <> a(i + 1, j) Then
        k = k + 1
        For m = 1 To UBound(a, 2)
          b(k, m) = a(i, m)
          b(k + 1, m) = a(i + 1, m)
        Next
        k = k + 1
        Exit For
      End If
    Next
  Next
  Application.ScreenUpdating = False
  Sheets("Sheet2").Range("D6").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  Application.ScreenUpdating = True
End Sub



--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
 
Last edited:
Upvote 0
Considering your last example, the data starts in cell D6.

Try the following macro:

VBA Code:
Sub comparecells()
  Dim i As Long, j As Long, k As Long
  Application.ScreenUpdating = False
  k = 6
  With Sheets("Sheet1")
    For i = 6 To .Range("D" & Rows.Count).End(3).Row Step 2
      For j = 5 To .Cells(i, Columns.Count).End(1).Column
        If .Cells(i, j).Value <> .Cells(i + 1, j) Then
          .Rows(i & ":" & i + 1).Copy Sheets("Sheet2").Range("A" & k)
          k = k + 2
          Exit For
        End If
      Next
    Next
  End With
  Application.ScreenUpdating = True
End Sub


------------------------
This is probably faster:

VBA Code:
Sub comparecells_v2()
  Dim i As Long, j As Long, k As Long, m As Long
  Dim a As Variant, b As Variant
  a = Sheets("Sheet1").Range("D6:T" & Sheets("Sheet1").Range("D" & Rows.Count).End(3).Row).Value
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
 
  For i = 1 To UBound(a, 1) - 1 Step 2
    For j = 1 To UBound(a, 2)
      If a(i, j) <> a(i + 1, j) Then
        k = k + 1
        For m = 1 To UBound(a, 2)
          b(k, m) = a(i, m)
          b(k + 1, m) = a(i + 1, m)
        Next
        k = k + 1
        Exit For
      End If
    Next
  Next
  Application.ScreenUpdating = False
  Sheets("Sheet2").Range("D6").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  Application.ScreenUpdating = True
End Sub



--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
Very nice it only lacks fetching column c
 
Upvote 0
Use this:

VBA Code:
Sub comparecells_v2()
  Dim i As Long, j As Long, k As Long, m As Long
  Dim a As Variant, b As Variant
  a = Sheets("Sheet1").Range("C6:T" & Sheets("Sheet1").Range("D" & Rows.Count).End(3).Row).Value
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
 
  For i = 1 To UBound(a, 1) - 1 Step 2
    For j = 3 To UBound(a, 2)
      If a(i, j) <> a(i + 1, j) Then
        k = k + 1
        For m = 1 To UBound(a, 2)
          b(k, m) = a(i, m)
          b(k + 1, m) = a(i + 1, m)
        Next
        k = k + 1
        Exit For
      End If
    Next
  Next
  Application.ScreenUpdating = False
  Sheets("Sheet2").Range("C6").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  Application.ScreenUpdating = True
End Sub

--------------
I hope to hear from you soon.
Respectfully
Dante Amor
--------------​

"A bird in the hand is better than hundreds in the air"
"mas vale pájaro en mano que cientos volando"

-dicho popular-​
Thank you, thank you, thank you....... I really appreciate your cooperation. You're a wonderful person.
 
Upvote 0

Forum statistics

Threads
1,216,058
Messages
6,128,538
Members
449,456
Latest member
SammMcCandless

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