How to create a consecutive between 2 number in excel

Saher Naji

Board Regular
Joined
Dec 19, 2019
Messages
76
Office Version
  1. 2013
Platform
  1. Windows
for example, I have 33 in cell A1, and 40 in cell B1, I want a formula in cell C1, to write an arithmetic sequence from 33 to 40 like this: 33, 34, 35, 36, 37, 38, 39, 40
1655670673015.png
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
actually the numbers I have like this: for example: 33-38 in cell A1, I tried to right this formula: "=RIGHT(B1,2)-LEFT(A1,2)" the result will be 5, I thought that I can use this number to generate a sequence starting from 33 (5) times to reach to 38, but I did not find a formula to help me
 
Upvote 0
One way using a VBA custom function.

ExpandRange.xlsm
ABC
1334033, 34, 35, 36, 37, 38, 39, 40
Sheet3
Cell Formulas
RangeFormula
C1C1=CONSEC(A1,B1)


VBA Code:
Function CONSEC(ln As Integer, rn As Integer)
With CreateObject("Scripting.Dictionary")
    For i = ln To rn
        .Add i, True
    Next i
    CONSEC = Join(.keys, ", ")
End With
End Function
 
Upvote 0
Solution
Thanks
Is it because I don't have Office 2019, or I entered the VBA code somewhere wrong?
1655671895659.png
 
Upvote 0
No, it should work fine on 2013. You get into the VB editor by hitting Alt+F11. The go to Insert, then select Module. Paste the code there, then use the formula like normal.
 
Upvote 0
That's great, and working very well, but could I apply this into one cell, the original data I have in one column, like this:
Check column A please,
as you see I hided Columns A and B, which contain (Left) and (Right) formulas

1655675093083.png
 
Upvote 0
I'm sorry, it's working,
I used your formula with small adjustment and it's working perfect

=CONSEC(LEFT(A1,2),RIGHT(A1,2))

Thanks a lot♥
 
Upvote 0
@Irobbo314's function is what you use per your last post or have the function handle it.
If you always use 2-digits, either way will work.

Code:
Function CONSEC2(ln As Variant)
Dim rn As Integer
rn = 1 * Right(ln, Len(ln) - InStr(1, ln, "-"))
ln = 1 * Left(ln, InStr(1, ln, "-") - 1)
With CreateObject("Scripting.Dictionary")

    For i = ln To rn
        .Add i, True
    Next i
    CONSEC2 = Join(.keys, ", ")
End With
End Function
 
Upvote 0
@Irobbo314's function is what you use per your last post or have the function handle it.
If you always use 2-digits, either way will work.

Code:
Function CONSEC2(ln As Variant)
Dim rn As Integer
rn = 1 * Right(ln, Len(ln) - InStr(1, ln, "-"))
ln = 1 * Left(ln, InStr(1, ln, "-") - 1)
With CreateObject("Scripting.Dictionary")

    For i = ln To rn
        .Add i, True
    Next i
    CONSEC2 = Join(.keys, ", ")
End With
End Function
what about the formula in this case?
I tested it with the same formula, I changed it from CONSEC TO CONSEC2, but the result: (#VALUE!)
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,160
Members
453,021
Latest member
Justyna P

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