我正在尝试从.csv报告文件中获取一些数据。 Set ClassBook中有错误,因为我猜这只是使用xls或xlsx文件。
路径由用户在单元格中定义,格式类似于C:\ Folder \,其余代码则指定文件名,因为每天都在变化。
Sub PullData()
Application.ScreenUpdating = False
Dim PathFile As String
PathFile = Cells(23, 15)
Dim ClassBook As Workbook
Set ClassBook = PathFile & "class" & Format(Now() + 1, "ddmmyy") & "-booked.csv"
Dim Template As Workbook
Set Template = ActiveSheet
Sheets(Template).Cells(5, 15) = Sheets(ClassBook).Cells(2, 1)
Application.ScreenUpdating = True
End Sub
提前感谢您的帮助!
答案 0 :(得分:0)
如果您尝试打开现有文件,请使用:
Dim ClassBook As Workbook, ClassSheet As Worksheet
Set ClassBook = Workbooks.Open(PathFile & "class" & Format(Now() + 1, "ddmmyy") & "-booked.csv")
Set ClassSheet = ClassBook.ActiveSheet
此外,在您的代码的下方,看起来您正在分配模板(Dim
'为Workbook
到ActiveSheet
,这是{{1}您将要使用此代码:
WorkSheet
然后,向下走,你可以写:
Dim Template As Worksheet
Set Template = ThisWorkbook.Worksheets("SomeSheetName") '<~ put the name of the sheet you want to use as a template here
最后,您可以在Template.Cells(5, 15) = ClassSheet.Cells(2, 1)
作业中更加明确,当用户不可避免地做出您没想到的事情时,这可以为您节省一些调试费用:
PathFile
所以,为了把它全部包起来,这里是我如何写这个的一个例子:
Dim PathFile As String
PathFile = Template.Cells(23, 15) '<~ use Template AFTER you set it as a worksheet