为我的单元格编号并根据年份重置

这是我第一篇关于堆栈溢出和我的第一个关于编程的在线问题。

我正在开发一个程序来logging制造工厂中项目(工作订单)的请求。 由于从Windows XP切换,当前系统正在被删除。

我无法开发我的编号。

代码的格式是:YY – ######。 我目前正在使用:

.Cells(iRow, 1).Value = Format(Date, "yy") & " - " & Format(iRow - 1, "000000") 

所以这将input年份和“iRow” – 1到第一列。 iRow使用以下方法确定:

 iRow = 2 'starting index for next empty WONum cell With WORecordSheet 'Find the next empty WONum cell Do While .Cells(iRow, 1).Value <> "" .Cells(iRow, 1).Activate iRow = iRow + 1 Loop End With 

它循环遍历直到find一个空行,然后使用年份input数字,并将指定的行减1到该单元格中。

我遇到的问题是与今年。 当新的一年到来时,需要切换回第一个工作单。例如:

14 – 001111

14 – 001112

14 – 001113

15 – 000001

15 – 000002

但是,问题是这个数字是用行来做的,所以它不会在新的一年重新开始。 我知道,如果年份相同,我将需要另一个variables,如果年份相同,则将1加到前一个数字上,或者循环到同一年的行中,直到find一个新的行,这个数字代表工单号码,但是input数字的空行应该和以前一样用iRow循环来决定。 我不知道如何才能确定前一年的进入…你能以某种方式将值分成“YY”和“######”两部分吗? 并运行一个if语句,如果“YY”与当前年份相同,那么它会将“######”加1。

感谢您的任何build议!

更新:我创build了一个工作的代码块。 它基于@ user1759942的响应。 但是,我确实参照了暗淡variables的其他反应,并正确使用“右”function。 这里是更新的代码:

 Dim iRow As Long Dim yr As Long Dim lCodeNum As Long WORecordSheet.Activate iRow = 2 'starting index for next empty WONum cell lCodeNum = 1 'starting index for last 6 digits of wonum With WORecordSheet 'Find the next empty WONum cell Do While .Cells(iRow, 1).Value <> "" .Cells(iRow, 1).Activate iRow = iRow + 1 Loop yr = Left(.Cells(iRow - 1, 1).Value, 2) If yr = Format(Date, "yy") Then lCodeNum = Right(.Cells(iRow - 1, 1).Value, 6) + 1 Else lCodeNum = 1 End If End With 

后来:

  .Cells(iRow, 1).Value = Format(Date, "yy") & " - " & Format(lCodeNum, "000000") 

你可以做你想到的。 将YY与其余部分分开并检查。

有一个variables – dim yr as string来保存年份,我会使用一个单独的variables来放置数字: dim lCodeNum as long并在while循环中设置这些variables

 dim yr as string iRow = 2 'starting index for next empty WONum cell lCodeNum = 1 With WORecordSheet 'Find the next empty WONum cell yr = left(.cells(iRow,1).value, 2) 'gets the first 2 characters in the cell Do While .Cells(iRow, 1).Value <> "" .Cells(iRow, 1).Activate iRow = iRow + 1 if not left(.cells(iRow, 1).value, 2) = yr then lCodeNum = 1 else lCodeNum = lCodeNum + 1 end if yr = left(.cells(iRow,1).value, 2) Loop End With 

那么当你将这个值赋给空单元格时,你可以这样做:

  .Cells(iRow, 1).Value = Format(Date, "yy") & " - " & Format(lCodeNum, "000000") 

所以上面的方法,你使用irow遍历每一行,并引用单元格,但使用lCodeNum作为数字,因此它重置为1,每次遇到新的一年。

你可以使用irow而不是lcodenum,但是如果遇到新的一年,你只能放1,然后这些数字会在他们离开的地方回升,所以你最终会得到像

 14 - 000144 14 - 000145 15 - 000001 15 - 000147 

这不是一个100%完整的答案,但希望它会给你一切你需要应用到你自己的程序。 而且,这不是解决这个问题的唯一方法,但它是一种方法。 希望它会给你一个正确的方向推动…

在VBA中获取当前年份的最后两位数字就足够简单了:

 Dim yearDigits As Long yearDigits = Right(Year(Now), 2) 

现在你知道当前的最后两位数字了,你可以通过做一些接近的事情来比较它与YY – ######这样的一些数据string:

 Dim invoice As String yearDigits = Right(year(Now), 2) invoice = "13 - 001111" invoiceYear = Left(invoice, 2) 

把它捆绑在一起可能看起来像这样:

 Sub test() Dim yearDigits As Long Dim invoice As String Dim invoiceYear As Long Dim equalYear As Boolean yearDigits = Right(year(Now), 2) invoice = "13 - 001111" invoiceYear = Left(invoice, 2) equalYear = yearDigits = invoiceYear MsgBox "Current Year = " & yearDigits & ". Invoice Year = " & invoiceYear & ". Are they equal? " & equalYear End Sub 

如果这没有帮助,或者您需要一些帮助将其应用到您的代码,请回发,我会给你更多的轻推。

祝你好运!

这应该在每次运行时在A列中放置一个新的数字代码。 它应该重新启动年份的变化顺序:

 Sub FillId() Dim N As Long, Prev As String, PrevYr As Long, PrevNum As Long Dim Yr As Long, M As Long If Range("A1").Value = "" Then Range("A1").Value = Mid(Year(Date), 3) & " - " & "000001" Exit Sub End If N = Cells(Rows.Count, "A").End(xlUp).Row Prev = Cells(N, "A").Value PrevYr = CLng(Mid(Prev, 1, 2)) PrevNum = CLng(Mid(Prev, 6)) Yr = CLng(Mid(Year(Date), 3)) If Yr <> PrevYr Then M = 1 Else M = PrevNum + 1 End If Cells(N + 1, "A").Value = Mid(Year(Date), 3) & " - " & Format(M, "000000") End Sub