2016-05-06 6 views
1

if文と.autofilterを使って、読んでいるテキストファイルの最初の2桁が何であるかによって異なる列で検索します。私の問題は、テキストファイルの最初の行の対応する列を検索し、ループを実行するたびに同じ列を検索することです。これは私が持っているものです:オートフィルタエラー(間違った列にある)

Dim myFile As String 

myFile = Application.GetOpenFilename() 

Open myFile For Input As #1 

Dim source As String 
Dim destination As Range 

Dim rowNum As Long 

Set destination = Sheets("SHOULDER").Range("A2") 

Do Until EOF(1) 

    Line Input #1, source 

    If Mid(source, 1, 2) = "09" Then 

     With destination.Columns("B:C") 
      .AutoFilter field:=1, Criteria1:=Mid(source, 3, 4) 
      .AutoFilter field:=2, Criteria1:=Mid(source, 7, 4) 
     End With 

     rowNum = Range("A" & Rows.Count).End(xlUp).row 
     Cells(rowNum, 8).Value = Mid(source, 23, 2) & " " & Mid(source, 21, 2) & " " & Mid(source, 19, 2) 
     Cells(rowNum, 9).Value = Mid(source, 11, 2) & "/" & Mid(source, 13, 2) & "/" & Mid(source, 15, 4) 

    ElseIf Mid(source, 1, 2) = "15" Then 

     With destination.Columns("L:M") 
      .AutoFilter field:=1, Criteria1:=Mid(source, 3, 4) 
      .AutoFilter field:=2, Criteria1:=Mid(source, 7, 4) 
     End With 

     rowNum = Range("A" & Rows.Count).End(xlUp).row 
     Cells(rowNum, 18).Value = Mid(source, 23, 2) & " " & Mid(source, 21, 2) & " " & Mid(source, 19, 2) 
     Cells(rowNum, 19).Value = Mid(source, 11, 2) & "/" & Mid(source, 13, 2) & "/" & Mid(source, 15, 4) 

    ElseIf Mid(source, 1, 2) = "23" Then 

     With destination.Columns("V:W") 
      .AutoFilter field:=1, Criteria1:=Mid(source, 3, 4) 
      .AutoFilter field:=2, Criteria1:=Mid(source, 7, 4) 
     End With 

     rowNum = Range("A" & Rows.Count).End(xlUp).row 
     Cells(rowNum, 28).Value = Mid(source, 23, 2) & " " & Mid(source, 21, 2) & " " & Mid(source, 19, 2) 
     Cells(rowNum, 29).Value = Mid(source, 11, 2) & "/" & Mid(source, 13, 2) & "/" & Mid(source, 15, 4) 

    End If 
Loop 
    Selection.AutoFilter 

ご協力いただければ幸いです!

答えて

0

適用されたRange.AutoFilter Methodを削除してから、次の行にループする前にのソースに読み込む必要があります。適用された.AutoFilter 外部のをループ内ではなくキャンセルしています。

With Worksheets("SHOULDER") 
    With .Range(.Cells(2, "A"), .Cells(Rows.Count, "W").End(xlUp)) 
     Do Until EOF(1) 
      Line Input #1, Source 
      Select Case Mid(Source, 1, 2) 
       Case "09" 
        .AutoFilter field:=2, Criteria1:=Mid(Source, 3, 4) 
        .AutoFilter field:=3, Criteria1:=Mid(Source, 7, 4) 
       Case "15" 
        .AutoFilter field:=12, Criteria1:=Mid(Source, 3, 4) 
        .AutoFilter field:=13, Criteria1:=Mid(Source, 7, 4) 
       Case "23" 
        .AutoFilter field:=22, Criteria1:=Mid(Source, 3, 4) 
        .AutoFilter field:=23, Criteria1:=Mid(Source, 7, 4) 
       Case Else 
        'do nothing 
      End Select 

      rowNum = .Range("A" & Rows.Count).End(xlUp).Row 
      .Cells(rowNum, 28).Value = Mid(Source, 23, 2) & " " & Mid(Source, 21, 2) & " " & Mid(Source, 19, 2) 
      .Cells(rowNum, 29).Value = Mid(Source, 11, 2) & "/" & Mid(Source, 13, 2) & "/" & Mid(Source, 15, 4) 

      'this is what was missing - reset the .AutoFilter in prep for the next one 
      If .Parent.AutoFilterMode Then .Parent.AutoFilterMode = False 
     Loop 
    End With 
End With 

ロジックを書き換えてSelect Case statementを使用し、冗長コードを減らしました。完全に正しいとは限りませんが、ループ内の次のものを受け入れるために.AutoFilterをリセットした場所を確認してください。

+0

ああ私の良さ。どうもありがとうございます! – user6300479

関連する問題