用VBA在excel表中自動化生成測試用例數據(異常)

最近在做銀行接口方面測試,同時也引入了自動化測試執行,自動化執行大家都已實現,但人工維護測試數據繁瑣,如某一個字段,字符類型,長度爲10,就單這一個字段,異常測試數據就可以達到15條之多,如果一個接口有幾十個字段,只維護這類型數據就得花很長時, 基於此,採用根據段長度,類型自動生成測試用例及數據。並把異常測試數據的項自動標紅,以便於後期維護。

代碼如下:

 

Sub startMakeTestData()
' 用於循環依次生成反向測試用例數據

'
    initSheet = 5 ' 開始生成的sheet頁數
    intcount = Sheets.Count ' 工作表的總數
    
    ' --debug 用於調試數據
    intcount = 5
        
    For i = initSheet To intcount
        Sheets(i).Select
         main
    Next
End Sub


 Sub main()
'
' 根據一條正向測試用例數據,自動生成反向測試用例數據。主要以長度、類型自動生成反向測試用例數據;
'
' Author : 劉林
' Date   : 2012-5-20
'
    
    Dim columnlen ' 列數量
    columnlen = getcolumnlen - 1 ' 最後一列爲測試執行結果,不使用
    Randomize
    
    ' debug  -- 調試多行
    ' columnlen = 6
     
     Row = 6 ' 從第幾行開始生成數據
     sourcerow = 5 ' 源數據行
     curRow = 5 ' 當前行
     typeFlag = 0 ' 1-數字小數, 2-數字整型, 3-字符
     typerow = 4 ' 描述類型的行;
     desclen = 3 ' 描述長度的行
     intlen = 0 ' 當爲小數時,整數部份長度
     decLen = 0 ' 當爲小數時,小數部份長度
    
    
    ' --------------- 從第6列 到 最後列,做用例數據生成 ---------
    For Column = 6 To columnlen
        ' 看是否爲 X-字符型,9-數字型,9.99-金額型(保留兩位小數)

        If IsNumeric(Cells(typerow, Column).Value) Then  ' 步驟 1. 先看是否爲數字型
            If InStr(Cells(typerow, Column), ".") Then ' 步驟 2. 判定是否爲小數 利用 查詢小數點作判斷
                typeFlag = 1 ' 小數
                ' 拆分整數部份與小數部份
                intlen = Mid(Cells(desclen, Column), 1, InStr(Cells(desclen, Column), ".") - 1)
                decLen = Mid(Cells(desclen, Column), InStr(Cells(desclen, Column), ".") + 1)
            Else  ' 步驟 3. 前2條件已判斷是否爲整數,小數,則是整數
                typeFlag = 2 ' 整數
            End If
        Else ' 不是數字,就是字符
            typeFlag = 3 ' 字符
        End If
        
        ' 根據不同類型生成不同測試數據
        If (Int(curRow) > 1) And (Int(curRow) < 65535) Then  ' 防止超出範圍出錯
            Select Case typeFlag
                Case 1 ' 1-數字小數
                    curRow = makeFloatTestData(curRow, Column, sourcerow, intlen, decLen)
                Case 2 ' 2-數字整型  -- 已完成 2012-5-20
                    curRow = makeIntTestData(curRow, Column, sourcerow, typeFlag)
                Case 3 ' 3-字符
                    curRow = makeCharTestData(curRow, Column, sourcerow, typeFlag)
            End Select
        Else
            MsgBox "行值爲:" & curRow & ",當前行範圍無效,退出行執行!"
            Exit Sub ' 當行範圍無效時退出
        End If
        typeFlag = 0 ' 重置爲0
        
    Next
    ' --------------- 從第6列 到 最後列,做用例數據生成 end ---------
    
End Sub
Private Function makeIntTestData(curRow, curcolumn, sourcerow, typeFlag)
' 生成整數的測試數據
' curRow , 當前行值
' curcolumn, 當前列
' sourcerow, 源行 -- 正向用例列數據
' typeFlag,  此列類型標識

