VBA 匹配单元格中的E、N文本,替换写测试用例编号

'匹配单元格中的E、N文本,替换写测试用例编号
Sub GetColumnBText()
Dim text As String
Dim targetN As String
Dim targetE As String
Dim i, j, n, e As Integer
Dim positionN As Integer
Dim positionE As Integer

Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("sheet名") '修改为实际的工作表名称
targetN = "N"
targetE = "E"
n = 1
e = 1

For i = 6 To 325
    'For j = 5 To 5
        text = ws.Cells(i, 6).Value
        positionN = 0
        positionE = 0
        positionN = InStr(text, targetN)
        positionE = InStr(text, targetE)
        If positionN > 0 Then
            If n < 10 Then
                ws.Cells(i, 6).Value = "N-00" & n
                n = n + 1
             ElseIf n >= 10 And n < 100 Then
                ws.Cells(i, 6).Value = "N-0" & n
                n = n + 1
             Else
                ws.Cells(i, 6).Value = "N-" & n
                n = n + 1
             End If
             
        ElseIf positionE > 0 Then
           If e < 10 Then
                ws.Cells(i, 6).Value = "E-00" & e
                ws.Cells(i, 6).Interior.ColorIndex = 44
                e = e + 1
             ElseIf e >= 10 And e < 100 Then
                ws.Cells(i, 6).Value = "E-0" & e
                ws.Cells(i, 6).Interior.ColorIndex = 44
                e = e + 1
             Else
                ws.Cells(i, 6).Value = "E-" & e
                ws.Cells(i, 6).Interior.ColorIndex = 44
                e = e + 1
             End If
        Else
            ws.Cells(i, 6).Value = ""
        End If
    'Next
Next
MsgBox "完了"

End Sub

posted on 2024-08-31 13:35  yffs168169  阅读(7)  评论(0编辑  收藏  举报

导航