返回列表 上一主題 發帖

[發問] 我想要做sheet的欄位做排序並刪除重覆的列

回復 1# kasl
試試看
  1. Option Explicit
  2. Sub Ex()
  3.     Dim i As Integer, Rng As Range
  4.     With ActiveSheet  'Sheets("sheet1")
  5.         .UsedRange.Sort Key1:=.Range("A2"), Order1:=xlAscending, Key2:=.Range( _
  6.         "E2"), Order2:=xlAscending, Header:=xlYes
  7.         i = 2
  8.         Do While .Cells(i, "E") <> ""
  9.             If .Cells(i, "b") & .Cells(i, "E") = .Cells(i + 1, "b") & .Cells(i + 1, "E") Then
  10.                 If Rng Is Nothing Then
  11.                     Set Rng = .Cells(i, "E").Offset(1)
  12.                 Else
  13.                     Set Rng = Union(Rng, .Cells(i, "E").Offset(1))
  14.                 End If
  15.             End If
  16.             i = i + 1
  17.         Loop
  18.         If Not Rng Is Nothing Then
  19.             Rng.EntireRow.Delete
  20.             .UsedRange.Sort Key1:=.Range("C2"), Order1:=xlAscending, Key2:=.Range( _
  21.             "A2"), Order2:=xlAscending, Header:=xlYes
  22.         End If
  23.     End With
  24. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 3# kasl
  1. Option Explicit
  2. Sub Ex()
  3.     Dim i As Integer, Rng As Range
  4.     With ActiveSheet  'Sheets("sheet1")
  5.         .UsedRange.Sort Key1:=.Range("A2"), Order1:=xlAscending, Key2:=.Range( _
  6.         "E2"), Order2:=xlAscending, Header:=xlYes
  7.         i = 2
  8.         Do While .Cells(i, "E") <> ""
  9.             If .Cells(i, "b") = .Cells(i + 1, "b") Then '當日與下一日 個股名稱一樣
  10.                 If .Cells(i, "E") = .Cells(i + 1, "E") Or .Cells(i, "E") > .Cells(i + 1, "C") Then
  11.                   '.Cells(i, "E") = .Cells(i + 1, "E")-> 出場日一樣
  12.                   '.Cells(i, "E") > .Cells(i + 1, "C")-> 當日進場還沒出場 下一筆就進場
  13.                     If Rng Is Nothing Then
  14.                         Set Rng = .Cells(i, "E").Offset(1)
  15.                     Else
  16.                         Set Rng = Union(Rng, .Cells(i, "E").Offset(1))
  17.                     End If
  18.                 End If
  19.             End If
  20.             i = i + 1
  21.         Loop
  22.         If Not Rng Is Nothing Then
  23.             Rng.EntireRow.Delete
  24.             .UsedRange.Sort Key1:=.Range("C2"), Order1:=xlAscending, Key2:=.Range( _
  25.             "A2"), Order2:=xlAscending, Header:=xlYes
  26.         End If
  27.     End With
  28. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 地上種了菜,就不易長草;心中有善,就不易生惡。
返回列表 上一主題