以下是應用過濾器後複製數據的代碼。錯誤1400:沒有滿足標準的此類單元格
Sub read_excel_file(path_to_current_work_book As String, path_to_destination_workbook As String)
Dim work_book As Object
Dim destination_workbook As Object
Dim i, m As Integer
Dim array_of_account_numbers() As Variant
Dim array_of_debit_or_credits() As Variant
Dim current_sheets As Worksheet
Dim buf_rng As Range
array_of_account_numbers = Array("1400", "1401", "1402", "1403", "1410", "1411", "1412", "1413", "1414", "1420", "1421", "1422", "1423", "1424", "1430", "1440")
array_of_debit_or_credits = Array("10", "11", "20", "21")
Application.DisplayAlerts = False
Application.Visible = True
Set work_book = Workbooks.Open(path_to_current_work_book)
Set destination_workbook = Workbooks.Open(path_to_destination_workbook)
destination_workbook.Sheets(1).Cells(1, 1).Value = "Debit(10,11)/Credit(20, 21)"
destination_workbook.Sheets(1).Cells(1, 2).Value = "Balance account number"
destination_workbook.Sheets(1).Cells(1, 3).Value = "Currency code"
destination_workbook.Sheets(1).Cells(1, 4).Value = "Resident"
destination_workbook.Sheets(1).Cells(1, 5).Value = "Amount"
destination_workbook.Sheets(1).Cells(1, 6).Value = "Date"
m = 2
For i = 1 To work_book.Worksheets.Count
With work_book.Sheets(i)
If (.UsedRange.Rows.Count > 1) Then
.UsedRange.AutoFilter Field:=1, Criteria1:=array_of_debit_or_credits, Operator:=xlFilterValues
.UsedRange.AutoFilter Field:=2, Criteria1:=array_of_account_numbers, Operator:=xlFilterValues
m = destination_workbook.Sheets(1).Range("A" & Rows.Count).End(xlUp).row + 1
.AutoFilter.Range.Offset(1).Resize(.AutoFilter.Range.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Copy destination_workbook.Sheets(1).Range("A" & m)
End If
End With
Next i
work_book.Close savechanges:=False
destination_workbook.Close savechanges:=True
End Sub
它產生以下錯誤(當autofiltered範圍,不包括報頭,是空的):「錯誤1400:不存在這樣的細胞,其saticfies規定 - 」。
.AutoFilter.Range.Offset(1).Resize(.AutoFilter.Range.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Copy destination_workbook.Sheets(1).Range("A" & m)
我該如何處理這個錯誤?
謝謝您的回覆。我測試了你的建議解決方案。但是,現在代碼在下面一行中產生如下錯誤:'424:Object required':'If Not(Rng)Nothing Then''。你能否建議如何糾正這個錯誤? –
您是否在代碼的頂部定義了'Dim Rng as Range'? –
我不使用'Option Explicit',所以我沒有定義它。 –