返回列表 上一主題 發帖

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

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


呈上資料頁面,請問如果我要先以land_no_m和land_no_c兩攔為準做排序之後保留以下這九個欄位其他刪除該怎麼撰寫VBA呢~感謝
(section) (SC) (LANDUSE)  (PUBNO)        (OPTION)        (METHOD)        (MUPLAN)        (DUPLAN)        (ORG_FID)

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

TOP

@@是的每張欄位格式都是一樣的,所以land_no_m, land_no_c 每張表都有,只有少數才有多一行的狀況才會想說指定欄位,感謝您的再度協助

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

遇到問題了@@如果需要以land_no_m為KEY1與land_no_c為KEY2去做排序以下排序的程序該如何修改ㄋ

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

  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

TOP

感激~註解詳細~感覺上了一課@@謝謝您的解答

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

感謝stillfish00 這對我這菜鳥來說,看起來簡直是變魔術,感恩,是您的匯入方式比較合理,
.Range("A:C,E:I,K,Q").Delete Shift:=xlToLeft是直接選定欄位
最後一個問題是所謂"刪除特定攔位"可以是指定保留(section) (SC) (LANDUSE) (PUBNO) (OPTION) (METHOD) (MUPLAN) (DUPLAN) (ORG_FID)標題的一整個欄位,其他無論甚麼標題的欄位都刪掉的寫法嗎(用以確保批次時某一個檔的例外多一條無須保留的攔位)

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

@@Private Sub CommandButton2_Click()
的OK了對於您的解答感謝萬分,我來好好研究一下
Private Sub CommandButton1_Click()的意思是說
"複製工作表過來時名字應該是和原本工作表一樣的"沒錯
因為我的檔案有很多,而且雖然內容格式一樣但檔名不一樣,我的想法是要怎麼樣
把不同檔名的檔案匯進來之後都可以執行Private Sub CommandButton2_Click(),
因為指定的With Sheets("工作表名稱")工作表名稱會變
等這個都了解了接下來再想下一步怎麼批次
再次謝謝你歐

TOP

        靜思自在 : 成功是優點的發揮,失敗是缺點的累積。
返回列表 上一主題