2017-10-07 67 views
2

我想给宏中的初始数字(在此示例中为T00101),并且如果满足条件(在列C具有“良好”的值),所以如果列B中的单元格没有变化,则将其增加1,如果列中有变化,则将增加100(从先前名称的第一次出现开始计数) B.编号取决于更改另一列中的单元格值 - vba代码

如果在列C中该单词为“假”,则A中的单元格将保持空。 这是该词的第一次出现总是以01结尾,该词的第二次出现以02结尾。如果名称中有变化,则该数字的前三位数字增加1。牛逼信)

我想这是一个结果:

A  B  C 
Id  Name Status 
T00101 Apple good 
T00102 Apple good 
T00103 Apple good 
T00201 Peach good 
T00301 Orange good 
     Banana false 
T00401 Grapes good 
T00402 Grapes good 

我有这个,但它不能正常工作......为宏任何其他建议?

Sub numbering() 
    Sheet1.Select 
    Dim Start As Long 
    Dim Piece As String 
    Dim lastrow As Long 
    Dim erow As Long 
    lastrow = Sheet1.Cells(Rows.Count, 2).End(xlUp).Row 
    erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row 
    Start = 00101 
    Piece = 0 
    For i = erow To lastrow 
     If Cells(i, 3) = "good" Then 
      Sheet1.Cells(i, 1) = Start 
      If Cells(i, 2) = Cells(i + 1, 2) Then 
       Piece = Piece + 1 
       Start = Start + 1 
      Else: 
       Sheet1.Cells(i, 1) = Start - Piece + 100 
      End If 
     End If 
    Next i 
End Sub 
+0

在发布代码,请使用缩进,使之可读。 –

+0

'它不能正常工作'并没有以任何有用的方式描述问题 – jsotola

回答

0

这给一试:

Sub FillA() 
    Dim SeqNum As Long, iNum As Long 
    Dim N As Long, i As Long 

    SeqNum = 100 
    iNum = 1 
    Range("A2").Value = "T" & Format(SeqNum + iNum, "00000") 
    N = Cells(Rows.Count, "B").End(xlUp).Row 

    For i = 3 To N 
     If Cells(i, "C").Value = "good" And Cells(i, "B").Value = Cells(i - 1, "B").Value Then 
      iNum = iNum + 1 
      Cells(i, "A").Value = "T" & Format(SeqNum + iNum, "00000") 
     End If 

     If Cells(i, "C").Value = "good" And Cells(i, "B").Value <> Cells(i - 1, "B").Value Then 
      iNum = 1 
      SeqNum = SeqNum + 100 
      Cells(i, "A").Value = "T" & Format(SeqNum + iNum, "00000") 
     End If 

     If Cells(i, "C").Value <> "good" Then 
      Cells(i, "A") = "" 
     End If 
    Next i 

End Sub 
+0

非常感谢你!它很棒,它确实是我想要的。 – Hunga

0

这应该做的伎俩,我已经添加了一些注释来解释:

Sub numbering() 
Sheet1.Select 
' You need to treat the Id as a String, 
' but also create Integers to keep track of the numbering components 
Dim theId As String 
Dim idLetter As String 
Dim txt_idCategory As String 
Dim txt_idNumber As String 
Dim idCategory As Integer 
Dim idNumber As Integer 
Dim firstRow As Integer 
Dim lastRow As Integer 

    firstRow = 2 ' sets the first row to start updating the Id 
    lastRow = Sheet1.Cells(Rows.Count, 2).End(xlUp).Row ' finds the last row 
    idLetter = "T" ' set your letter 
    idNumber = 1 ' first Id number will always be 1 

For i = firstRow To lastRow ' loop through the rows 
    If Cells(i, 3).Value <> "good" Then ' skip the row if Status not equal "good" 
     GoTo nextOne 
    End If 

    If Cells(i, 2).Value <> Cells(i - 1, 2).Value Then ' if the name changes (or it's the first one) 
     idCategory = idCategory + 1 ' increment the category 
     idNumber = 1 ' and increment the number 
    Else ' otherwise 
     idNumber = idNumber + 1 ' just increment the number 
    End If 

    Select Case Len(CStr(idCategory)) ' based on the length of the category, append "0's" to the front 
     Case 1 
      txt_idCategory = "00" & CStr(idCategory) 
     Case 2 
      txt_idCategory = "0" & CStr(idCategory) 
     Case 3 
      txt_idCategory = CStr(idCategory) 
     Case Else 
      MsgBox "Category over 3 digits not catered for, macro will end" 
      Exit Sub 
    End Select 

    Select Case Len(CStr(idNumber)) ' based on the length of the number, append "0's" to the front 
     Case 1 
      txt_idNumber = "0" & CStr(idNumber) 
     Case 2 
      txt_idNumber = CStr(idNumber) 
      MsgBox "Id Number over 2 digits not catered for, macro will end" 
      Exit Sub 
    End Select 

    theId = idLetter & txt_idCategory & txt_idNumber ' put all 3 parts together 
    Cells(i, 1).Value = theId ' update the cell with the id 

nextOne: 
Next i 

End Sub 
+0

Gary的学生看起来像我在Excel中创建我的代码时所回答的。我喜欢Gary的回答,因为它更加简洁,但是如果可能会有超过999个不同的名称(例如苹果,桃子)或99个以上的名称相同(例如Apple出现在100多行中),您需要考虑这个。 – ACCtionMan

+0

非常感谢你的代码,我也非常感谢你的解释。 – Hunga

+0

@Hunga快乐 – ACCtionMan

相关问题