Excel VBA = Workbook.Open(“文件名”)错误

时间:2019-04-18 07:12:33

标签: excel vba worksheet-function autofilter

我正在尝试运行一个宏,我可以选择一个Excel文件,在该文件中可以过滤日期和副本(在特定工作表中)并将数据粘贴回活动工作簿(在特定工作表中)。

我尝试过各种关于workbook.open错误的论坛,但仍然无法解决我的公式。

Sub CopyFilteredValuesToActiveWorkbook()

Dim wbSource As Workbook, wbDest As Workbook
Dim wsSource As Worksheet, wsDest As Worksheet
Dim rngSource As Range, rngDest As Range
Dim Fname As String
Dim strName As String 'for filter


Fname = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls*), *.xls*", Title:="Select a File")
If Fname = "False" Then Exit Sub
Set wbSource = Workbooks.Open(Fname) 'ERROR POINTS THIS LINE

Set wsSource = wbSource.Worksheets("Table 1")

strName = InputBox("Input Year")
wsSource.Range("A:A").AutoFilter Field:=3, Criteria1:="=* & strName & *", Operator:=xlAnd
Set rngSource = wsSource.Range("A:K")

Set wbDest = ThisWorkbook
Set wsDest = wbDest.Worksheets("Sheet3")
Set rngDest = wsDest.Range("A:K")

rngDest.Value = rngSource.Value

wbSource.Close (False) 'Close without saving changes

End Sub

0 个答案:

没有答案