VBA loop code

Woodsa

New Member
Joined
Feb 26, 2021
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am trying to put some code together, but seem unable to crack it.

I want to check if one cell does not match another cell, then cut the data out and paste to right.

this is what I am trying to achieve:

IF A2 <> D2 'if the value in A2 does not match the value in D2
Cut (D2:E2) 'Cut the data from the rows
Paste G2:H2 'Paste the data below the last set of data
Shift up the now blank row cells 'Cut the data out and paste it in G2 then shift the row up to remove blank cells
Loop 'I want it to stop at this point and then loop through the data starting from the beginning and keep doing this action until all the data matches up
 

Attachments

  • Example.png
    Example.png
    9.1 KB · Views: 8

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I was experimenting with this, but quickly realised that I needed some kind of dynamic Loop function to be able to look through all of the data

Sub Tests()
Dim lrow As Long
lrow = Cells(Rows.Count, "G").End(xlUp).Row

If Range("A3") <> Range("D3") Then
Range("D3:E3").Copy Range("G" & lrow).Offset(1, 0)
Else:
End If
End Sub
 
Upvote 0
You can try:

VBA Code:
Sub transit()
Dim RangeA As Range, Cell As Range
Set RangeA = Selection

For Each Cell In RangeA
If Cell.Value <> Cell.Offset(, 3).Value Then
Cell.Offset(, 3).Resize(1, 2).Cut Cell.Offset(, 6).Resize(1, 2)
End If
Next

End Sub
 
Upvote 0
You can try:

VBA Code:
Sub transit()
Dim RangeA As Range, Cell As Range
Set RangeA = Selection

For Each Cell In RangeA
If Cell.Value <> Cell.Offset(, 3).Value Then
Cell.Offset(, 3).Resize(1, 2).Cut Cell.Offset(, 6).Resize(1, 2)
End If
Next

End Sub
Hey,

Thank you for taking the time to look at this, can you explain how it works?

I am trying to get it to loop through each line, if it doesn't match then I want to to cut it out, close the gap and check again to see if it matches.
 
Upvote 0
Try
VBA Code:
Sub test()
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    With CreateObject("scripting.dictionary")
        For i = Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
            If Cells(i, 4) <> Cells(i, 5) Then
                If Not .exists(Cells(i, 1) & Cells(i, 4)) Then
                    .Add Cells(i, 4).Value, Cells(i, 5).Value
                    Cells(i, 4).Resize(, 2).Delete Shift:=xlUp
                End If
            End If
        Next
        Cells(2, 7).Resize(.Count, 2) = Application.Transpose(Application.Index(Array(.keys, .items), 0, 0))
    End With
End Sub
 
Upvote 0
Try
VBA Code:
Sub test()
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    With CreateObject("scripting.dictionary")
        For i = Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
            If Cells(i, 4) <> Cells(i, 5) Then
                If Not .exists(Cells(i, 1) & Cells(i, 4)) Then
                    .Add Cells(i, 4).Value, Cells(i, 5).Value
                    Cells(i, 4).Resize(, 2).Delete Shift:=xlUp
                End If
            End If
        Next
        Cells(2, 7).Resize(.Count, 2) = Application.Transpose(Application.Index(Array(.keys, .items), 0, 0))
    End With
End Sub
Wow, this is very complex haha, I have tried it and didn't get any errors, but I am not sure what has happened, it has copied a few rows of data to different columns, but the logic isn't right.
 
Upvote 0
Well,
May be the result is upside down
Cause started from the bottom up
!!!
 
Upvote 0
Try this version
VBA Code:
Sub test()
    Dim i As Long
    Dim drng As Range
    With CreateObject("scripting.dictionary")
        For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
            If Cells(i, 4) <> Cells(i, 5) Then
                If Not .exists(Cells(i, 1) & Cells(i, 4)) Then
                    .Add Cells(i, 4).Value, Cells(i, 5).Value
                    If drng Is Nothing Then
                        Set drng = Cells(i, 4).Resize(, 2) 
                    Else
                        Set drng = Union(drng, Cells(i, 4).Resize(, 2))
                    End If
                End If
            End If
        Next
        drng.Delete Shift:=xlUp
        Cells(2, 7).Resize(.Count, 2) = Application.Transpose(Application.Index(Array(.keys, .items), 0, 0))
    End With
End Sub
 
Upvote 0
Try this version
VBA Code:
Sub test()
    Dim i As Long
    Dim drng As Range
    With CreateObject("scripting.dictionary")
        For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
            If Cells(i, 4) <> Cells(i, 5) Then
                If Not .exists(Cells(i, 1) & Cells(i, 4)) Then
                    .Add Cells(i, 4).Value, Cells(i, 5).Value
                    If drng Is Nothing Then
                        Set drng = Cells(i, 4).Resize(, 2)
                    Else
                        Set drng = Union(drng, Cells(i, 4).Resize(, 2))
                    End If
                End If
            End If
        Next
        drng.Delete Shift:=xlUp
        Cells(2, 7).Resize(.Count, 2) = Application.Transpose(Application.Index(Array(.keys, .items), 0, 0))
    End With
End Sub
Thank you very much for looking into this, it seems to just be copying all the data to rows D:E and not matching at all
 
Upvote 0
Book1
ABCDEFGH
1
2102030102030102030102030
310203010203099999102030
4102030102030102030102030
5102030102030102030102030
6102030102030102030102030
71020301020303333102030
8102030102030102030102030
9102030102030102030102030
10
Sheet3



Book1
ABCDEFGH
1
210203010203010203010203099999102030
31020301020301020301020303333102030
4102030102030102030102030
5102030102030102030102030
6102030102030102030102030
7102030102030102030102030
8102030102030
9102030102030
10
11
Sheet3
 
Upvote 0

Forum statistics

Threads
1,215,025
Messages
6,122,734
Members
449,094
Latest member
dsharae57

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