' 處理整型值,
   ' 0. 空
   ' 1, 0
   ' 2, 小數 (範圍內)
   ' 3, 長度 -1
   ' 4, 長度 +1
   ' 5, 負數 (範圍內)
   ' 6, 長度 +10
   
   Dim arr As String
   typearr = Array("空", _
                    "零", _
                    "小數 (範圍內)", _
                    "長度 -1", _
                    "長度 +1", _
                    "負數(範圍內)", _
                    "長度 +10")
                    
    typelenrow = 3 ' 描述 長度的行
    casedscolumn = 4 ' 用例描述的列
    typenamerow = 2 ' 字段名 所在行
    fieldLen = Int(Cells(typelenrow, curcolumn)) ' 字段長度

    ' 1~6種情況處理
    For i = 0 To 5
        Select Case i
        Case 0    ' 空
            curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
            Cells(curRow, curcolumn).Value = ""
            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
            Cells(curRow, curcolumn).Interior.Color = 255
        Case 1  ' 0
            curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
            Cells(curRow, curcolumn).Value = 0
            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
            Cells(curRow, curcolumn).Interior.Color = 255
        Case 2 ' 小數 (範圍內)
            curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
            If Int(Cells(typelenrow, curcolumn).Value) <= 3 Then  ' 如果長度爲3位或以下,則直接用1位整數+小數點+ 1位小數
                Cells(curRow, curcolumn).Value = makeInt(1) & "." & makeInt(1)
                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
                Cells(curRow, curcolumn).Interior.Color = 255
            Else ' 否則生成長度一致的小數
                Cells(curRow, curcolumn).Value = Mid(String(Int(Cells(typelenrow, curcolumn)), Int((Rnd()) * 9) + 1 & ""), 1, Cells(typelenrow, curcolumn) - 3) & "." & _
                                        String(2, Int((Rnd()) * 9) + 1 & "")
                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
                Cells(curRow, curcolumn).Interior.Color = 255
            End If
            
        Case 3 ' 長度 -1
            If Int(Cells(typelenrow, curcolumn)) > 1 Then ' 如果輸入長度爲 1 則不作 -1 長度測試
                curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
                Cells(curRow, curcolumn).Value = makeInt(Int(Cells(typelenrow, curcolumn)))
                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
                Cells(curRow, curcolumn).Interior.Color = 255
            Else
                curRow = curRow - 1 '
            End If
            
        Case 4 ' 長度 +1
            curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
            Cells(curRow, curcolumn).Value = Mid(String(Int(Cells(typelenrow, curcolumn) + 1), Int((Rnd()) * 9) + 1 & ""), 1, Cells(typelenrow, curcolumn) + 1)
            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
            Cells(curRow, curcolumn).Interior.Color = 255
        Case 5 '  負數 (範圍內)
            curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
            Cells(curRow, curcolumn).Value = "-" & makeInt(fieldLen)
            
            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
            Cells(curRow, curcolumn).Interior.Color = 255
        Case 6 '  負數 (範圍內)
            curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
            Cells(curRow, curcolumn).Value = "-" & makeInt(fieldLen + 10)
            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
            Cells(curRow, curcolumn).Interior.Color = 255

        End Select
        
        Cells(curRow, 5) = "反"  ' 標識正反用例
        Cells(curRow, 2) = Cells(curRow - 1, 2) + 1 ' 標數據增長
    Next
   
    makeIntTestData = curRow ' 返回當前行
End Function
Function copydata(cpsourcerow, cpcurrow)
' 從源行拷貝數據到當前行+1
'
' cpsourcerow -- 拷貝源
' cpcurrow    -- 當前行

     Rows(cpsourcerow).Select
     Selection.Copy
     Rows(cpcurrow + 1).Select
     ActiveSheet.Paste
     cpcurrow = cpcurrow + 1
 
    copydata = cpcurrow
