VBA打开文件错误
问题描述:
我的工作簿上有一个名为CTAttendanceProgram的宏。在我的代码中,我试图使用浏览功能来打开其他工作簿。我想将这个附加文件的Sheet1放在原始“CTAttendanceProgram”中的“员工数据”表之后。这里是我的代码到目前为止:VBA打开文件错误
Dim fn
fn = Application.GetOpenFilename
If fn = False Then
MsgBox "Nothing Chosen"
Exit Sub
Else
Workbooks.Open (fn)
Workbooks("CTAttendanceProgram.xlsm").Activate
End If
Application.DisplayAlerts = False
Worksheets("Recent Attendance").Delete
Application.DisplayAlerts = Trueppl
Workbooks(fn).Sheets(1).Copy After:=Workbooks("CTAttendanceProgram.xlsm").Sheets("Employee Data")
Sheets(4).Name = "Recent Attendance"
以前,我用这个代码来打开预定义的文件,它工作得很好。但是,现在我正在使用浏览功能(但仍然打开相同的文件),我遇到了“Workbooks(fn).Sheet(1).Copy ...”(倒数第二行)错误)。任何提示/建议?预先感谢您的支持。
答
因为fn用于定义文件路径&文件名不能用作工作簿。
做到以下几点:
Dim fn
Dim wb As Workbook
fn = Application.GetOpenFilename
If fn = False Then
MsgBox "Nothing Chosen"
Exit Sub
Else
Workbooks.Open (fn)
Set wb = ActiveWorkbook
Workbooks("CTAttendanceProgram.xlsm").Activate
Application.DisplayAlerts = False
Worksheets("Recent Attendance").Delete
Application.DisplayAlerts = True
wb.Sheets(1).Copy After:=Workbooks("CTAttendanceProgram.xlsm").Sheets("Employee Data")
Sheets(4).Name = "Recent Attendance"
wb.Close False
End If
谢谢!然而,当我设置wb_name = ActiveWorkbook.Name时,我尝试了这个代码出错。任何想法这可能是由什么造成的? – bmyers
我的错误是我现在调整了anwser中的代码。使用activeworkbook.name来检查正确性。它必须是当然设置wb = ActiveWorkbook – DragonSamu
我感谢您的帮助,但代码仍然不适合我。我遇到了我之前遇到的同样的问题,它在将新工作表复制到CTA出勤工作簿的行中出现类型不匹配错误。我不确定什么是错误的,因为所有的数据类型看起来都是正确的,我不能发现任何拼写错误的函数。 – bmyers