Solved

VBA code

Posted on 2013-12-19
23
281 Views
Last Modified: 2013-12-25
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,
0
Comment
Question by:marian68
  • 8
  • 6
  • 5
  • +1
23 Comments
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 39729071
Hi,

Could you send a dummy?

Regards
0
 

Author Comment

by:marian68
ID: 39729095
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
0
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 39729100
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
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:marian68
ID: 39729136
I am trying to put you code in a macro but it becomes red.
0
 
LVL 33

Assisted Solution

by:Rob Henson
Rob Henson earned 50 total points
ID: 39729145
If you want the groups titled in some way, you can add a vlookup to your data.

Create a small table with your data in 3 columns; assume Sheet2!A1:C11

Lower  Upper  Title
0          6          Group 1
6          6.5       Group 2
6.6       7          Group 3
etc

Then assuming Field1 is in column A, the vlookup would be:

=vlookup(A2,Sheet2!$A$1:$C$11,3)

Then you can use a Pivot table to group the data including the Group Title with a count of one the fields in the data area.

Thanks
Rob H
0
 

Author Comment

by:marian68
ID: 39729159
Hi Rob,
 
Thank you Rob.
I would like better to have a macro to to this automatically if it is possibly.
0
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 39729162
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

0
 
LVL 33

Expert Comment

by:Rob Henson
ID: 39729165
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
0
 
LVL 33

Expert Comment

by:Rob Henson
ID: 39729209
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
0
 

Author Comment

by:marian68
ID: 39729213
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
0
 
LVL 35

Assisted Solution

by:[ fanpages ]
[ fanpages ] earned 50 total points
ID: 39729237
Hi,

Perhaps the final grouping is incorrect due to rounding of decimal values, or the fact that values that are exactly 10 are not being counted.

Note:

        Case Is > 10
            int100plus = int100plus + 1

Open in new window


Should be...

        Case Is >= 10
            int100plus = int100plus + 1

Open in new window


You could also try this change:

    Select Case c.Value
        Case Is >= 0, Is < 6
            int00_60 = int00_60 + 1
        Case Is >= 6, Is < 6.5
            int60_65 = int60_65 + 1
        Case Is >= 6.5, Is < 7
            int65_70 = int65_70 + 1
        Case Is >= 7, Is < 7.5
            int70_75 = int70_75 + 1
        Case Is >= 7.5, Is < 8
            int75_80 = int75_80 + 1
        Case Is >= 8, Is < 8.5
            int80_85 = int80_85 + 1
        Case Is >= 8.5, Is < 9
            int85_90 = int85_90 + 1
        Case Is >= 9, Is < 10
            int90_100 = int95_100 + 1
        Case Is >= 10
            int100plus = int100plus + 1
    End Select

Open in new window


BFN,

fp.
0
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 39729241
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
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 39729245
^ values that are exactly 10 are still not being counted.
0
 
LVL 50

Accepted Solution

by:
Rgonzo1971 earned 400 total points
ID: 39729258
Hi

Another go

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

0
 

Author Comment

by:marian68
ID: 39729264
Hi guys,

Rgonzo1971 - your code works like a charm.
Thank you guys,
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 39729265
You're welcome.
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 39729306
I suggest Rgonzo1971's comment that should be accepted is "ID: 39729258" instead of the one you have quoted.
0
 

Author Comment

by:marian68
ID: 39737611
Thank you guys
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 39737683
The accepted comment is still not the actual solution.
0
 

Author Comment

by:marian68
ID: 39737935
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
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 39738224
Thanks Modalot.
0
 

Author Comment

by:marian68
ID: 39739049
Thanks Modalot
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question