End Function

Function getcolumnlen()
' 取第一行的長度,
'
    For i = 1 To 256
        If Cells(1, i).Value = "" Then
            getcolumnlen = i - 1
            Exit Function
        End If
    Next
End Function




Private Function makeFloatTestData(curRow, curcolumn, sourcerow, mftdintlen, mftddecLen)
' 生成小數測試數據
' curRow , 當前行值
' curcolumn, 當前列
' sourcerow, 源行 -- 正向用例列數據
' typeFlag,  此列類型標識
' mftdintlen 整數部份長度
' mftddecLen 小數部份長度

' 處理帶小數值情況,
    ' --    0   空
    ' --    1   零
    ' --    2   完全整數,爲指定 長度
    ' --    3   整數部份超 +1 長度,小數部份爲指定長度
    ' --    4   整數據部份爲指定長度,小數部份超 +1 長度
    ' --    5   整數部份超 +1 長度,小數部份超 +1 長度
    ' --    6   負數(整數指定長度範圍內)
    ' --    7   負數(整數部份超 +1 長度,小數部份爲指定長度   )
    ' --    8   負數(整數據部份爲指定長度,小數部份超 +1 長度)
    ' --    9   負數(整數部份超 +1 長度,小數部份超 +1 長度  )
    ' --    10  指定長度範圍內,整數部份有特殊字符
    ' --    11  指定長度範圍內,小數部份有特殊字符
    ' --    12  整數部份超 -1 長度,小數部份爲指定長度
    ' --    15  負數(整數部份超 -1 長度,小數部份爲指定長度   )
    ' --    17  負數(整數部份超 -1 長度,小數部份超 -1 長度  )
    ' --    13  整數據部份爲指定長度,小數部份超 -1 長度
    ' --    14  整數部份超 -1 長度,小數部份超 -1 長度
    ' --    16  負數(整數據部份爲指定長度,小數部份超 -1 長度)
    
    Dim arr As String
    typearr = Array("空", _
                    "零", _
                    "只有整數部分,長度爲整數與小數長度之合", _
                    "整數部份超 +1 長度,小數部份爲指定長度", _
                    "整數據部份爲指定長度,小數部份超 +1 長度", _
                    "整數部份超 +1 長度,小數部份超 +1 長度", _
                    "負數(整數指定長度範圍內)", _
                    "負數(整數部份超 +1 長度,小數部份爲指定長度)", _
                    "負數(整數據部份爲指定長度,小數部份超 +1 長度)", _
                    "負數(整數部份超 +1 長度,小數部份超 +1 長度)", _
                    "指定長度範圍內,整數部份有特殊字符", _
                    "指定長度範圍內,小數部份有特殊字符", _
                    "整數部份超 -1 長度,小數部份爲指定長度", _
                    "負數(整數部份超 -1 長度,小數部份爲指定長度)", _
                    "負數(整數部份超 -1 長度,小數部份超 -1 長度)", _
                    "整數據部份爲指定長度,小數部份超 -1 長度", _
                    "整數部份超 -1 長度,小數部份超 -1 長度", _
                    "負數(整數據部份爲指定長度,小數部份超 -1 長度)")
                    
    typelenrow = 3 ' 描述 長度的行
    casedscolumn = 4 ' 用例描述的列
    typenamerow = 2 ' 字段名 所在行
    testTypeLen = UBound(typearr) - LBound(typearr) - 1 ' 檢測類型 組合長度
    
    
    If (mftdintlen < 1) Or (mftddecLen < 1) Then
        MsgBox "字段長度值非法,請檢查,行爲:" & typelenrow & "列爲:" & curcolumn
        Exit Function
    End If

    For i = 0 To testTypeLen
        Select Case i
            Case 0      ' --    空
                curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
                
                ' --- 生成數據
                Cells(curRow, curcolumn).Value = ""
                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
                Cells(curRow, curcolumn).Interior.Color = 255
                
            Case 1      ' --    零
                curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
                ' --- 生成數據
                Cells(curRow, curcolumn).Value = 0
                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
                Cells(curRow, curcolumn).Interior.Color = 255
                
            Case 2      ' --    完全整數,爲指定 長度
                curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
                Cells(curRow, curcolumn).Value = String(Int(mftdintlen) + Int(mftddecLen), Int((Rnd()) * 9) + 1 & "")
                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
                Cells(curRow, curcolumn).Interior.Color = 255
                
            Case 3      ' --    整數部份超 +1 長度,小數部份爲指定長度
                curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
                Cells(curRow, curcolumn).Value = String(mftdintlen + 1, Int((Rnd()) * 9) + 1 & "") & "." & String(mftddecLen, Int((Rnd()) * 9) + 1 & "")
                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
                Cells(curRow, curcolumn).Interior.Color = 255
                
            Case 4      ' --    整數據部份爲指定長度,小數部份超 +1 長度
                curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
                Cells(curRow, curcolumn).Value = String(mftdintlen, Int((Rnd()) * 9) + 1 & "") & "." & String(mftddecLen + 1, Int((Rnd()) * 9) + 1 & "")
                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
                Cells(curRow, curcolumn).Interior.Color = 255
                
            Case 5      ' --    整數部份超 +1 長度,小數部份超 +1 長度
                curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
                Cells(curRow, curcolumn).Value = String(mftdintlen + 1, Int((Rnd()) * 9) + 1 & "") & "." & String(mftddecLen + 1, Int((Rnd()) * 9) + 1 & "")
                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
                Cells(curRow, curcolumn).Interior.Color = 255
                
            Case 6      ' --    負數(整數指定長度範圍內)
                curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
                Cells(curRow, curcolumn).Value = "-" & String(mftdintlen, Int((Rnd()) * 9) + 1 & "")
                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
                Cells(curRow, curcolumn).Interior.Color = 255
                
            Case 7      ' --    負數(整數部份超 +1 長度,小數部份爲指定長度   )
                curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
                Cells(curRow, curcolumn).Value = "-" & String(mftdintlen + 1, Int((Rnd()) * 9) + 1 & "") & "." & String(mftddecLen, Int((Rnd()) * 9) + 1 & "")
                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
                Cells(curRow, curcolumn).Interior.Color = 255
                
            Case 8      ' --    負數(整數據部份爲指定長度,小數部份超 +1 長度)
                curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
                Cells(curRow, curcolumn).Value = "-" & String(mftdintlen, Int((Rnd()) * 9) + 1 & "") & "." & String(mftddecLen + 1, Int((Rnd()) * 9) + 1 & "")
                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
                Cells(curRow, curcolumn).Interior.Color = 255
                
            Case 9      ' --    負數(整數部份超 +1 長度,小數部份超 +1 長度  )
                curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
                Cells(curRow, curcolumn).Value = "-" & String(mftdintlen + 1, Int((Rnd()) * 9) + 1 & "") & "." & String(mftddecLen + 1, Int((Rnd()) * 9) + 1 & "")
                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
                Cells(curRow, curcolumn).Interior.Color = 255
            Case 10     ' --    指定長度範圍內,整數部份有特殊字符
                curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
                Cells(curRow, curcolumn).Value = makeInt(mftdintlen - 1) & makeSpecialChar(1) & "." & makeInt(mftddecLen)
                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
                Cells(curRow, curcolumn).Interior.Color = 255
            Case 11     ' --    指定長度範圍內,小數部份有特殊字符
                curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
                Cells(curRow, curcolumn).Value = makeInt(mftdintlen) & "." & makeInt(mftddecLen - 1) & makeSpecialChar(1)
                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
                Cells(curRow, curcolumn).Interior.Color = 255
            Case 12     ' --    整數部份超 -1 長度,小數部份爲指定長度
                curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
                Cells(curRow, curcolumn).Value = makeInt(mftdintlen - 1) & "." & makeInt(mftddecLen)
                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
                Cells(curRow, curcolumn).Interior.Color = 255
            Case 15     ' --    負數(整數部份超 -1 長度,小數部份爲指定長度   )
                curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
                Cells(curRow, curcolumn).Value = "-" & makeInt(mftdintlen - 1) & "." & makeInt(mftddecLen)
                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
                Cells(curRow, curcolumn).Interior.Color = 255
            Case 17     ' --    負數(整數部份超 -1 長度,小數部份超 -1 長度  )
                curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
                Cells(curRow, curcolumn).Value = "-" & makeInt(mftdintlen - 1) & "." & makeInt(mftddecLen - 1)
                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
                Cells(curRow, curcolumn).Interior.Color = 255
            Case 13     ' --    整數據部份爲指定長度,小數部份超 -1 長度
                curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
                Cells(curRow, curcolumn).Value = makeInt(mftdintlen) & "." & makeInt(mftddecLen - 1)
                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
                Cells(curRow, curcolumn).Interior.Color = 255
            Case 14     ' --    整數部份超 -1 長度,小數部份超 -1 長度
                curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
                Cells(curRow, curcolumn).Value = makeInt(mftdintlen - 1) & "." & makeInt(mftddecLen - 1)
                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
                Cells(curRow, curcolumn).Interior.Color = 255
            Case 16     ' --    負數(整數據部份爲指定長度,小數部份超 -1 長度)
                curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
                Cells(curRow, curcolumn).Value = "-" & makeInt(mftdintlen) & "." & makeInt(mftddecLen - 1)
                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
                Cells(curRow, curcolumn).Interior.Color = 255
        End Select
        
        Cells(curRow, 5) = "反"  ' 標識正反用例
        Cells(curRow, 2) = Cells(curRow - 1, 2) + 1 ' 標數據增長
    Next
   
    makeFloatTestData = curRow ' 返回當前行
