我的工作簿下面有以下代码,可以为我保存工作簿,并根据特定单元格中的值将发票编号增加1:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Range("L5").Value = Range("L5").Value + 1
ThisWorkbook.Save
End Sub
所以单元格L5从1开始,现在每次关闭工作簿时它都会为它添加+1并保存书籍,因此下次打开时它将是2,3,4,5等等。
我遇到的问题是,如果有人关闭它并打开和关闭它,即使该发票没有退出,也会继续添加数字。
无论如何它可以检查工作表2中列的值列表以查看当前发票编号是否存在,如果存在则添加1并保存,如果没有则保留相同的数字并保存?
更新的提交代码,包括清除,保存和提升发票号码。除了refTable中的单元格之外,所有单元格都被锁定和保护,用户可编辑这些单元格,但L5已被锁定,只需要由VBA代码编辑,而不是用户。
Sub Submit()
Dim refTable As Variant, trans As Variant
refTable = Array("B = L5", "C = C5", "D=G5", "E=C10", "F=C9", "G=I9", "H=I10", "I=C13", "J=C14", "K=C15", "L=C16", "M=C17", "N=C18", "O=I13", "P=I14", "Q=I15", "R=I16", "S=I17", "W=H20")
Dim Row As Long
Row = Worksheets("TravelLog").UsedRange.Rows.Count + 1
For Each trans In refTable
Dim Dest As String, Field As String
Dest = Trim(Left(trans, InStr(1, trans, "=") - 1)) & Row
Field = Trim(Right(trans, Len(trans) - InStr(1, trans, "=")))
Worksheets("TravelLog").Range(Dest).Value = Worksheets("TravelRequest").Range(Field).Value
Next
If Worksheets("TravelRequest").CheckBox1.Value Then
Worksheets("TravelLog").Range("T" & Row).Value = "Yes"
Else
Worksheets("TravelLog").Range("T" & Row).Value = "No"
End If
If Worksheets("TravelRequest").CheckBox2.Value Then
Worksheets("TravelLog").Range("U" & Row).Value = "Yes"
Else
Worksheets("TravelLog").Range("U" & Row).Value = "No"
End If
If Worksheets("TravelRequest").CheckBox3.Value Then
Worksheets("TravelLog").Range("V" & Row).Value = "Yes"
Else
Worksheets("TravelLog").Range("V" & Row).Value = "No"
End If
Range("L5").Value = Range("L5").Value + 1
Range("I9:I10, I13:I17, H20, C5, C9:C10, C13:C18").Select
Selection.ClearContents
Dim OleObj As OLEObject
For Each OleObj In ActiveSheet.OLEObjects
If OleObj.progID = "Forms.CheckBox.1" Then
OleObj.Object = False
End If
Next OleObj
ThisWorkbook.Save
End Sub
答案 0 :(得分:0)
在Workbook_BeforeClose中使用goto,条件是你提到的那样
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If 'your_condition' = TRUE Then goto do_save
Range("L5").Value = Range("L5").Value + 1
do_save:
ThisWorkbook.Save
End Sub