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è¯å¥ä»¥ä½¿æ¤å®ä»¥æˆ‘需è¦çš„æ–¹å¼è¿è¡Œï¼Ÿ
ç”案 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