End Function


Private Function makeCharTestData(curRow, curcolumn, sourcerow, typeFlag)
' 生成 字符 型 測試數據
' curRow , 當前行值
' curcolumn, 當前列
' sourcerow, 源行 -- 正向用例列數據
' typeFlag,  此列類型標識
' mftdintlen 整數部份長度
' mftddecLen 小數部份長度

' 處理字符 型值情況,
    ' --0   空
    ' --1   正常長度,空白
    ' --2   正常長度,數字
    ' --3   正常長度, 字母
    ' --4   正常長度, 特殊符號(含中文)
    ' --5   正常長度, 數字、特殊符號(含中文)、字母混合
    ' --6   超長度 + 1, 空白
    ' --7   超長度 + 1, 數字
    ' --8   超長度 + 1, 字母
    ' --9   超長度 + 1, 特殊符號
    ' --10  超長度 + 1, 數字、特殊符號(含中文)、字母
    ' --11  短長度 - 1, 空白
    ' --12  短長度 - 1, 數字
    ' --13  短長度 - 1, 字母
    ' --14  短長度 - 1, 特殊符號
    ' --15  短長度 - 1, 數字、特殊符號(含中文)、字母

    Dim arr As String
    typearr = Array("空", "正常長度,空白", "正常長度,數字", "正常長度, 字母", "正常長度, 特殊符號", _
                    "正常長度, 數字、特殊符號(含中文)、字母混合", "超長度 + 1, 空白", "超長度 + 1, 數字", _
                    "超長度 + 1, 字母", "超長度 + 1, 特殊符號", "超長度 + 1, 數字、特殊符號(含中文)、字母", _
                    "短長度 - 1, 空白", "短長度 - 1, 數字", "短長度 - 1, 字母", "短長度 - 1, 特殊符號", _
                    "短長度 - 1, 數字、特殊符號(含中文)、字母")
                    
    typelenrow = 3 ' 描述 長度的行
    casedscolumn = 4 ' 用例描述的列
    typenamerow = 2 ' 字段名 所在行
    fieldLen = Int(Cells(typelenrow, curcolumn)) ' 字段長度
    testTypeLen = UBound(typearr) - LBound(typearr) - 1 ' 檢測類型 組合長度
    
    ' debug 長度值調試
    If fieldLen < 1 Then
        MsgBox "字段長度值非法,請檢查,行爲:" & typelenrow & "列爲:" & curcolumn
        Exit Function
    ElseIf fieldLen = 1 Then
        testTypeLen = testTypeLen - 5   ' 輸入長度爲1,則只長度-1測試可以不做
    End If
    
    For i = 0 To testTypeLen
        Select Case i
        Case 0      ' --    0   空
                curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
                ' --- 生成數據
                Cells(curRow, curcolumn).Value = ""
                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
                Cells(curRow, curcolumn).Interior.Color = 255
                
        Case 1      ' --    1   正常長度,   空白
                curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
                Cells(curRow, curcolumn).Value = String(fieldLen, " ")
                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
                Cells(curRow, curcolumn).Interior.Color = 255
                rowaddFlag = True
                
        Case 2      ' --    2   正常長度,   數字
                curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
                ' --- 生成數據
                Cells(curRow, curcolumn).Value = makeInt(fieldLen)
                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
                Cells(curRow, curcolumn).Interior.Color = 255
                
        Case 3      ' --    3   正常長度,   字母
            curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
                ' --- 生成數據
            Cells(curRow, curcolumn).Value = makeLCase(fieldLen)
            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
            Cells(curRow, curcolumn).Interior.Color = 255
                
        Case 4      ' --    4   正常長度,   特殊符號
            curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
                ' --- 生成數據
            Cells(curRow, curcolumn).Value = makeSpecialChar(fieldLen)
            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
            Cells(curRow, curcolumn).Interior.Color = 255
            
        Case 5      ' --    5   正常長度,   數字、特殊符號(含中文)、字母混合
            curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
            
            ' --- 生成數據
            ' 如果長度低於3位時,需處理
            If (fieldLen = 1) Then
                Cells(curRow, curcolumn).Value = makeSpecialChar(1)
            ElseIf (fieldLen = 2) Then
                Cells(curRow, curcolumn).Value = makeLCase(1) & makeInt(1)
            ElseIf (fieldLen = 3) Then
                Cells(curRow, curcolumn).Value = makeLCase(1) & makeInt(1) & makeSpecialChar(1)
            Else ' 大於3位時,全部混合, 字母+ 數字 + 中文+ 特殊 (其餘位)
                Cells(curRow, curcolumn).Value = makeLCase(1) & makeInt(1) & makeChinaChar(2) & makeSpecialChar(fieldLen - 4)
            End If
            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
            Cells(curRow, curcolumn).Interior.Color = 255
            
        Case 6      ' --    6   超長度 + 1,     空白
            curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
            Cells(curRow, curcolumn).Value = String(fieldLen + 1, " ")
            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
            Cells(curRow, curcolumn).Interior.Color = 255
                
        Case 7      ' --    7   超長度 + 1,     數字
            curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
            Cells(curRow, curcolumn).Value = makeInt(fieldLen + 1)
            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
            Cells(curRow, curcolumn).Interior.Color = 255
            
        Case 8      ' --    8   超長度 + 1,     字母
            curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
            Cells(curRow, curcolumn).Value = makeLCase(fieldLen + 1)
            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
            Cells(curRow, curcolumn).Interior.Color = 255
            
        Case 9      ' --    9   超長度 + 1,     特殊符號
            curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
            Cells(curRow, curcolumn).Value = makeSpecialChar(fieldLen + 1)
            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
            Cells(curRow, curcolumn).Interior.Color = 255
            
        Case 10     ' --    10  超長度 + 1,     數字、特殊符號(含中文)、字母
            curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
            
            ' --- 生成數據
            ' 如果長度低於2位時,需處理
            If (fieldLen = 1) Then
                 Cells(curRow, curcolumn).Value = makeSpecialChar(1)
            ElseIf (fieldLen = 2) Then
                Cells(curRow, curcolumn).Value = makeLCase(1) & makeInt(1) & makeSpecialChar(1)
            ElseIf (fieldLen = 3) Then
                Cells(curRow, curcolumn).Value = makeLCase(1) & makeSpecialChar(1) & makeChinaChar(2)
            ElseIf (field = 4) Then  ' 等於4位時,全部混合, 字母+ 數字 + 中文(2)
                Cells(curRow, curcolumn).Value = makeLCase(1) & makeInt(1) & makeChinaChar(2) _
                                                & makeLCase(1) ' 多加一位
            ElseIf (field > 4) Then  ' 大於4位時,全部混合, 字母+ 數字 + 中文+ 特殊 (其餘位)
                Cells(curRow, curcolumn).Value = makeLCase(1) & makeInt(1) & makeChinaChar(2) & makeSpecialChar(fieldLen - 4) _
                                                & makeLCase(1) ' 多加一位
            End If
            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
            Cells(curRow, curcolumn).Interior.Color = 255
            
            
        Case 11     ' --    11  短長度 - 1,     空白
            curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
            Cells(curRow, curcolumn).Value = String(fieldLen - 1, " ")
            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
            Cells(curRow, curcolumn).Interior.Color = 255
                
        Case 12     ' --    12  短長度 - 1,     數字
            curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
            Cells(curRow, curcolumn).Value = makeInt(fieldLen - 1)
            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
            Cells(curRow, curcolumn).Interior.Color = 255
            
        Case 13     ' --    13  短長度 - 1,     字母
            curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
            Cells(curRow, curcolumn).Value = makeLCase(fieldLen - 1)
            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
            Cells(curRow, curcolumn).Interior.Color = 255
            
        Case 14     ' --    14  短長度 - 1,     特殊符號
            curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
            Cells(curRow, curcolumn).Value = makeSpecialChar(fieldLen - 1)
            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
            Cells(curRow, curcolumn).Interior.Color = 255
            
        Case 15     ' --    15  短長度 - 1,     數字、特殊符號(含中文)、字母
            curRow = copydata(sourcerow, curRow) ' --- 從源拷貝數據
            
            ' --- 生成數據
            ' 如果長度低於2位時,需處理
            If (fieldLen = 1) Then
                Rows(curRow).Select
                ActiveSheet.Delete
                curRow = curRow - 1 ' 當長度要求爲1時,少一位就相當於空,所以回滾一行
            ElseIf (fieldLen = 2) Then
                Cells(curRow, curcolumn).Value = makeSpecialChar(1)
            ElseIf (fieldLen = 3) Then
                Cells(curRow, curcolumn).Value = makeSpecialChar(1) & makeInt(1)
            ElseIf (fieldLen = 4) Then ' 等於4位時,全部混合, 字母+ 中文
                Cells(curRow, curcolumn).Value = makeLCase(1) & makeChinaChar(2)
            ElseIf (fieldLen = 5) Then ' 等於 5 位時,全部混合, 字母+ 數字 + 中文
                Cells(curRow, curcolumn).Value = makeLCase(1) & makeInt(1) & makeChinaChar(2)
            ElseIf (fieldLen > 5) Then ' 大於 5 位時,全部混合, 字母+ 數字 + 中文+ 特殊 (其餘位)
                Cells(curRow, curcolumn).Value = makeLCase(1) & makeInt(1) & makeChinaChar(2) & makeSpecialChar(fieldLen - 5)
            End If
            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值爲:" & typearr(i) & ", 交易失敗"
            Cells(curRow, curcolumn).Interior.Color = 255
            
        End Select
        
        Cells(curRow, 5) = "反"  ' 標識正反用例
        Cells(curRow, 2) = Cells(curRow - 1, 2) + 1 ' 標數據增長
    Next
   
    makeCharTestData = curRow ' 返回當前行
