2016-11-08 186 views
0

我希望你能幫上忙。我得到錯誤運行時錯誤1004範圍類的自動過濾方法失敗VBA運行時錯誤1004範圍類的自動過濾方法失敗

當我運行我的代碼Public Sub TestThis(),有趣的是它本身完美的作品,但是當我把它放入其他代碼並調用它時,它給出了錯誤運行時錯誤1004 Range類的自動篩選方法失敗

錯誤是發生在這條線

.Range("A:K").AutoFilter Field:=11, Criteria1:="<>", Operator:=xlFilterValues 

就像我說的時候,當把它稱爲蟲子不叫,並通過自身運行沒有問題。任何幫助將不勝感激。

我的代碼如下。

Sub Open_Workbook_Dialog() 

Dim my_FileName As Variant 
Dim my_Workbook As Workbook 

    MsgBox "Pick your CRO file" '<--| txt box for prompt to pick a file 

    my_FileName = Application.GetOpenFilename(FileFilter:="Excel Files,*.xl*;*.xm*") '<--| Opens the file window to allow selection 

    If my_FileName <> False Then 
    Set my_Workbook = Workbooks.Open(Filename:=my_FileName) 

    Call TestThis 

    Call Filter(my_Workbook) '<--|Calls the Filter Code and executes 

    End If 
End Sub 

Public Sub Filter(my_Workbook As Workbook) 
    Dim rCountry As Range, helpCol As Range 
    Dim wb As Workbook 
    With my_Workbook.Sheets(1) '<--| refer to data worksheet 
    With .UsedRange 
     Set helpCol = .Resize(1, 1).Offset(, .Columns.Count) '<--| get a "helper" column just at the right of used range, it'll be used to store unique country names in 
    End With 

    With .Range("A1:Y" & .Cells(.Rows.Count, 1).End(xlUp).Row) '<--| refer to its columns "A:Y" from row 1 to last non empty row of column "A" 
      .Columns(11).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=helpCol, Unique:=True '<-- call AdvancedFilter on 11th column of the referenced range and store its unique values in "helper" column 
      Set helpCol = Range(helpCol.Offset(1), helpCol.End(xlDown)) '<--| set range with unique names in (skip header row) 
      For Each rCountry In helpCol '<--| iterate over unique country names range (skip header row) 
       .AutoFilter 11, rCountry.Value2 '<--| filter data on country field (11th column) with current unique country name 
       If Application.WorksheetFunction.Subtotal(103, .Cells.Resize(, 1)) > 1 Then '<--| if any cell other than header ones has been filtered... 
        Set wb = Application.Workbooks.Add '<--... add new Workbook 
         wb.SaveAs Filename:=rCountry.Value2 '<--... saves the workbook after the country 
          .SpecialCells(xlCellTypeVisible).Copy wb.Sheets(1).Range("A1") 
           ActiveSheet.Name = rCountry.Value2 '<--... rename it 
          .SpecialCells(xlCellTypeVisible).Copy ActiveSheet.Range("A1") 'copy data for country under header 
          Sheets(1).Range("A1:Y1").WrapText = False 'Takes the wrap text off 
          ActiveWindow.Zoom = 55 
         Sheets(1).UsedRange.Columns.AutoFit 'Autofits the column 
        wb.Close SaveChanges:=True '<--... saves and closes workbook 
       End If 
      Next 
     End With 
     .AutoFilterMode = False '<--| remove autofilter and show all rows back 
    End With 
    helpCol.Offset(-1).End(xlDown).Clear '<--| clear helper column (header included) 
End Sub 

Public Sub TestThis() 
Dim wks As Worksheet 

Set wks = ThisWorkbook.Sheets(1) 

With wks 
.AutoFilterMode = False 
.Range("A:K").AutoFilter Field:=11, Criteria1:="<>", Operator:=xlFilterValues 
.Range("A:C").SpecialCells(xlCellTypeBlanks).Interior.Color = 65535 
.AutoFilterMode = False 
End With 
End Sub 

回答

1

請檢查Set wks = ThisWorkbook.Sheets(1)給你你想要工作簿您需要的板材,ThisWorkbook.語句引用工作簿,其中放置宏(VBA項目)。也許你需要將其更改爲

Set wks = ActiveWorkbook.Sheets(1) 

或通過my_WorkbookTestThis()宏。

+0

Limak:令人敬畏的東西我的朋友我不怎麼想我。一組全新的眼睛可以創造奇蹟。非常感謝你對都柏林的尊重:-) –

相關問題