|
こんにちは
多分これで大丈夫だと思います。
Sub Test2()
Dim MyFil As String, Wb As Workbook, Ws As Worksheet
Dim NowWb As Workbook, NowWs As Worksheet, C As Range
MyFil = Application.GetOpenFilename("テキスト ファイル (*.csv), *.csv")
If MyFil = "False" Then Exit Sub
Application.ScreenUpdating = False
Set Ws = ThisWorkbook.Worksheets("Sheet1")
Set NowWb = Workbooks.Add(1)
Set Wb = Workbooks.Open(MyFil)
With Wb.ActiveSheet
.Range("1:1,3:3").Delete Shift:=xlUp
.Columns(8).AdvancedFilter xlFilterCopy, , Ws.Range("A1"), True
If .AutoFilterMode = False Then
.Rows(1).AutoFilter
End If
For Each C In Ws.Range("A2", Ws.Range("A65536").End(xlUp)).SpecialCells(xlCellTypeConstants)
If Not IsEmpty(C.Value) Then
.Columns(8).AutoFilter 8, C.Value
If .Range("H65536").End(xlUp).Row > 1 Then
Set NowWs = NowWb.Worksheets.Add
NowWs.Name = C.Value
.AutoFilter.Range.Copy NowWs.Range("A1")
With NowWs
.Rows(1).Delete
.UsedRange.Sort Key1:=.Range("A1"), Order1:=xlAscending, Key2:=.Range("B1") _
, Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False _
, Orientation:=xlTopToBottom, SortMethod:=xlPinYin, DataOption1:=xlSortNormal _
, DataOption2:=xlSortTextAsNumbers
.Columns("A:B").Delete
.Cells.EntireColumn.AutoFit
End With
Set NowWs = Nothing
End If
End If
Next C
.AutoFilterMode = False
End With
Ws.Columns(1).Clear
Wb.Close False
With Application
.DisplayAlerts = False
NowWb.Sheets("Sheet1").Delete
.DisplayAlerts = True
.ScreenUpdating = True
End With
Set NowWb = Nothing: Set Ws = Nothing: Set Wb = Nothing
End Sub
|
|