Link to home
Start Free TrialLog in
Avatar of marian68
marian68

asked on

VBA code

Hi guys,

I have a report with 10 fields and thousands of records in Excel.
The first filed consists of numbers starting with 0.
Can anyone give the VBA code to obtain another report with only 2 fields grouping the values
from the first field like:

Field1            Field2
0-6                Number of records
6 -6,5             Number of records
6,5-7             Number of records
7-7,5             Number of records
7,5-8             Number of records
8-8,5             Number of records
8,5-9             Number of records
9-10              Number of records
10 and up   Number of records

Thank you,
Avatar of Rgonzo1971
Rgonzo1971

Hi,

Could you send a dummy?

Regards
Avatar of marian68

ASKER

It would take me a  lot of time to change the data.
Let me know what additional details you want to give you.
Thank you
Hi,

You could use formulas like these

=COUNTIFS(Sheet1!A:A,">="&CountResult!B2,Sheet1!A:A,"<"&CountResult!C2)

Open in new window


see example
Countifs1.xlsx
Regards
I am trying to put you code in a macro but it becomes red.
SOLUTION
Avatar of Rob Henson
Rob Henson
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Rob,
 
Thank you Rob.
I would like better to have a macro to to this automatically if it is possibly.
Hi,

pls try

Sub macro()

int00_60 = 0
int60_65 = 0
int65_70 = 0
int70_75 = 0
int75_80 = 0
int80_85 = 0
int85_90 = 0
int90_100 = 0
int100plus = 0

For Each c In Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
    Select Case c.Value
        Case 0 To 6 - 0.000001
            int00_60 = int00_60 + 1
        Case 6 To 6.5 - 0.000001
            int60_65 = int60_65 + 1
        Case 6.5 To 7 - 0.000001
            int65_70 = int65_70 + 1
        Case 7 To 7.5 - 0.000001
            int70_75 = int70_75 + 1
        Case 7.5 To 8 - 0.000001
            int75_80 = int75_80 + 1
        Case 8 To 8.5 - 0.000001
            int80_85 = int80_85 + 1
        Case 8.5 To 9 - 0.000001
            int85_90 = int85_90 + 1
        Case 9 To 10 - 0.000001
            int90_100 = int95_100 + 1
        Case Is > 10
            int100plus = int100plus + 1
    End Select
Next
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Activate
ActiveSheet.Name = "Result"

Range("A1") = "Brackets"
Range("A2") = "0-6"
Range("A3") = "6-6,5"
Range("A4") = "6,5-7"
Range("A5") = "7-7,5"
Range("A6") = "7,5-8"
Range("A7") = "8-8,5"
Range("A8") = "8,5-9"
Range("A9") = "'9-10"
Range("A10") = "10 and up"

Range("B1") = "Number of records"
Range("B2") = int00_60
Range("B3") = int60_65
Range("B4") = int65_70
Range("B5") = int70_75
Range("B6") = int75_80
Range("B7") = int80_85
Range("B8") = int85_90
Range("B9") = int90_100
Range("B10") = int100plus

End Sub

Open in new window

I know you can't upload sample data but can you give us an indication of the layout of your file? Headers, sheet names etc.

We can then put together something for you.

Thanks
Rob
I was about to suggest something similar to RGonzo1971 but I would have used a helper column in the data on which I was then going to create a Pivot.

Thanks
Rob H
Hi Rgonzo1971

Thank you for your code. It partially works. The only problem is that the report has 33326 records
and the result of your code is 33289.
The difference comes from the interval 9-10. Your code brings 1 record but in my report I have 38 records.
Can you find the problem.

Thanks a lot
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
HI,

Corrected code delete the result sheet before running the macro
Sub macro()

int00_60 = 0
int60_65 = 0
int65_70 = 0
int70_75 = 0
int75_80 = 0
int80_85 = 0
int85_90 = 0
int90_100 = 0
int100plus = 0

For Each c In Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
    Select Case c.Value
        Case 0 To 6 - 0.000001
            int00_60 = int00_60 + 1
        Case 6 To 6.5 - 0.000001
            int60_65 = int60_65 + 1
        Case 6.5 To 7 - 0.000001
            int65_70 = int65_70 + 1
        Case 7 To 7.5 - 0.000001
            int70_75 = int70_75 + 1
        Case 7.5 To 8 - 0.000001
            int75_80 = int75_80 + 1
        Case 8 To 8.5 - 0.000001
            int80_85 = int80_85 + 1
        Case 8.5 To 9 - 0.000001
            int85_90 = int85_90 + 1
        Case 9 To 10 - 0.000001
            int90_100 = int90_100 + 1
        Case Is > 10
            int100plus = int100plus + 1
    End Select
Next
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Activate
ActiveSheet.Name = "Result"

Range("A1") = "Brackets"
Range("A2") = "0-6"
Range("A3") = "6-6,5"
Range("A4") = "6,5-7"
Range("A5") = "7-7,5"
Range("A6") = "7,5-8"
Range("A7") = "8-8,5"
Range("A8") = "8,5-9"
Range("A9") = "'9-10"
Range("A10") = "10 and up"

Range("B1") = "Number of records"
Range("B2") = int00_60
Range("B3") = int60_65
Range("B4") = int65_70
Range("B5") = int70_75
Range("B6") = int75_80
Range("B7") = int80_85
Range("B8") = int85_90
Range("B9") = int90_100
Range("B10") = int100plus

End Sub

Open in new window

Regards
^ values that are exactly 10 are still not being counted.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi guys,

Rgonzo1971 - your code works like a charm.
Thank you guys,
You're welcome.
I suggest Rgonzo1971's comment that should be accepted is "ID: 39729258" instead of the one you have quoted.
Thank you guys
The accepted comment is still not the actual solution.
Yes you are right.
Fortunately in my reports I had no value of exactly 10.00.
Anyway I corrected the code. Case Is >= 10
Thanks a lot
Merry Christmas
Thanks Modalot.
Thanks Modalot