感謝您的回答其實是因為檔案很多資料長度範圍不一定,想開始學習VBA所致,我自己其實有先用錄製巨集的方式試過如下並設為兩個按鈕,新手學習請您包含
Private Sub CommandButton1_Click()
Dim uFile$
ChDrive "資料磁碟"
ChDir "資料路徑"
Source = Application.GetOpenFilename
With Workbooks.Open(Source)
For i = 1 To ActiveWorkbook.Sheets.Count
.Sheets(i).Copy after:=ThisWorkbook.Worksheets(Sheets.Count)
Next i
.Close
End With
End Sub
以上是將資料匯入並開新的工作表(同時名稱為該資料檔)請問要如何設置自動命名為同一個工作表名稱?
----------------------------------------------------------------------------------------
Private Sub CommandButton2_Click()
Cells.Select
Cells.EntireColumn.AutoFit
Application.WindowState = xlMaximized
ActiveWorkbook.Worksheets("工作表名稱").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("工作表名稱").Sort.SortFields.Add Key:=Range( _
"land_no_m的範圍"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
ActiveWorkbook.Worksheets("工作表名稱").Sort.SortFields.Add Key:=Range( _
"land_no_c的範圍"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("工作表名稱").Sort
.SetRange Range("資料全範圍")----->要怎麼設計無論資料多長都可以選起呢?是(.Range("A1").CurrentRegion)嗎
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Application.Width = 831
Application.Height = 810
Columns("A:C").Select
Range("C1").Activate
Selection.Delete Shift:=xlToLeft
Columns("B:F").Select
Selection.Delete Shift:=xlToLeft
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 1
Columns("C").Select
Selection.Delete Shift:=xlToLeft
Columns("G:G").Select
Selection.Delete Shift:=xlToLeft
Range("J1").Select
End Sub
-------------------------------------------------------------
其實我是想指定刪除上述特定標題的欄位作者: stillfish00 時間: 2013-1-23 11:32
回復 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作者: billchenfantasy 時間: 2013-1-23 12:20
@@Private Sub CommandButton2_Click()
的OK了對於您的解答感謝萬分,我來好好研究一下
Private Sub CommandButton1_Click()的意思是說
"複製工作表過來時名字應該是和原本工作表一樣的"沒錯
因為我的檔案有很多,而且雖然內容格式一樣但檔名不一樣,我的想法是要怎麼樣
把不同檔名的檔案匯進來之後都可以執行Private Sub CommandButton2_Click(),
因為指定的With Sheets("工作表名稱")工作表名稱會變
等這個都了解了接下來再想下一步怎麼批次
再次謝謝你歐作者: stillfish00 時間: 2013-1-23 15:14
With ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
'依land_no_m, land_no_c欄排序
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
'找出不符合的欄
For j = 1 To .[A1].CurrentRegion.Columns.Count
If IsError(Application.Match(.Cells(1, j).Value, Array("section", "SC", "LANDUSE", "PUBNO", "OPTION", "METHOD", "MUPLAN", "DUPLAN", "ORG_FID"), 0)) Then
If rng Is Nothing Then Set rng = .Columns(j) Else Set rng = Union(rng, .Columns(j))
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作者: stillfish00 時間: 2013-1-25 13:47