VBA help

Emmily

Well-known Member
Joined
Oct 5, 2008
Messages
676
Hi, is there a macro that will produce the below result. For each team i want the count of entries per age criteria as reflected in row 1 and then i want the amount in (AUD) and then the number of entries which does not have a comment in col P and it value in AUD.

I know all of this can be done via formula, but i will have another 20 teams and the last age criteria will go to >90, so there will be a lot of formulas, hence it will slow the performance of the workbook.

Col L-P is raw data and R-S is FX data

Excel Workbook
ABCDEFGHIJKLMNOPQRS
12-56-29
2TeamNo.of itemsValue (AUD)No. of items without CommentsValue (AUD)No.of itemsValue (AUD)No. of items without CommentsValue (AUD)AmountCCYAgeSourceCommentsFX RATESRate (AUD)
3CMP1111,593.6615,796.8300.0000.008,268.57AUD1456TRACESAED3.712435
4TRACES20,000.00AUD1248TRACESANG1.809243
5LEHMAN1,000.00AUD1248TRACESARS4.053867
64,417.80GBP3CMP1TestATS10.203373
72,208.90GBP1018CMP1AUD1
8100,000.00AUD742TRACESBEF29.912398
91,163.22EUR561LEHMANBGN1.450275
10-3,320.14EUR561LEHMANGBP0.381053
119,646.51EUR561LEHMANTestBMD1.01075
12-12,618.80EUR561LEHMANBRL1.688256
13476,017.76EUR561LEHMANBSD1.01075
14-1,688,137.00JPY561LEHMANBVD2
157,187,517.00JPY561LEHMANCAD1.001098
16-6,303,971.00JPY561LEHMANCHF0.957838
17108,431.34USD561LEHMAN
18
19
20
Sheet1
 
Ok, by using the trim part its working, but i noticed one thing when i ran your code, it removes the the teams in Col A, and the code puts the teams there. I need the teams to be stationary as some teams might not be on the L:P but i still need to show them in the stats.

<table border="0" cellpadding="0" cellspacing="0" width="64"><col style="width: 48pt;" width="64"> <tbody><tr style="height: 14.4pt;" height="19"> <td style="height: 14.4pt; width: 48pt;" height="19" width="64">
</td></tr><tr style="height: 14.4pt;" height="19"><td style="height: 14.4pt;" height="19">
</td></tr><tr style="height: 14.4pt;" height="19"><td style="height: 14.4pt;" height="19">
</td></tr><tr style="height: 14.4pt;" height="19"><td style="height: 14.4pt;" height="19">
</td></tr></tbody></table>
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi,

Here you go..


Excel Workbook
ABCDERSTUVWXYZAAABACAD
1*2-5***>90****
2*No. of itemsValue (AUD)No. of items without CommentsValue (AUD)No. of itemsValue (AUD)No. of items without CommentsValue (AUD)*AmountCCYAgeSourceCommentsFX RATESRate (AUD)
3TRACES00004129268.64129268.6*8,268.57AUD1456TRACESAED3.712435
4CMP1111593.660015796.83215796.832*20,000.00AUD1248TRACESANG1.809243
5LEHMAN00009-1626618-168375*1,000.00AUD1248TRACESATS10.203373
6**********4,417.80GBP3CMP1TestAUD1
7**********2,208.90GBP1018CMP1BEF29.912398
8**********1,00,000.00AUD742TRACESJPY1.450275
9**********1,163.22EUR561LEHMANGBP0.381053
10**********-3,320.14EUR561LEHMANBMD1.01075
11**********9,646.51EUR561LEHMANTestEUR1.688256
12**********-12,618.80EUR561LEHMANBSD1.01075
13**********4,76,017.76EUR561LEHMANBVD2
14**********-16,88,137.00JPY561LEHMANCAD1.001098
15**********71,87,517.00JPY561LEHMANUSD0.957838
16**********-63,03,971.00JPY561LEHMAN**
17**********1,08,431.34USD561LEHMAN
Sheet1
 
Upvote 0
Hi,

Try

