20170501xlVBA销售订单整理一行转多行

Sub NextSeven_CodeFrame()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.Calculation = xlCalculationManual
    Application.StatusBar = ">>>>>>>>程序正在运行>>>>>>>>"


    On Error GoTo ErrHandler

    Dim StartTime, UsedTime As Variant
    StartTime = VBA.Timer
    '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    Dim Wb As Workbook
    Dim Sht As Worksheet
    Dim oSht As Worksheet
    Dim Rng As Range
    'Dim Arr As Variant
Dim Arr()
    
    Dim EndRow As Long
    Const HEAD_ROW As Long = 1
    Const SHEET_NAME As String = "原始订单"
    Const START_COLUMN As String = "A"
    Const END_COLUMN As String = "O"
    Dim i As Long, j As Long, k As Long
    Dim N As Long
    Const OTHER_HEAD_ROW As Long = 1
    Const OTHER_SHEET_NAME As String = "整理订单"
    Const OTHER_START_COLUMN As String = "A"
    Const OTHER_END_COLUMN As String = "O"
    '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    '获取原始记录
    Set Wb = Application.ThisWorkbook
    Set Sht = Wb.Worksheets(SHEET_NAME)
    With Sht
        EndRow = .Cells(.Cells.Rows.Count, 1).End(xlUp).Row
        Set Rng = .Range(.Cells(HEAD_ROW + 1, START_COLUMN), .Cells(EndRow, END_COLUMN))
        'Arr = Rng.Value
        ReDim Arr(1 To Rng.Rows.Count, 1 To Rng.Columns.Count)
        With Rng
            For i = 1 To .Rows.Count
                  For j = 1 To .Columns.Count
                        Arr(i, j) = .Cells(i, j).Text
                  Next j
            Next i
        End With
    End With

    '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    '生成新记录
    Dim brr() As String
    ReDim brr(1 To 15, 1 To 1)
    N = 0

    For i = LBound(Arr) To UBound(Arr)
        Key = CStr(Arr(i, 2))
        '判断Chr(10)
        If InStr(1, Key, Chr(10)) = 0 Then
            N = N + 1
            ReDim Preserve brr(1 To 15, 1 To N)
            For j = 1 To 15
                brr(j, N) = Arr(i, j)
            Next j
        Else
            crr = Split(Key, Chr(10))
            For k = LBound(crr) To UBound(crr)
                N = N + 1
                ReDim Preserve brr(1 To 15, 1 To N)
                If k = 0 Then
                    For j = 1 To 15
                        If j = 2 Then
                            brr(j, N) = crr(k)
                        Else
                            brr(j, N) = Arr(i, j)
                        End If
                    Next j
                Else
                    brr(2, N) = crr(k)
                    brr(14, N) = Arr(i, 14)
                    brr(15, N) = Arr(i, 15)
                End If
            Next k
        End If

    Next i
      
    For i = LBound(brr, 2) To UBound(brr, 2)
       brr(14, i) = Replace(brr(14, i), "深圳号-顺丰国际小包挂号", "USPS")
    Next i


    '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    Set oSht = Wb.Worksheets(OTHER_SHEET_NAME)
    With oSht
            .UsedRange.Offset(1).ClearComments
            .Range("A2").Resize(UBound(brr, 2), UBound(brr)).Value = _
            Application.WorksheetFunction.Transpose(brr)
            .UsedRange.Columns.AutoFit
    End With


    '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    UsedTime = VBA.Timer - StartTime
    MsgBox "本次耗时:" & Format(UsedTime, "0.000秒"), vbOKOnly, "NextSeven Excel Studio"

ErrorExit:
    Set Wb = Nothing
    Set Sht = Nothing
    Set Rng = Nothing
    Set oSht = Nothing

    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.Calculation = xlCalculationAutomatic
    Application.StatusBar = False
    Exit Sub
    '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ErrHandler:
    If Err.Number <> 0 Then
        MsgBox Err.Description & "!", vbCritical, "NextSeven Excel Studio "
        'Debug.Print Err.Description
        Err.Clear
        Resume ErrorExit
    End If
End Sub

  

转载于:https://www.cnblogs.com/nextseven/p/7129166.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值