从excel中的多行生成多个excel文件

时间:2016-04-07 08:32:42

标签: excel vba excel-vba

我正在尝试从excel中的列表生成多个Excel文件。下面是我尝试的代码,但是得到了运行时错误70。

我使用的excel(可以在这里下载:https://drive.google.com/folderview?id=0B7u1K6cUEOzeWURZWWd3NjQ4R0k&usp=sharing

1)BasicInvoice.xlsx 2)2011.xlsx

Private Sub CommandButton1_Click()

Dim Name As String
Dim invoicenumber As Long
Dim r As Long
Dim path As String
Dim myfilename As String
lastrow = Sheets("1").Range("A" & Rows.Count).End(xlUp).Row
r = 2
For r = 2 To lastrow

Date = Sheets("1").Cells(r, 1).Value
invoicenumber = Sheets("1").Cells(r, 2).Value
Name = Sheets("1").Cells(r, 3).Value
Description = Sheets("1").Cells(r, 4).Value
Amount = Sheets("1").Cells(r, 5).Value

Workbooks.Open ("BasicInvoice.xlsx")
ActiveWorkbook.Sheets("BasicInvoice").Activate
ActiveWorkbook.Sheets("BasicInvoice").Range("E9").Value = Date
ActiveWorkbook.Sheets("BasicInvoice").Range("E10").Value = invoicenumber
ActiveWorkbook.Sheets("BasicInvoice").Range("B9").Value = Name
ActiveWorkbook.Sheets("BasicInvoice").Range("B16").Value = Description
ActiveWorkbook.Sheets("BasicInvoice").Range("E16").Value = Amount

path = "C:\invoices\"
ActiveWorkbook.SaveCopyAs Filename:=path & invoicenumber & ".xlsx"
myfilename = ActiveWorkbook.FullName
Application.DisplayAlerts = True
ActiveWorkbook.PrintOut copies:=1
ActiveWorkbook.Close SaveChanges:=False

nextrow:

Next r

End Sub

1 个答案:

答案 0 :(得分:0)

运行时错误70表示您无法写入写保护的内容。您收到错误的是哪一行?

检查以确保硬盘驱动器上确实存在C:/ Invoices文件夹。

以下是我的代码整理过程。

trainControl