End Function




Function makeInt(makeIntlen)
' 生成指定長度的數字
' strmakeIntlen, 需要生成的長度

    ' debug
    'strmakeIntlen = 9
    If makeIntlen < 0 Then
        MsgBox "makeIntlen 參數小於0 ,退出!"
        Exit Function
    End If
    
    Dim strintdata
    strintdata = ""
    For i = 1 To makeIntlen
        strintdata = strintdata & Chr(Int((Rnd() * 10 + 48)))
    Next
    
        makeInt = strintdata
End Function

Function makeLCase(makeLCaselen)
' 生成指定長度的數字
' makeLCaselen, 需要生成的長度

    ' debug
    'strmakeIntlen = 9
    If makeLCaselen < 0 Then
        MsgBox "makeLCaselen 參數小於0 ,退出!"
        Exit Function
    End If
    
    Dim strintdata
    strintdata = ""
    For i = 1 To makeLCaselen
        strintdata = strintdata & Chr(Int((Rnd() * 26 + 97)))
    Next
    
        makeLCase = strintdata
End Function


Function makeSpecialChar(makeSpecialCharlen)
' 生成  特殊字符
' makeSpecialCharlen, 需要生成的長度

    ' debug
    'strmakeIntlen = 9
    If makeSpecialCharlen < 0 Then
        MsgBox "makeSpecialCharlen 參數小於0 ,退出!"
        Exit Function
    End If
    
    
    '
    Dim strintdata
    strintdata = ""
    For i = 1 To makeSpecialCharlen
        ' 因特殊符號,不在相鄰的ascii碼值內,分爲4個階段
        If ((10 Mod 4) = 0) Then
            strintdata = strintdata & Chr(Int((Rnd() * 33 + 15)))
        ElseIf ((10 Mod 4) = 1) Then
            strintdata = strintdata & Chr(Int((Rnd() * 7 + 58)))
        ElseIf ((10 Mod 4) = 2) Then
            strintdata = strintdata & Chr(Int((Rnd() * 6 + 91)))
        ElseIf ((10 Mod 4) = 3) Then
            strintdata = strintdata & Chr(Int((Rnd() * 4 + 123)))
        End If
    Next
    
    makeSpecialChar = strintdata
