Lining up Row ins columns

jlrivera11

New Member
Joined
Mar 4, 2006
Messages
2
Hello,
I am working on a project in excel and hope I can get a little help here,

The problem is I have several columns of data two of which are the same, however in column A there is 1000 entries and in column D there are 400 entries, columns B, C, E and F are different. Is there a way I can line up the rows of columns A and D keeping the data which is part of this particular data here’ an example below of what I want to do:

Columns A and D are sorted however they are not lined up to each other.

Column A Column B Column C Column D Column E Column F
DXX02114 327184 974487 DXX02114 327183 974488
DXX02115 326910 974636 DXX02201 328405 972770
DXX02118 327379 972798 DXX02203 328293 9722251
DXX02122 326865 973269 DXX02204 328506 9716100
DXX02200 328407 973630 DXX02206 329690 9740993
DXX02201 328405 972773
DXX02202 328808 971546
DXX02203 328293 972225
DXX02204 328504 971614
DXX02205 328399 970741
DXX02206 329695 974093



The following has the rows of columns A and D manually lined up. And kept associated data in rows to columns A and D.

Column A Column B Column C Column D Column E Column F
DXX02114 327184 974487 DXX02114 327183 974488
DXX02115 326910 974636
DXX02118 327379 972798
DXX02122 326865 973269
DXX02200 328407 973630
DXX02201 328405 972773 DXX02201 328405 972770
DXX02202 328808 971546
DXX02203 328293 972225 DXX02203 328293 9722251
DXX02204 328504 971614 DXX02204 328506 9716100
DXX02205 328399 970741
DXX02206 329695 974093 DXX02206 329690 9740993


Is there a macro which can help me do this?
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
jlrivera11

Welcome to the Mr Excel board!

See if this is any help.

1. Insert 3 new columns between your existing columns C and D
2. In the new cell D1, put the formula =IF(ISNUMBER(MATCH($A1,$G:$G,0)),INDEX($G:$I,MATCH($A1,$G:$G,0),COLUMN()-COLUMN($C1)),"")
3. Copy this formula across to new cell F1 and then all down to the bottom of your data.
4. You could hide columns G:I (old data if required)
Mr Excel.xls
ABCDEFGHIJ
1DXX02114327184974487DXX02114327183974488DXX02114327183974488
2DXX02115326910974636   DXX02201328405972770
3DXX02118327379972798   DXX022033282939722251
4DXX02122326865973269   DXX022043285069716100
5DXX02200328407973630   DXX022063296909740993
6DXX02201328405972773DXX02201328405972770
7DXX02202328808971546   
8DXX02203328293972225DXX022033282939722251
9DXX02204328504971614DXX022043285069716100
10DXX02205328399970741   
11DXX02206329695974093DXX022063296909740993
12
Sheet1
 
Upvote 0
Also, using Peter's 3 inserted columns, you can use these VLOOKUP formulas, copied down.

In D1, =IF(ISNA(VLOOKUP($A1,$G$1:$I$5,1,FALSE)),"",VLOOKUP($A1,$G$1:$I$5,1,FALSE))

In E1, =IF(ISNA(VLOOKUP($A1,$G$1:$I$5,2,FALSE)),"",VLOOKUP($A1,$G$1:$I$5,2,FALSE))

In F1, =IF(ISNA(VLOOKUP($A1,$G$1:$I$5,3,FALSE)),"",VLOOKUP($A1,$G$1:$I$5,3,FALSE))

Whichever formulas you use, after you copy D1:F1 down as far as needed, select those three columns, copy them to the clipboard and then use Edit>Paste Special>Values to convert the formulas to values. You can then get rid of columns G:I.
 
Upvote 0
Hi jlrivera11:

Welcome to MrExcel Board!

Following is a formula based soution ...
y060304h1.xls
ABCDEFGHIJ
1ColumnAColumnBColumnCColumnDColumnEColumnFColumnDColumnEColumnF
2DXX02114327184974487DXX02114327183974488DXX02114327183974488
3DXX02115326910974636DXX02201328405972770   
4DXX02118327379972798DXX022033282939722251   
5DXX02122326865973269DXX022043285069716100   
6DXX02200328407973630DXX022063296909740993DXX02201328405972770
7DXX02201328405972773DXX02201328405972770
8DXX02202328808971546   
9DXX02203328293972225DXX022033282939722251
10DXX02204328504971614DXX022043285069716100
11DXX02205328399970741   
12DXX02206329695974093DXX022063296909740993
13
Sheet4


Array Formula in cell G2 is ...

=IF(ISNA(MATCH(LEFT($B2,5),LEFT($E$2:$E$6,5),0)),"",INDEX(D$2:D$6,MATCH(LEFT($B2,5),LEFT($E$2:$E$6,5),0)))

this is then copied down and across to cells G2:I12.

