我的计划是将电子表格与数据链接到模板 - 每行数据应创建一个单独的文件并保存。我已经集成了一个单击按钮来执行此操作,但它只打开模板并且不输入数据,也不保存它。我想我错过了什么。
Private Sub CommandButton21_Click()
' First we define a few variables to capture data from our Excel worksheet
Dim r As Long
Dim mydate As String
Dim path As String
Dim myfilename As String
Dim lastrow As Long
Dim wb1 As Excel.Workbook
Set wb1 = Workbooks.Open("C:\Benefits\BasicInvoice.xlsx")
' our last row of data in the worksheet is defined
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
' we start at row 2 since the first row contains headers
r = 2
' Looping process starts
For r = 2 To lastrow
' If the value under the header ‘Note’ is done then the data in the row is not processed and jump to the label nextrow
If Cells(r, 17).Value = “done” Then GoTo nextrow
' we map excel worksheet data to the variables
companycode = Sheets(“GeneralList”).Cells(r, 3).Value
profitcentre = Sheets(“GeneralList”).Cells(r, 4).Value
vendorno = Sheets(“GeneralList”).Cells(r, 5).Value
vendorname = Sheets(“GeneralList”).Cells(r, 6).Value
reference = Sheets(“GeneralList”).Cells(r, 7).Value
employeename = Sheets(“GeneralList”).Cells(r, 8).Value
Frequency = Sheets(“GeneralList”).Cells(r, 9).Value
Date = Sheets(“GeneralList”).Cells(r, 10).Value
curr = Sheets(“GeneralList”).Cells(r, 11).Value
amount = Sheets(“GeneralList”).Cells(r, 12).Value
Cells(r, 13).Value = “done”
Application.DisplayAlerts = False
' Workbooks.Open Filename:="BasicInvoice.xlsx"
Workbooks.Open "BasicInvoice.xlsx"
' Worksheets(“BasicInvoice”).Select
ActiveWorkbook.Sheets(“BasicInvoice”).Activate
' now we map the variables to the invoice worksheet data
wb1.Sheets(“BasicInvoice”).Cells(“B8”).Value = vendorno
wb1.Sheets(“BasicInvoice”).Cells(“B9”).Value = vendorname
wb1.Sheets(“BasicInvoice”).Cells(“B10”).Value = curr
wb1.Sheets(“BasicInvoice”).Cells(“B11”).Value = amount
wb1.Sheets(“BasicInvoice”).Cells(“B12”).Value = Frequency
wb1.Sheets(“BasicInvoice”).Cells(“B13”).Value = reference
wb1.Sheets(“BasicInvoice”).Cells(“B14”).Value = employeename
wb1.Sheets(“BasicInvoice”).Cells(“A18”).Value = "272240"
wb1.Sheets(“BasicInvoice”).Cells(“B18”).Value = profitcentre
wb1.Sheets(“BasicInvoice”).Cells(“C18”).Value = companycode
wb1.Sheets(“BasicInvoice”).Cells(“D18”).Value = Frequency
wb1.Sheets(“BasicInvoice”).Cells(“E18”).Value = Date
wb1.Sheets(“BasicInvoice”).Cells(“F18”).Value = amount
wb1.Sheets(“BasicInvoice”).Cells(“F20”).Value = amount
path = "C:\Benefits\"
mydate = Date
mydate = Format(mydate, “mm_dd_yyyy”)
' File is saved as a read-only file
ActiveWorkbook.SaveAs Filename:=path & curr & “ - ” & vendorname & “ - ” & mydate & “.xlsx”
myfilename = ActiveWorkbook.FullName
SetAttr myfilename, vbReadOnly
Application.DisplayAlerts = True
' ActiveWorkbook.PrintOut copies:=1
' We close the active workbook. We don’t need to save it because we just saved it!
ActiveWorkbook.Close SaveChanges:=True
' our label next row. Labels have a colon after their name
nextrow:
Next r
End Sub