End Function



Function makeChinaChar(makeChinaCharlen)
' 生成  特殊字符
' makeSpecialCharlen, 需要生成的長度
' 一箇中文佔2個

    ' debug
    'strmakeIntlen = 9
    If makeChinaCharlen < 0 Then
        MsgBox "makeChinaCharlen 參數小於0 ,退出!"
        Exit Function
    End If
    
    ' arry  長度爲 19, 類型爲中文
    Dim arry
    arry = Array("使", "用", "百", "度", "特", "殊", "符", _
                "號", "中", "文", "以", "第", "一", "時", _
                "間", "收", "到", "提", "問", "有", "新", _
                "回", "答", "回", "答", "被", "採", "納", _
                "網", "友", "求", "助", "的", "通", "查", _
                "看", "詳", "情")

    Dim strintdata
    strintdata = ""
    
    ' 當長度爲奇數時,最後一位用字母
    If (makeChinaCharlen Mod 2) = 1 Then
        makeChinaCharlen = makeChinaCharlen - 1
        For i = 1 To makeChinaCharlen Step 2
            strintdata = strintdata & arry(Rnd * 19 + 1)
        Next
        ' 爲奇數時,最後付一位字母
        strintdata = strintdata & Chr(Int((Rnd() * 26 + 97)))
    Else
        For i = 1 To makeChinaCharlen Step 2
            strintdata = strintdata & arry(Rnd * 19 + 1)
        Next
    End If
    
    
    makeChinaChar = strintdata
End Function


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章