2013-12-17 49 views
2

道歉,但我是VBA的新手。我已经使用该网站来处理问题,我有匹配2列的数据;VBA - 排列excel中的数据值

IP26 5BN IP26 5BN 
IP26 5BN IP26 5DB 
IP26 5DB IP26 5DB 
IP26 5EL IP26 5EL 
IP26 5ET IP26 5EL 
IP26 5ET IP26 5ET 
IP26 5HN IP26 5ET 
IP26 5HR IP26 5HN 
IP26 5HR IP26 5HR 
IP26 5JA IP26 5JA 
IP26 5JA IP26 5JA 
IP26 5NJ IP26 5NJ 
IP27 0DJ IP27 0DJ 
IP27 0DZ IP27 0DZ 
IP27 0ER IP27 0ER 
IP27 0JN IP27 0JN 

项目可以是在列A &乙多次,并且我想排队是在A和B中,反之亦然的匹配元件的任何项目,并且当存在于任一A和B插入空白行是一个额外的“相同”的价值。

我必须;

Sub Expand() 
Dim first_col As Range 
Dim second_col As Range 
Dim row As Integer 

Set first_col = Range("A1:A17") 
Set second_col = Range("B1:B17") 

For row = 1 To second_col.Rows.Count 
    If (first_col.Cells(row, 1).Value = second_col.Cells(row, 1).Value Or second_col.Cells(row, 1).Value = first_col.Cells(row, 1).Value) Then 
    End If 

     If first_col.Cells(row, 1).Value <> second_col.Cells(row, 1).Value Then 
     second_col.Cells(row, 1).Insert shift:=xlDown 
     ElseIf first_col.Cells(row, 1).Value = second_col.Cells(row, 1).Offset(1, 0).Value Then 
     '// code to insert the row 
     first_col.Cells(row, 1).Insert shift:=xlDown 


    End If 
Next row 
End Sub 

但正在发生的事情是第一次,如果不总是返回一个“真”时,我认为它应该和这样的阵容去一部分一路下跌或他们对齐时有重复的最后一行。

有什么建议吗?

回答

0

您的第一个If块为空。

你的意思是这样做吗?

For Row = 1 To second_col.Rows.Count 
    If (first_col.Cells(Row, 1).Value = second_col.Cells(Row, 1).Value Or second_col.Cells(Row, 1).Value = first_col.Cells(Row, 1).Value) Then 
    'End If <-- Remove this 

     If first_col.Cells(Row, 1).Value <> second_col.Cells(Row, 1).Value Then 
     second_col.Cells(Row, 1).Insert shift:=xlDown 
     ElseIf first_col.Cells(Row, 1).Value = second_col.Cells(Row, 1).Offset(1, 0).Value Then 
     '// code to insert the row 
     first_col.Cells(Row, 1).Insert shift:=xlDown 
     End If '<-- Add this 

    End If 
Next Row 
0

我不完全理解你的问题。但是,我假设你想要在活动单元下面插入新行。 我认为这可能会对你有所帮助。

ActiveCell.Offset(1).EntireRow.Insert