返回列表 上一主題 發帖

[發問] 排序後刪除特定攔位

回復 1# billchenfantasy
依你敘述的需求應該用一般Excel操作就可達成:
先將不必要的欄刪除 , 再到  資料>排序(excel 2010)  設定即可

TOP

回復 3# billchenfantasy
>> Private Sub CommandButton1_Click()
>> 以上是將資料匯入並開新的工作表(同時名稱為該資料檔)請問要如何設置自動命名為同一個工作表名稱?

不明白你的意思 , 你複製工作表過來時名字應該是和原本工作表一樣的

>>Private Sub CommandButton2_Click()

試試看
Private Sub CommandButton2_Click()
With Sheets("工作表名稱")
    .[A1].CurrentRegion.Sort Key1:=.[E:E], Order1:=xlAscending, _
                             Key2:=.[F:F], Order2:=xlAscending, _
                             Header:=xlYes
    .Range("A:C,E:I,K:L,Q:Q").Delete Shift:=xlToLeft
End With
End Sub

TOP

回復 5# billchenfantasy
同一活頁簿是不能有多個相同名稱的工作表

你要的批次處理是這樣嗎? (可選多個檔案)
  1. Private Sub CommandButton1_Click()
  2. Dim Source, f

  3. '可選擇多個檔案
  4. Source = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls; *.xlsx),*.xls;*.xlsx", _
  5.                                     MultiSelect:=True)
  6. If TypeName(Source) = "Boolean" Then If Source = False Then Exit Sub

  7. For Each f In Source
  8. '開啟檔案/活頁簿
  9. With Workbooks.Open(f)
  10.     '對所有工作表
  11.     For i = 1 To ActiveWorkbook.Sheets.Count
  12.         '複製工作表到本活頁簿
  13.         .Sheets(i).Copy after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
  14.         '本活頁簿中該工作表
  15.         With ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
  16.            '依E,F欄排序
  17.            .[A1].CurrentRegion.Sort Key1:=.[E:E], Order1:=xlAscending, _
  18.                                     Key2:=.[F:F], Order2:=xlAscending, _
  19.                                     Header:=xlYes
  20.            '刪除不要的欄
  21.            .Range("A:C,E:I,K:L,Q:Q").Delete Shift:=xlToLeft
  22.         End With
  23.     Next i
  24.     '關閉檔案
  25.     .Close
  26. End With
  27. Next f
  28. End Sub
複製代碼

TOP

回復 7# billchenfantasy
  1. Private Sub CommandButton1_Click()
  2. Dim Source, f
  3. Dim rng As Range

  4. '可選擇多個檔案
  5. Source = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls; *.xlsx),*.xls;*.xlsx", _
  6.                                     MultiSelect:=True)
  7. If TypeName(Source) = "Boolean" Then If Source = False Then Exit Sub

  8. For Each f In Source
  9. '開啟檔案/活頁簿
  10. With Workbooks.Open(f)
  11.     '對所有工作表
  12.     For i = 1 To ActiveWorkbook.Sheets.Count
  13.         '複製工作表到本活頁簿
  14.         .Sheets(i).Copy after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
  15.         '本活頁簿中該工作表
  16.         With ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
  17.            '依land_no_m, land_no_c欄排序
  18.             If Not IsError(Application.Match("land_no_m", .Rows(1), 0)) Then .[A1].CurrentRegion.Sort Key1:=.Columns(Application.Match("land_no_m", .Rows(1), 0)), Order1:=xlAscending, Header:=xlYes
  19.             If Not IsError(Application.Match("land_no_c", .Rows(1), 0)) Then .[A1].CurrentRegion.Sort Key1:=.Columns(Application.Match("land_no_c", .Rows(1), 0)), Order1:=xlAscending, Header:=xlYes
  20.             
  21.             '找出不符合的欄
  22.             For j = 1 To .[A1].CurrentRegion.Columns.Count
  23.                 If IsError(Application.Match(.Cells(1, j).Value, Array("section", "SC", "LANDUSE", "PUBNO", "OPTION", "METHOD", "MUPLAN", "DUPLAN", "ORG_FID"), 0)) Then
  24.                     If rng Is Nothing Then Set rng = .Columns(j) Else Set rng = Union(rng, .Columns(j))
  25.                 End If
  26.             Next j
  27.             '刪除
  28.             .Range(rng.Address).Delete shift:=xlToLeft
  29.             Set rng = Nothing
  30.         End With
  31.     Next i
  32.     '關閉檔案
  33.     .Close
  34. End With
  35. Next f
  36. End Sub
複製代碼

TOP

回復 10# billchenfantasy
land_no_m, land_no_c 每張表都有嗎?
  1.     If Not (IsError(Application.Match("land_no_m", .Rows(1), 0)) Or _
  2.             IsError(Application.Match("land_no_c", .Rows(1), 0))) Then
  3.         .[A1].CurrentRegion.Sort Key1:=.Columns(Application.Match("land_no_m", .Rows(1), 0)), _
  4.                                     Order1:=xlAscending, _
  5.                                     Key2:=.Columns(Application.Match("land_no_c", .Rows(1), 0)), _
  6.                                     Order2:=xlAscending, _
  7.                                     Header:=xlYes
  8.     Else
  9.         MsgBox .Sheets(i).Name & " : Sorting field not found."
  10.     End If
複製代碼

TOP

回復 12# billchenfantasy
恩 試了一下  #10 的兩行順序調換
結果應該跟 #11 一樣

TOP

        靜思自在 : 稻穗結得越飽滿,越會往下垂,一個人越有成就,就要越有謙沖的胸襟。
返回列表 上一主題