Anda di halaman 1dari 3

Option Explicit Sub Recap() Dim sInput As String Dim aryWorking As Variant Dim arySlash As Variant Dim aryX

As Variant s from multipliers Dim aryMultiplier As Variant of X in single slash group Dim lMultiplierSum As Long group Dim sMultiplier As String Dim aryNumberGroups As Variant up Dim lX As Long Dim lY As Long Dim lZ As Long Dim lXCounter As Long Dim sSubString As String Dim lActiveRow As Long Dim oFound As Object Dim lLastRow As Long Dim sOutput As String Dim lLastDataRow As Long Dim sActiveSheetName As String hould contain the input lines Dim sTempString As String 'Holds each side of equality 'Holds each slash grouping 'In a single slash group splits number group 'Holds string reprensetation of what is left 'Holds sum of multipliers in a single slash 'String representation of sum of multipliers 'Holds number groups from a single slash gro 'For-Next Counter 'For-Next Counter 'For-Next Counter 'Counts X in string 'A piece of sInput 'The RecapTemp worksheet row to write on 'check for a number group already present 'Last filled row of various columns 'To hold the answer as it is built 'Last row that holds input data 'Name of activesheet when code is run. It s

sActiveSheetName = ActiveSheet.Name lLastDataRow = Cells(Rows.Count, 2).End(xlUp).Row 'Create scratch worksheet On Error Resume Next Application.DisplayAlerts = False Worksheets("RecapTemp").Delete Application.DisplayAlerts = True On Error GoTo 0 Worksheets.Add(After:=Sheets(Sheets.Count)).Name = "RecapTemp" Worksheets(sActiveSheetName).Select For lZ = 1 To lLastDataRow Worksheets("RecapTemp").UsedRange.Clear sOutput = vbNullString sTempString = vbNullString sInput = Cells(lZ, 2).Value 'column B should contain only strings like 4 9.41.80.70.30.13.85.49.99.11.74.58.94.32.68.1149.149.49.94.85.41.30.49x20 'Validate sInput characters (0-9, , x, X, For lX = 1 To Len(sInput) sSubString = Mid(sInput, lX, 1) If InStr("0123456789.xX/ ", sSubString) = 0 Then MsgBox "Character " & lX & "[" & sSubString & "] in the input st ring for row " & lZ & " is invalid. Exiting.": GoTo End_Sub Else If sSubString = " " Then sSubString = "" sTempString = sTempString & sSubString

End If Next sInput = sTempString arySlash = Split(sInput, "/") For lX = LBound(arySlash) To UBound(arySlash) 'Examine each slash group lXCounter = 0 lMultiplierSum = 0 'Validate that there is only one X in the split being examined For lY = 1 To Len(arySlash(lX)) If UCase(Mid(arySlash(lX), lY, 1)) = "X" Then lXCounter = lXCoun ter + 1 Next Select Case lXCounter Case 0: MsgBox "There is no multiplier in at least segment for row " & l Z & ". Exiting." Stop GoTo End_Sub Case 1 'Good to Go Case Else: MsgBox "There is more than one multiplier in at least one segment for row " & lZ & ". Exiting.": GoTo End_Sub End Select 'Examine each slash group 'Extract Multiplier aryX = Split(arySlash(lX), "x") 'handle multi part multiplier aryMultiplier = Split(aryX(1), ".") For lY = LBound(aryMultiplier) To UBound(aryMultiplier) lMultiplierSum = lMultiplierSum + aryMultiplier(lY) Next aryNumberGroups = Split(aryX(0), ".") For lY = LBound(aryNumberGroups) To UBound(aryNumberGroups) 'Search for numbergroup in column 1 if With Worksheets("RecapTemp") Set oFound = .Columns("A:A").Find(What:=aryNumberGroups(lY), LookIn:=xlValues, _ LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection: =xlNext, _ MatchCase:=False, SearchFormat:=False) If Not oFound Is Nothing Then lActiveRow = oFound.Row Else lActiveRow = .Cells(.Rows.Count, 1).End(xlUp).Row + 1 With .Cells(lActiveRow, 1) .NumberFormat = "@" .Value = aryNumberGroups(lY) End With End If 'Add/update row .Cells(lActiveRow, 2).NumberFormat = "General" .Cells(lActiveRow, 2).Value = .Cells(lActiveRow, 2) + lMulti plierSum End With Next Next

'Sort Result With Worksheets("RecapTemp") lLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row .Range("A1:B" & lLastRow).Sort Key1:=.Range("B2"), Order1:=xlAscendi ng, Key2:=.Range("A2"), _ Order2:=xlAscending, Header:=xlNo, OrderCustom:=1, MatchCase:=Fa lse, _ Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOptio n2:=xlSortNormal 'Group Result sMultiplier = .Cells(2, 2) For lX = 2 To lLastRow + 1 If sMultiplier <> .Cells(lX, 2) Then sOutput = sOutput & "x" & sMultiplier & "/" & .Cells(lX, 1) sMultiplier = .Cells(lX, 2) Else sOutput = sOutput & IIf(Right(sOutput, 1) = "/" Or Len(sOutp ut) = 0, "", ".") & .Cells(lX, 1) End If Next sOutput = Left(sOutput, Len(sOutput) - 1) End With Cells(lZ, 3).Value = sOutput Next End_Sub: End Sub

Anda mungkin juga menyukai