You may optionally hide columns D:F, or first paste values in cells G2:I12, and then delete columns D:F

I hope this helps! If you must have a macro based solution, please post back!
 
Upvote 0
Hi jlrivera11:

Using the same data layout as in my last post, for the VBA solution, I temporarily use three extra columns G, H, and I
Code:
Sub yJeshuatree()
        Range("G2").FormulaArray = "=IF(ISNA(MATCH(LEFT($B2,5),LEFT($E$2:$E$6,5),0)),"""",INDEX(D$2:D$6,MATCH(LEFT($B2,5),LEFT($E$2:$E$6,5),0)))"
        Range("H2").FormulaArray = "=IF(ISNA(MATCH(LEFT($B2,5),LEFT($E$2:$E$6,5),0)),"""",INDEX(E$2:E$6,MATCH(LEFT($B2,5),LEFT($E$2:$E$6,5),0)))"
        Range("I2").FormulaArray = "=IF(ISNA(MATCH(LEFT($B2,5),LEFT($E$2:$E$6,5),0)),"""",INDEX(F$2:F$6,MATCH(LEFT($B2,5),LEFT($E$2:$E$6,5),0)))"
        Range("G2:I2").Copy Range("G3:G12")
        [g2:i12].Copy
        [g2:i12].PasteSpecial Paste:=xlValues
        Application.CutCopyMode = False
        'now you may optionally delete or hide columns D:F
        'Columns("D:F").Delete
End Sub
I hope this helps!
 
Upvote 0
Hi
try
Code:
Sub test()
Dim dic As Object, a, i As Long, result(), temp
Set dic = CreateObject("scripting.dictionary")
a = Range("a1").CurrentRegion.Resize(, 6).Value
For i = 1 To UBound(a, 1)
    If Not IsEmpty(a(i, 1)) Then
        If Not dic.exists(a(i, 1)) Then
            dic.Add a(i, 1), i
        End If
    End If
Next
For i = 1 To UBound(a, 1)
    If dic.exists(a(i, 4)) Then
        x = dic(a(i, 4))
        For ii = 1 To 3
            temp = a(x, 3 + ii)
            a(x, 3 + ii) = a(i, 3 + ii)
            a(i, 3 + ii) = temp
        Next
    End If
Next
Range("a1").CurrentRegion.Resize(, 6).Value = a
Set dic = Nothing: Erase a
End Sub
 
Upvote 0
jindon - -

Can you help me understand something please, I'm not asking rehtorically, really just curious as to your method and thinking, what is your reason for creating a scripting dictionary object as you often do in your codes, just to then delete it. I agree that an optimal solution using VBA (not that VBA is the optimal alternative) would include not needing to insert rows or columns to keep the user's environment as unchanged as possible as yours does. Is a scripting object an advantage to just using Find or Match per the following two examples that also accomplish this task:



Sub Test1()
Application.ScreenUpdating = False
Dim xRow&, aFind As Variant
For xRow = Cells(Rows.Count, 1).End(xlUp).row To 2 Step -1
Set aFind = _
Columns(4).Find(What:=Cells(xRow, 1).Value, LookIn:=xlFormulas, LookAt:=xlWhole)
If Not aFind Is Nothing Then _
Range(Cells(aFind.row, 4), Cells(aFind.row, 6)).Cut Cells(xRow, 4)
Next xRow
Application.ScreenUpdating = True
End Sub



Sub Test2()
Application.ScreenUpdating = False
Dim xRow&, var As Variant
For xRow = Cells(Rows.Count, 1).End(xlUp).row To 2 Step -1
var = Application.Match(Cells(xRow, 1).Value, Columns(4), 0)
If Not IsError(var) Then _
Range(Cells(var, 4), Cells(var, 6)).Cut Cells(xRow, 4)
Next xRow
Application.ScreenUpdating = True
End Sub



I recognize that VBA is not required here. Manual insertion of columns and native formulas for a non-VBA approach are do-able as have been shown. This is a general VBA question for anyone, what advantage or disadvantage is there to a scripting object creation and deletion versus simple finding or matching or filtering, and then cutting and pasting. I would think that a scripting object is not necessary, just adding extra steps but I see it used often enough to wonder if it's more advantageous than I've thought.
 
Upvote 0
Hi, Tom

I've just posted the code only for
Thanks All, This really helps, Is there also a way I can express this in a VBA macro?
and I don't think dictionary object is advanced.
If somebody want to use it, it is quite clearly explained in the help.
actually, I got it accidentally while I was exploring help file...

the main reason I often use it with an array is the speed of process.
Unlike Access or other database tools, excel is slow, I'm not saying
excel is bad. it is actually too excelent tool!

As you say, it would be not adequate to use dictionary object in this case....

rgds,
jindon
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,123
Members
452,381
Latest member
Nova88

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