Code:
Sub kTest()
    
    Dim ka, k, i As Long, c As Long, n As Long, Idx, m As Long
    Dim dic1 As Object, Fx, Age As String, dic2 As Object
    Dim Hdr, AgeGroup, r As Long, tmp, Flg As Boolean, t As Long
    
    '// adjust to suit
    Const SourceShtName         As String = "Sheet1"
    Const SourceDataRange       As String = "L:P"
    Const StartRow              As Long = 3
    Const FxDataRange           As String = "R3:S16"
    Const DestRange             As String = "A1"
    '//End
    
    
    Hdr = Array("No. of items", "Value (AUD)", "No. of items without Comments", "Value (AUD)")
    AgeGroup = Array("2-5", "6-29", "30-59", "60-89", ">90")
    
    Set dic1 = CreateObject("scripting.dictionary")
        dic1.comparemode = 1
    Set dic2 = CreateObject("scripting.dictionary")
        dic2.comparemode = 1
        
    With Worksheets(CStr(SourceShtName))
        ka = Intersect(.UsedRange, .Range(CStr(SourceDataRange)))
        Fx = .Range(CStr(FxDataRange))
        r = .Range("a" & .Rows.Count).End(xlUp).Row
        tmp = .Range("a3:a" & r)
    End With
    
    For i = 1 To UBound(Fx, 1)
        dic1.Item(Fx(i, 1)) = Fx(i, 2)
    Next
    
    For i = 1 To UBound(tmp, 1)
        dic2.Item(tmp(i, 1)) = i
    Next
    
    Age = ",{2,1;6,2;30,3;60,4;90,5}"
    
    ReDim k(1 To UBound(tmp, 1), 1 To 21)
    
    c = 23 - Range(CStr(SourceDataRange)).Column
    
    For i = StartRow To UBound(ka, 1)
        Idx = Evaluate("=lookup(" & ka(i, 3) & Age & ")")
        If Not IsError(Idx) Then
            If dic2.exists(ka(i, 4)) Then
                Flg = True
                t = dic2.Item(ka(i, 4))
                k(t, 1) = ka(i, 4)
                k(t, Idx * 4 - 4 + 2) = k(t, Idx * 4 - 4 + 2) + 1
                If CSng(dic1.Item(ka(i, 2))) Then
                    k(t, Idx * 4 - 4 + 3) = k(t, Idx * 4 - 4 + 3) + CSng(ka(i, 1) / dic1.Item(ka(i, 2)))
                End If
                If Len(Trim$(ka(i, 5))) = 0 Then
                    k(t, Idx * 4 - 4 + 4) = k(t, Idx * 4 - 4 + 4) + 1
                    If CSng(dic1.Item(ka(i, 2))) Then
                        k(t, Idx * 4 - 4 + 5) = k(t, Idx * 4 - 4 + 5) + CSng(ka(i, 1) / dic1.Item(ka(i, 2)))
                    End If
                End If
            End If
        End If
    Next
    
    If Flg Then
        With Worksheets(CStr(SourceShtName))
            If c > 0 Then .Cells(1).Resize(, c).EntireColumn.Insert
            .Rows(1).NumberFormat = "@"
            .Rows(2).WrapText = 1
            .Rows(2).Font.Bold = 1
            n = UBound(tmp, 1)
            With .Range(CStr(DestRange))
                .Resize(n + 2, 21).ClearContents
                m = 1
                For i = 0 To UBound(AgeGroup)
                    .Offset(, m).Value = AgeGroup(i)
                    .Offset(1, m).Resize(, 4).Value = Hdr
                    m = m + 4
                Next
                .Offset(1) = "Team"
                
                .Offset(2).Resize(n, 21).Value = k
                On Error Resume Next
                .Offset(2).Resize(n, 21).SpecialCells(4).Value = 0
                On Error GoTo 0
            End With
        End With
    End If
        
End Sub

HTH
 
Upvote 0
Great, this works. If i was going to add named ranges to the data source and FX would that be too difficult to change

So W:AA becomes rngSource
AC3:AD16 becomes rngFX

This will give me flexibility to put Source data and Fx on another sheet if need be

Const SourceDataRange As String = "W:AA"
Const FxDataRange As String = "AC3:AD16"
 
Upvote 0
Great, this works.

You are welcome !

If i was going to add named ranges to the data source and FX would that be too difficult to change

So W:AA becomes rngSource
AC3:AD16 becomes rngFX

This will give me flexibility to put Source data and Fx on another sheet if need be

Const SourceDataRange As String = "W:AA"
Const FxDataRange As String = "AC3:AD16"

Let the sourceshtname there.

replace the datarange and fxrange with

Code:
ka = Intersect(.UsedRange, .Range("rngSource"))
Fx = .Range("rngFX")
 
Upvote 0
You are welcome !

Let the sourceshtname there.

replace the datarange and fxrange with

Code:
ka = Intersect(.UsedRange, .Range("rngSource"))
Fx = .Range("rngFX")

Kris, i notice when i repeat running the macro, the code repeats the age criterias after >90 criteria, it just keeps expanding everytime i run the code.
 
Upvote 0
Ok, i fixed that by putting the const as named range.

c = 23 - Range(SourceDataRange).Column.

Kris whilst on this subject, i have to do the same thing with asset data this time, using the same age critierias but not Exchange rates, how can i use your code for the below. The data is a bit different therefore i have posted a formula result so you can what needs to be done.

Excel Workbook
ARSTUVWAEAFAH
1>90
2TeamNo. of itemsUnitsNo. of items without CommentsUnitsSourceExceptionAge BreakComments
3TRANS1-149,000.0000.00TRANS-149,000.001255TEST
4LEHMAN2-6,223,937.801263.20LEHMAN-6,224,201.001081Test
5AMORTISE2-3,884.942-3,884.94LEHMAN1081Test
6LEHMAN1081Test
7LEHMAN1081Test
8LEHMAN263.201080
9AMORTISE-1,294.98812
10AMORTISE-2,589.96812
11
Sheet2
 
Upvote 0
Hi,

Try

