Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 303
  • Last Modified:

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,
0
marian68
Asked:
marian68
  • 8
  • 6
  • 5
  • +1
3 Solutions
 
Rgonzo1971Commented:
Hi,

Could you send a dummy?

Regards
0
 
marian68Author Commented:
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
 
Rgonzo1971Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
marian68Author Commented:
I am trying to put you code in a macro but it becomes red.
0
 
Rob HensonIT & Database AssistantCommented:
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
 
marian68Author Commented:
Hi Rob,
 
Thank you Rob.
I would like better to have a macro to to this automatically if it is possibly.
0
 
Rgonzo1971Commented:
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
 
Rob HensonIT & Database AssistantCommented:
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
 
Rob HensonIT & Database AssistantCommented:
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
 
marian68Author Commented:
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
 
[ fanpages ]IT Services ConsultantCommented:
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
 
Rgonzo1971Commented:
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
 
[ fanpages ]IT Services ConsultantCommented:
^ values that are exactly 10 are still not being counted.
0
 
Rgonzo1971Commented:
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
 
marian68Author Commented:
Hi guys,

Rgonzo1971 - your code works like a charm.
Thank you guys,
0
 
[ fanpages ]IT Services ConsultantCommented:
You're welcome.
0
 
[ fanpages ]IT Services ConsultantCommented:
I suggest Rgonzo1971's comment that should be accepted is "ID: 39729258" instead of the one you have quoted.
0
 
marian68Author Commented:
Thank you guys
0
 
[ fanpages ]IT Services ConsultantCommented:
The accepted comment is still not the actual solution.
0
 
marian68Author Commented:
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
 
[ fanpages ]IT Services ConsultantCommented:
Thanks Modalot.
0
 
marian68Author Commented:
Thanks Modalot
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 6
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now