2016-09-20 61 views
1

一部の解析を使用しようとしていますが、少し微調整できました。私はそれをストレートVBAのExcelで使用する場合、それは正常に動作します。私はVB.NETでモジュールと同じコードを使用する場合しかし、私は(コードのラインRangeクラスのAutoFilterメソッドがVB.NETで失敗しました

ws.Range(vTitles).AutoFilter()

にタイトルのエラーを取得します私はハードコアVB.Netプログラマーではないので、変換で何が起こっているのか分からないので、私はたくさんのグーグルでやっていますが、それほど多くは見つけられません。どのようにこれを修正することができますか、またはVB.Netでこのスニペットを使用するアイデアを放棄する必要がありますかに関する任意のアイデアですか?ここで

は、私が使用していたコードです:あなたは少なくとも一つのオプションのパラメータを指定する必要がありますように

'turned strict off or autofilter per  http://www.pcreview.co.uk/threads/autofilter-method-of-range-class-failed.3994483/ 
Option Strict Off 
Imports xl = Microsoft.Office.Interop.Excel 

Module ParseItems 

Public Sub ParseItems(ByRef fileName As String) 
    'Jerry Beaucaire (4/22/2010) 
    'Based on selected column, data is filtered to individual workbooks are named for the value plus today's date 
    Dim wb As xl.Workbook 
    Dim xlApp As xl.Application 
    Dim LR As Long, Itm As Long, MyCount As Long, vCol As Long 
    Dim ws As xl.Worksheet, MyArr As Object, vTitles As String, SvPath As String 

    'Set new application and make wb visible 
    xlApp = New xl.Application 
    xlApp.Visible = True 

    'open workbook 
    wb = xlApp.Workbooks.Open(fileName) 


    'Sheet with data in it 
    ws = wb.Sheets("Original Data") 

    'Path to save files into, remember the final "\" 
    SvPath = "G:\MC VBA test\" 

    'Range where titles are across top of data, as string, data MUST have titles in this row, edit to suit your titles locale 
    vTitles = "A1:L1" 

    'Choose column to evaluate from, column A = 1, B = 2, etc. 
    vCol = xlApp.InputBox("What column to split data by? " & vbLf & vbLf & "(A=1, B=2, C=3, etc)", "Which column?", 1, Type:=1) 
    If vCol = 0 Then Exit Sub 

    'Spot bottom row of data 
    LR = ws.Cells(ws.Rows.Count, vCol).End(xl.XlDirection.xlUp).Row 

    'Speed up macro execution 
    'Application.ScreenUpdating = False 

    'Get a temporary list of unique values from key column 
    ws.Columns(vCol).AdvancedFilter(Action:=xl.XlFilterAction.xlFilterCopy, CopyToRange:=ws.Range("EE1"), Unique:=True) 

    'Sort the temporary list 
    ws.Columns("EE:EE").Sort(Key1:=ws.Range("EE2"), Order1:=xl.XlSortOrder.xlAscending, Header:=xl.XlYesNoGuess.xlYes, _ 
     OrderCustom:=1, MatchCase:=False, Orientation:=xl.Constants.xlTopToBottom, DataOption1:=xl.XlSortDataOption.xlSortNormal) 

    'Put list into an array for looping (values cannot be the result of formulas, must be constants) 
    MyArr = xlApp.WorksheetFunction.Transpose(ws.Range("EE2:EE" & ws.Rows.Count).SpecialCells(xl.XlCellType.xlCellTypeConstants)) 

    'clear temporary worksheet list 
    ws.Range("EE:EE").Clear() 

    'Turn on the autofilter, one column only is all that is needed 
    ws.Range(vTitles).AutoFilter() 

    'Loop through list one value at a time 
    For Itm = 1 To UBound(MyArr) 
     ws.Range(vTitles).AutoFilter(Field:=vCol, Criteria1:=MyArr(Itm)) 

     ws.Range("A1:A" & LR).EntireRow.Copy() 
     xlApp.Workbooks.Add() 
     ws.Range("A1").PasteSpecial(xl.XlPasteType.xlPasteAll) 
     ws.Cells.Columns.AutoFit() 
     MyCount = MyCount + ws.Range("A" & ws.Rows.Count).End(xl.XlDirection.xlUp).Row - 1 

     xlApp.ActiveWorkbook.SaveAs(SvPath & MyArr(Itm), xl.XlFileFormat.xlWorkbookNormal) 
     'ActiveWorkbook.SaveAs SvPath & MyArr(Itm) & Format(Date, " MM-DD-YY") & ".xlsx", 51 'use for Excel 2007+ 
     xlApp.ActiveWorkbook.Close(False) 

     ws.Range(vTitles).AutoFilter(Field:=vCol) 
    Next Itm 

    'Cleanup 
    ws.AutoFilterMode = False 
    MsgBox("Rows with data: " & (LR - 1) & vbLf & "Rows copied to other sheets: " & MyCount & vbLf & "Hope they match!!") 
    xlApp.Application.ScreenUpdating = True 
End Sub 



End Module 
+0

あなただけの範囲で 'vTitles'を交換する場合はどうなりますか?また、この 'ws.Range(vTitles).AutoFilter(Nothing、Operator:= Excel.XlAutoFilterOperator.xlFilterValues) 'を試すことができますか? – Codexer

+0

@Zagglerはい、同じエラーが発生しました。あなたのスニペットをその場所で試してみましたが、同じエラーも受けました。 – Darw1n34

+0

'ws.Range(vTitles).AutoFilter(Field:= 1)'のように見えます。 –

答えて

1

が見えます。これを試してみてください:

ws.Range(vTitles).AutoFilter(Field:=1)

関連する問題