combination

farasatbalooch

New Member
Joined
Aug 12, 2023
Messages
28
Office Version
  1. 2013
Platform
  1. Windows
i want to make numbers combination from 1 to 49
5 numbers combination for example ( 1 2 3 4 5 ) can you
tell me the code it will be a graet help i am using
MS EXCEL 2013.

thanks.
 
Another good reason NOT to oversimplify your code or what you are trying to do, if you are not comfortable modifying the code we create.
Quite literally, all you have to do is copy/paste your "vals" line over what I have in my code, i.e.

Rich (BB code):
Sub Maja2()

    Dim K As Long
    Dim cl As Long
    Dim rw As Long
    
    cl = 1
    rw = 1
    
    vals = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49")

    Application.ScreenUpdating = False
    
    For Each a In vals
        For Each b In vals
            For Each c In vals
                For Each d In vals
                    For Each e In vals
                        Cells(rw, cl) = a & b & c & d & e
                        If rw = Rows.Count Then
                            rw = 1
                            cl = cl + 1
                        Else
                            rw = rw + 1
                        End If
                    Next e
                Next d
            Next c
        Next b
    Next a
    
    Application.ScreenUpdating = True
    
    MsgBox "Macro complete"
    
End Sub
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Another good reason NOT to oversimplify your code or what you are trying to do, if you are not comfortable modifying the code we create.
Quite literally, all you have to do is copy/paste your "vals" line over what I have in my code, i.e.

Rich (BB code):
Sub Maja2()

    Dim K As Long
    Dim cl As Long
    Dim rw As Long
   
    cl = 1
    rw = 1
   
    vals = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49")

    Application.ScreenUpdating = False
   
    For Each a In vals
        For Each b In vals
            For Each c In vals
                For Each d In vals
                    For Each e In vals
                        Cells(rw, cl) = a & b & c & d & e
                        If rw = Rows.Count Then
                            rw = 1
                            cl = cl + 1
                        Else
                            rw = rw + 1
                        End If
                    Next e
                Next d
            Next c
        Next b
    Next a
   
    Application.ScreenUpdating = True
   
    MsgBox "Macro complete"
   
End Sub
Rich (BB code):
Sir if I copy this code is it work for me?
 
Upvote 0
Sir it's not working I just tried this but it's not working excel is not responding
You may have to wait a while to let it run. It has to update millions of cells, which could take a while.
I would probably wait 10-15 minutes to see if it will finish.

Out of curiosity, exactly what are you trying to do here?
It looks like maybe you are trying to do something with a Lotto, returning all possible 5 number draws out of 49 numbers.
But for what purpose?

There may be other better ways to do what you want, if we understand why you are trying to do this.
 
Upvote 0
You may have to wait a while to let it run. It has to update millions of cells, which could take a while.
I would probably wait 10-15 minutes to see if it will finish.

Out of curiosity, exactly what are you trying to do here?
It looks like maybe you are trying to do something with a Lotto, returning all possible 5 number draws out of 49 numbers.
But for what purpose?

There may be other better ways to do what you want, if we understand why you are trying

Ok sir let me run the code and wait then I will tell you if I have any problem thanks and sir thats right I am making 5 number combination for the lotto
 
Upvote 0
Ok sir let me run the code and wait then I will tell you if I have any problem thanks and sir thats right I am making 5 number combination for the lotto
But why are you trying to create every possible combination?
What are you hoping to do with that?

If you just want a few random samples, that can probably be done fairly quickly without having to try to create every possible option.
 
Upvote 0
You may want to take a look here, in which Bill Jelen addresses your question.
 
Upvote 0
But why are you trying to create every possible combination?
What are you hoping to do with that?

If you just want a few random samples, that can probably be done fairly quickly without having to try to create every possible option.


This code is not working sir I have waited around 1 hour but still it's not working
 
Upvote 0
This code is not working sir I have waited around 1 hour but still it's not working
See the last post I posted, in which Bill Jelen (MrExcel himself) does a demo of what you are trying to do.
It is similar, but it looks like there are a few differences that may make a differences.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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