循环以在列中排列匹é…值,在没有匹é…时将å•å…ƒæ ¼ç•™ç©º

时间:2016-11-24 23:52:34

标签: excel vba macros

100         300
200         500
300         700
800         400
500         200 
600         100

我想å–两列数字(A列和C列)并排列它们,以便匹é…放在åŒä¸€è¡Œä¸­ï¼ŒåŒæ—¶ä¿ç•™åœ¨å„自的列中。两列都将按å‡åºæŽ’列。如果å¦ä¸€åˆ—中没有匹é…值,我希望那里有一个空格。以下是è¿è¡Œå®åŽåˆ—的外观:

100         100
200         200  
300         300
            400
500         500
600
            700
800

我知é“我需è¦è®¾ç½®ä¸€ä¸ªå¾ªçŽ¯è®©å®ƒåœ¨åˆ—中è¿è¡Œï¼Œå¦‚果在å¦ä¸€åˆ—中找到匹é…项,则将匹é…值å‘上或å‘下移动到其å„自匹é…的行。它将是这样的,从活动å•å…ƒæ ¼å¼€å§‹ï¼Œä½œä¸ºï¼†ï¼ƒ34; A1",A列中的第一个å•å…ƒæ ¼ï¼š

If Not ActiveCell.Value = ActiveCell.Offset(0,2) Then

然åŽè®©å®ƒåœ¨åˆ—C中找到匹é…值,然åŽå°†å…¶å‰ªåˆ‡å¹¶ç²˜è´´åˆ°ä¸Žæ´»åŠ¨å•å…ƒæ ¼ç›¸åŒçš„行中,或者如果没有匹é…值,则在活动å•å…ƒæ ¼çš„行中的列C中留一个空格在Cæ ä¸­ã€‚

我的问题是,如何在循环中设置If-Then语å¥ä»¥ä½¿æ­¤å®ä»¥æˆ‘需è¦çš„æ–¹å¼è¿è¡Œï¼Ÿ

2 个答案:

答案 0 :(得分:0)

看起æ¥ä¸æ˜¯å¾ˆç›´æŽ¥ï¼Œæ‰€ä»¥è¯•ä¸€è¯•ã€‚试试这个代ç ã€‚æ•°æ®åœ¨col Aå’ŒB

中
Sub Macro2()
    Range("A1:A6").Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A1:A6"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A1:A6")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

    Range("B1:B6").Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("B1:B6"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("B1:B6")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

    lr1 = Range("A" & Rows.Count).End(xlUp).Row
    lr2 = Range("B" & Rows.Count).End(xlUp).Row
    lr = WorksheetFunction.Min(lr1, lr2)
    i = 1

    Do While (i <= lr)
        a = Range("A" & i)
        b = Range("B" & i)

        If Not (a = b) Then
            If (Range("A" & i) > Range("B" & i)) Then
                Range("A" & i).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
            Else
                Range("B" & i).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
            End If

        End If
        i = i + 1
        lr1 = Range("A" & Rows.Count).End(xlUp).Row
        lr2 = Range("B" & Rows.Count).End(xlUp).Row
        lr = WorksheetFunction.Min(lr1, lr2)
    Loop
End Sub

答案 1 :(得分:0)

ä½ å¯ä»¥è¯•è¯•è¿™ä¸ªï¼š

Option Explicit

Sub Main()
    With Worksheets("numbers") '<--| change "numbers" to your actual worksheet name
        With Intersect(.Range("A1").Resize(, 2).EntireColumn, .UsedRange)
            .Offset(, .Columns.Count).Resize(, 1).Value = .Columns(1).Value
            .Offset(.Rows.Count, .Columns.Count).Resize(, 1).Value = .Columns(2).Value
            With .Offset(, .Columns.Count).Resize(2 * .Rows.Count, 1)
                .RemoveDuplicates Columns:=1, Header:=xlNo
                .Sort key1:=.Range("A1"), order1:=xlAscending, Header:=xlNo
                Order .Offset(, -2).Resize(.Rows.Count / 2), .Parent.Range(.Range("A1"), .Range("A1").End(xlDown))
                Order .Offset(, -1).Resize(.Rows.Count / 2), .Parent.Range(.Range("A1"), .Range("A1").End(xlDown))
                .ClearContents
            End With
        End With
    End With
End Sub

Sub Order(rngToOrder As Range, sortingRng As Range)
    Dim cell As Range
    ReDim arr(1 To sortingRng.Rows.Count)

    For Each cell In rngToOrder
        arr(sortingRng.Find(cell.Value, LookIn:=xlValues, lookat:=xlWhole).Row) = cell.Value
    Next cell
    rngToOrder.Resize(sortingRng.Rows.Count).Value = Application.Transpose(arr)
End Sub