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
 
Hi,

What's your address of rngSource ? If it's start with C3:Px then the code should work. otherwise adjust the column numbers used in the code.
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi,

What's your address of rngSource ? If it's start with C3:Px then the code should work. otherwise adjust the column numbers used in the code.

Address of rngSource is A3:P, i tried playing with the column numbers but no luck
 
Upvote 0
Hi,

What's your address of rngSource ? If it's start with C3:Px then the code should work. otherwise adjust the column numbers used in the code.

Kris in your code which part points to Col P

Why is the code not putting result in "No items without comments, have a look at the below for Traces and CMP1, the code is only picking up 3 trace items when there should be 4, also for CMP1 one item has no comments but the code is putting zero

Excel Workbook
BSTUV
1>90
2TeamNo. of itemsValue (AUD)No. of items without CommentsValue (AUD)
3TRACES3121,000.0000.00
4CMP1210,585.3400.00
Sheet1
Excel Workbook
ABCDEFGHIJKLMNOP
3CategoryRecAccountValue DateEntry DateTypeAmountCCYAgeSourceRef1Ref2Ref3ReferenceLast UpdateComments
4TESTTESTTESTTESTTESTTEST8268.57AUD1456TRACESTESTTESTTESTTEST
5TESTTESTTESTTESTTESTTEST20000AUD1248TRACESTESTTESTTESTTEST
6TESTTESTTESTTESTTESTTEST1000AUD1248TRACESTESTTESTTESTTEST
7TESTTESTTESTTESTTESTTEST4417.8GBP1019CMP1TESTTESTTESTTESTTest
8TESTTESTTESTTESTTESTTEST2208.9GBP1018CMP1TESTTESTTESTTEST
9TESTTESTTESTTESTTESTTEST100000AUD742TRACESTESTTESTTESTTEST
10TESTTESTTESTTESTTESTTEST1163.22EUR561LEHMANTESTTESTTESTTEST
Rec



 
Upvote 0
Hi,

Code:
If Len(Trim$(ka(i, 14))) = 0 Then

This line checks whether column P has comments or not. In your code replace 14 with 16.


Also confirm that there is no currency is missing.

Kris
 
Upvote 0
I changed to 16 and the code returned this, which is not correct, i should be getting 4 counts for Traces, see second screen shot below

Fx rates are correct

Excel Workbook
BSTUV
1>90
2TeamNo. of itemsValue (AUD)No. of items without CommentsValue (AUD)
3TRACES3121,000.0030.00
4CMP1210,585.3410.00
Sheet1


This is what i should be getting


Excel Workbook
BSTUV
1>90
2TeamNo. of itemsValue (AUD)No. of items without CommentsValue (AUD)
3TRACES4129,628.574129,628.57
4CMP1210,585.3413,528.45
Sheet1
 
Upvote 0
I changed this line to 8

If CSng(dic1.Item(ka(i, 8))) Then

Now i get this which is very close, but still not picking up the 4 Traces item, only 3 is being picked up

Excel Workbook
BSTUV
1>90
2TeamNo. of itemsValue (AUD)No. of items without CommentsValue (AUD)
3TRACES3121,000.003121,000.00
4CMP1210,585.3413,528.45
5LEHMAN11,568.7211,568.72
Sheet1
 
Upvote 0
Its not picking up this line

Excel Workbook
ABCDEFGHIJKLMNOP
3CategoryRecAccountValue DateEntry DateTypeAmountCCYAgeSourceRef1Ref2Ref3ReferenceLast UpdateComments
4TESTTESTTESTTESTTESTTEST8268.57AUD1456TRACESTESTTESTTESTTEST
Rec
 
Upvote 0
Hi,

Here you go

Code:
Sub BTest()

    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
    Dim dblAmount   As Double
    Dim strCurrency As String
    Dim lngAge      As Long
    Dim strSource   As String
    Dim strComments As String
    
    
    '// adjust to suit
    Const SourceShtName         As String = "Rec"
    Const SourceDataRange       As String = "rngSource"
    Const StartRow              As Long = 2
'    Const FxDataRange           As String = "R3:S16"
    Const DestRange             As String = "B1"
    '//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("rngSource"))
    End With
    
    With Worksheets("FX")
        Fx = .Range("rngFX")
    End With
        
    With Worksheets("Sheet1")
        r = .Range("b" & .Rows.Count).End(xlUp).Row
        tmp = .Range("B3:B" & 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)

    For i = StartRow To UBound(ka, 1)
        '//Adjust to suit
        dblAmount = CDbl(Trim$(ka(i, 7)))
        strCurrency = Trim$(ka(i, 8))
        lngAge = Trim$(ka(i, 9))
        strSource = Trim$(ka(i, 10))
        strComments = Trim$(ka(i, 16))
        '//
        Idx = Evaluate("=lookup(" & lngAge & Age & ")")
        If Not IsError(Idx) Then
            If dic2.exists(strSource) Then
                Flg = True
                t = dic2.Item(strSource)
                k(t, 1) = strSource
                k(t, Idx * 4 - 4 + 2) = k(t, Idx * 4 - 4 + 2) + 1
                If CSng(dic1.Item(strCurrency)) Then
                    k(t, Idx * 4 - 4 + 3) = k(t, Idx * 4 - 4 + 3) + dblAmount / dic1.Item(strCurrency)
                End If
                If Len(strComments) = 0 Then
                    k(t, Idx * 4 - 4 + 4) = k(t, Idx * 4 - 4 + 4) + 1
                    If CSng(dic1.Item(strCurrency)) Then
                        k(t, Idx * 4 - 4 + 5) = k(t, Idx * 4 - 4 + 5) + dblAmount / dic1.Item(strCurrency)
                    End If
                End If
            End If
        End If
    Next

    If Flg Then
        With Worksheets("Sheet1")
            .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, 1).Resize(n, 20).SpecialCells(4).Value = 0
                On Error GoTo 0
            End With
        End With
    End If

End Sub

HTH
 
Last edited:
Upvote 0
I get a type mismatch on this line run time error 13

dblAmount = CDbl(Trim$(ka(i, 7)))
 
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,558
Members
452,928
Latest member
101blockchains

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