Code:
Sub kTest()
    
    Dim ka, k, i As Long, c As Long, n As Long, Idx, m As Long
    Dim dic As Object, Age As String
    Dim Hdr, AgeGroup, r As Long, tmp, Flg As Boolean, t As Long
    
    '// adjust to suit
    Const SourceShtName         As String = "Sheet1"
    Const SourceDataRange       As String = "W:Z"
    Const StartRow              As Long = 3
    Const DestRange             As String = "A1"
    '//End
    
    
    Hdr = Array("No. of items", "Units", "No. of items without Comments", "Units")
    AgeGroup = Array("2-5", "6-29", "30-59", "60-89", ">90")
    
    Set dic = CreateObject("scripting.dictionary")
        dic.comparemode = 1
        
    With Worksheets(CStr(SourceShtName))
        ka = Intersect(.UsedRange, .Range(CStr(SourceDataRange)))
        r = .Range("a" & .Rows.Count).End(xlUp).Row
        tmp = .Range("a3:a" & r)
    End With
    
    For i = 1 To UBound(tmp, 1)
        If Len(Trim$(tmp(i, 1))) Then dic.Item(tmp(i, 1)) = i
    Next
    
    Age = ",{2,1;6,2;30,3;60,4;90,5}"
    
    ReDim k(1 To UBound(tmp, 1), 1 To 21)
    
    For i = StartRow To UBound(ka, 1)
        If Len(Trim$(ka(i, 2))) Then
            Idx = Evaluate("=lookup(" & ka(i, 3) & Age & ")")
            If Not IsError(Idx) Then
                If dic.exists(ka(i, 1)) Then
                    Flg = True
                    t = dic.Item(ka(i, 1))
                    k(t, 1) = ka(i, 1)
                    k(t, Idx * 4 - 4 + 2) = k(t, Idx * 4 - 4 + 2) + 1
                    k(t, Idx * 4 - 4 + 3) = k(t, Idx * 4 - 4 + 3) + ka(i, 2)
                    If Len(Trim$(ka(i, 4))) = 0 Then
                        k(t, Idx * 4 - 4 + 4) = k(t, Idx * 4 - 4 + 4) + 1
                        k(t, Idx * 4 - 4 + 5) = k(t, Idx * 4 - 4 + 5) + ka(i, 2)
                    End If
                End If
            End If
        End If
    Next
    
    If Flg Then
        With Worksheets(CStr(SourceShtName))
            .Rows(1).NumberFormat = "@"
            .Rows(2).WrapText = 1
            .Rows(2).Font.Bold = 1
            n = dic.Count
            With .Range(CStr(DestRange))
                .Resize(n + 2, 21).ClearContents
                m = 1
                For i = 0 To UBound(AgeGroup)
                    .Offset(, m).Value = AgeGroup(i)
                    .Offset(1, m).Resize(, 4).Value = Hdr
                    m = m + 4
                Next
                .Offset(1) = "Team"
                .Offset(2).Resize(n, 21).Value = k
                On Error Resume Next
                .Offset(2).Resize(n, 21).SpecialCells(4).Value = 0
                On Error GoTo 0
            End With
        End With
    End If
        
End Sub

HTH
 
Upvote 0
Kris, when i tried to use a named range "Test" then the code did not work, if you look at my raw data i hid some columns which were not important, but my real data look similar to the below, so not sure how your code looks up this data. I put the named range in Sheet 3, i then tries to do this, ran the macro but nothing happened.

ka = Intersect(Sheet3.UsedRange, Sheet3.Range("Test"))


Excel Workbook
ABCDEFGHIJKL
2SourceCase No.Value DateSecurity CodeDescriptionCcy CodeLedg StmtUnitsExceptionAge BreakLast UpdateComments
3TRANS68062239365INPUSDTestLedg149000-14900012558-Sep-10TEST
4LEHMAN55093339539OCVTestAUDLedg751981-622420110817-May-09Test
5LEHMAN55093339539OCVTestAUDLedg475213310817-May-09Test
6LEHMAN55093339539OCVTestAUDLedg37476310817-May-09Test
7LEHMAN55093339539OCVTestAUDLedg14076110817-May-09Test
8LEHMAN55503039540OCVTestAUDStmt75000263.2108029-May-09
9AMORTISE69870739808CW250335TestLedg1294.98-1294.9881223-Aug-10
10AMORTISE69871039808CWL25035TestLedg2589.96-2589.9681223-Aug-10
Sheet3
 
Upvote 0
I ran your code again, changed this to this, but the result is not the same as the orignal post. The code removes Trans and replace with zero

ka = Intersect(Sheet3.UsedRange, Sheet3.Range("Test"))


Excel Workbook
ARSTU
1>90
2TeamNo. of itemsUnitsNo. of items without CommentsUnits
3000.0000.00
4LEHMAN52,758,762.0000.00
5AMORTISE21,397,417.0000.00
Sheet2
 
Upvote 0

Forum statistics

Threads
1,224,564
Messages
6,179,543
Members
452,924
Latest member
JackiG

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top