Row match and alignment VBA code

Status
Not open for further replies.

Amms123

New Member
Joined
Aug 3, 2019
Messages
15
I have a vba code I use to align and match column rows of data and up to about200 rows of data it's been working without any problems. However, I haveencountered a problem when using it on several hundred or thousands of rows of data.

Below is the code I am using to match and align the column rows of data: -

Sub RowFormat()
Dim Rng As Range
Dim Dn As Range
Dim Dic1 As Object
Set Rng = Range(Range("A2"), Range("A" &Rows.Count).End(xlUp)).Resize(, 2)
Set Dic1 = CreateObject("scripting.dictionary")
Dic1.CompareMode =vbTextCompare
For Each Dn In Rng
If Not Dic1.Exists(Dn.Value) Then
Dic1.Add Dn.Value,""
Else
Dic1.Remove (Dn.Value)
End If
Next

For Each Dn In Rng
If Dn <> "" AndDic1.Exists(Dn.Value) Then
If Dn.Column = 1 Then
Dn.Offset(,1).Insert
Else
Dn.Offset(, -1).Insert
End If
End If
Next Dn
Set Rng = Range(Range("A2"), Range("A" &Rows.Count).End(xlUp))
For Each Dn In Rng
If Not Dn = "" And NotDic1.Exists(Dn.Value) Then Dn.Offset(, 1) = Dn
Next Dn
EndSub

Thedata below is an example of the raw data with both columns sorted in ascendingorder before running the code.

A-0001-T-01 A-0002-T-02
A-0002-V-01 A-0002-V-02
AH-01-V-0001 AX-00001
B-01-RG-01 C-0002
E-00001A E-00002A
E-00002A

Below is the data after running the code, as you can see the second column hasbeen aligned to match the first column and visa versa.
If there is no match it leaves a blank cell and goes onto the next row of data

A-0001-T-01
A-0002-T-02
A-0002-V-01
A-0002-V-02
AH-01-V-0001
AX-00001
B-01-RG-01
C-0002
E-00001A
E-00002A E-00002A

However, a problem has occurred with the code when used on bigger amounts ofdata and below shows the match and alignment has failed for the rows 458 thru'462, It then continues to correctly match and align the rows of datathereafter.

A further problem due to the miss-match in the second column, the data hasincreased by 5 items, re the duplication of V-0001 thru' V-0005, as shownbelow in red font.


US-00-154
US-00-155
V-0001
US-00-156
V-0002
US-00-157
V-0003
US-00-158
V-0004
US-00-162
V-0005
V-0001 V-0001
V-0002 V-0002
V-0003 V-0003
V-0004 V-0004
V-0005 V-0005
V-0006 V-0006
V-0007 V-0007
X-0001
X-0002

I would be grateful for any advice how to remedy this problem, or if there is a better wayto meet my objective consistently.

Thx

Amms123
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Status
Not open for further replies.

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

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