Create a custom range number list till max input

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,422
Office Version
  1. 2010
Using Excel 2010
Hello,

Here below in the sheet I have shown 3 examples but need macro or formula which I want to create a custom range number list till max input….
Example1…D3 max range 890 friction 50 create range from 1 to 890 in range C6:E23

Example2…H3 max range 1249 friction 75 create range from 1 to 1249 in range G6:I22

Example2…L3 max range 930 friction 100 create range from 1 to 930 in range K6:M15

Note: I want a formula or a macro which can be used in Excel 2000 also.
Here is an example sheet with expected results…

Sort By Range.xls
ABCDEFGHIJKLMN
1
2Max RangeMax RangeMax Range
38901249930
4Friction 50Friction 75Friction 100
5
61To501To751To100
751To10076To150101To200
8101To150151To225201To300
9151To200226To300301To400
10201To250301To375401To500
11251To300376To450501To600
12301To350451To525601To700
13351To400526To600701To800
14401To450601To675801To900
15451To500676To750901To930
16501To550751To825
17551To600826To900
18601To650901To975
19651To700976To1.050
20701To7501.051To1.125
21751To8001.126To1.200
22801To8501.201To1.249
23851To890
24
Sheet1


Regards,
Moti
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Simpler if you could put that Friction number in its own cell.

Also remember that with XL2BB, no need to show irrelevant columns/rows (eg columns A:B here). Either hide them or just not include them in your selection. Keeps your mini sheet smaller.

Cell Formulas
RangeFormula
D6:D24,L6:L24,H6:H24D6=IF(C6="","","To")
E6:E24,M6:M24,I6:I24E6=IF(C6="","",MIN(C6+E$4-1,D$3))
C7:C24,K7:K24,G7:G24C7=IF(LOOKUP(9^9,C$6:C6)+E$4>D$3,"",C6+E$4)
 
Upvote 0
Try the next alternative.
It would be more convenient if you put the friction value in one cell and the title in another cell, see my example.

Formulas start in cell C6
Cell Formulas
RangeFormula
K6:K25,G6:G25,C6:C25C6=IFERROR(IF(E5+1<D$3,E5+1,""),"")
L6:L25,H6:H25,D6:D25D6=IF(E6="","","To")
M6:M25,I6:I25,E6:E25E6=IF(C6="","",IF(D$4+E5<=D$3,D$4+E5,D$3))


--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
 
Upvote 0

motilulla because you can never have too many solutions, here is another one. If you have any problems, questions or suggestions, let us know.
VBA Code:
Sub Prog2()

Dim Count2 As Double
Dim Fric As Integer
Dim CellCnt As Integer

Count = Range("D3")
Fric = Range("E4")
CellCnt = Cells(Rows.Count, "C").End(xlUp).Row
    Range(Range("C6"), Cells(CellCnt, 5)).Select
    Selection.Delete Shift:=xlToLeft
    
    
Range("C6") = 1
Range("D6") = "To"
Range("E6") = Fric

Count2 = Range("D3") / Range("E4")

For i = 7 To (5 + Count2)

Cells(i, 3) = Cells(i - 1, 5) + 1
Cells(i, 4) = "To"
Cells(i, 5) = Fric + Cells(i - 1, 5)

Next i

If Range("D3") > Cells(i, 5) Then
    Cells(i, 3) = Cells(i - 1, 5) + 1
    Cells(i, 4) = "To"
    Cells(i, 5) = Range("D3")
End If

Columns("C:E").Select
With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
Range("C6").Select
End Sub

Max Range
1249
Friction75
1To75
76To150
151To225
226To300
301To375
376To450
451To525
526To600
601To675
676To750
751To825
826To900
901To975
976To1050
1051To1125
1126To1200
1201To1249
 
Upvote 1
Simpler if you could put that Friction number in its own cell.

Also remember that with XL2BB, no need to show irrelevant columns/rows (eg columns A:B here). Either hide them or just not include them in your selection. Keeps your mini sheet smaller.

Cell Formulas
RangeFormula
D6:D24,L6:L24,H6:H24D6=IF(C6="","","To")
E6:E24,M6:M24,I6:I24E6=IF(C6="","",MIN(C6+E$4-1,D$3))
C7:C24,K7:K24,G7:G24C7=IF(LOOKUP(9^9,C$6:C6)+E$4>D$3,"",C6+E$4)
Hello Peter_SSs, I want to conform that your formula worked perfect to my both versions.

I will follow-up your suggestion next time and will defiantly keep mini sheet smaller, thank you for your guidance.

Thank you for your kind help.

Nice weekend and wish you Good Luck.

Kind Regards,
Moti :)
 
Upvote 0
Try the next alternative.
It would be more convenient if you put the friction value in one cell and the title in another cell, see my example.

Formulas start in cell C6
Cell Formulas
RangeFormula
K6:K25,G6:G25,C6:C25C6=IFERROR(IF(E5+1<D$3,E5+1,""),"")
L6:L25,H6:H25,D6:D25D6=IF(E6="","","To")
M6:M25,I6:I25,E6:E25E6=IF(C6="","",IF(D$4+E5<=D$3,D$4+E5,D$3))


--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
Hello DanteAmor, I can say your formula worked perfectly with version 2010 but not with 2000 I guess function “=IFERROR” does not have in version 2000 may be that can be the reason.

Thank you for your kind help.

Nice weekend and wish you Good Luck.

Kind Regards,
Moti :)
 
Upvote 0

motilulla because you can never have too many solutions, here is another one. If you have any problems, questions or suggestions, let us know.
VBA Code:
Sub Prog2()

Dim Count2 As Double
Dim Fric As Integer
Dim CellCnt As Integer

Count = Range("D3")
Fric = Range("E4")
CellCnt = Cells(Rows.Count, "C").End(xlUp).Row
    Range(Range("C6"), Cells(CellCnt, 5)).Select
    Selection.Delete Shift:=xlToLeft
 
 
Range("C6") = 1
Range("D6") = "To"
Range("E6") = Fric

Count2 = Range("D3") / Range("E4")

For i = 7 To (5 + Count2)

Cells(i, 3) = Cells(i - 1, 5) + 1
Cells(i, 4) = "To"
Cells(i, 5) = Fric + Cells(i - 1, 5)

Next i

If Range("D3") > Cells(i, 5) Then
    Cells(i, 3) = Cells(i - 1, 5) + 1
    Cells(i, 4) = "To"
    Cells(i, 5) = Range("D3")
End If

Columns("C:E").Select
With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
Range("C6").Select
End Sub

Max Range
1249
Friction75
1To75
76To150
151To225
226To300
301To375
376To450
451To525
526To600
601To675
676To750
751To825
826To900
901To975
976To1050
1051To1125
1126To1200
1201To1249
Hello Ezguy4u, I like your VBA solution it is more practical. I think it does not need to be included 2 following lines code can be removed.
VBA Code:
Range(Range("C6"), Cells(CellCnt, 5)).Select
Selection.Delete Shift:=xlToLeft
Secondly it works perfect till Friction 32767, as I increase to 32768 it gives me an error “6” and code stop at line below
Code:
Fric = Range("E4")
and highlight in yellow. For example you try put max range 5.000.000 and Friction 32768 it will produce the error "6". Please can you take a look at it?

Thank you for your kind help.

Nice weekend and wish you Good Luck.

Kind Regards,
Moti:)
 
Upvote 0
Moti the line "CellCnt = Cells(Rows.Count, "C").End(xlUp).Row" is also a part of those 2 lines of code and what they all do is delete the previous calculation. Now you could take those 3 lines out, but you would have to manually delete the previous calculation. Now I have to ask you a question, is 5.000.000 the same as 5,000,000? In other words, are you using a period instead of a comma? Even better is to use no period or commas. Just 5000000 is better. Let's try another Dim statement. Again if you have any questions, let us know. Maybe some A+ student can suggest a better Dim that Variant.

VBA Code:
Sub Prog3()

Dim Fric As Variant
Dim CellCnt As Integer
Dim Count2 As Variant

Count = Range("D3")
Fric = Range("E4")
CellCnt = Cells(Rows.Count, "C").End(xlUp).Row
    Range(Range("C6"), Cells(CellCnt, 5)).Select
    Selection.Delete Shift:=xlToLeft
    
    
Range("C6") = 1
Range("D6") = "To"
Range("E6") = Fric

Count2 = Range("D3") / Range("E4")

For i = 7 To (5 + Count2)

Cells(i, 3) = Cells(i - 1, 5) + 1
Cells(i, 4) = "To"
Cells(i, 5) = Fric + Cells(i - 1, 5)

Next i

If Range("D3") > Cells(i, 5) Then
    Cells(i, 3) = Cells(i - 1, 5) + 1
    Cells(i, 4) = "To"
    Cells(i, 5) = Range("D3")
End If

Columns("C:E").Select
With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
Range("C6").Select
End Sub
 
Upvote 1
Moti the line "CellCnt = Cells(Rows.Count, "C").End(xlUp).Row" is also a part of those 2 lines of code and what they all do is delete the previous calculation. Now you could take those 3 lines out, but you would have to manually delete the previous calculation.

VBA Code:
Sub Prog3()

Dim Fric As Variant
Dim CellCnt As Integer
Dim Count2 As Variant

End Sub
Hello Ezguy4u, I am sorry you are correct I did not realized these lines delete all previous calculations while every time macro is run with new friction or max range it is absolutely perfect.
Now I have to ask you a question, is 5.000.000 the same as 5,000,000? In other words, are you using a period instead of a comma? Even better is to use no period or commas. Just 5000000 is better.
I was using the period separator “5.000.000 the same as 5,000,000” as per different regions settings. But I will follow-up your suggestion.
Let's try another Dim statement.
Wow Ezguy4u, this Dim statement worked like magic 100% accurate! 🙌
Maybe some A+ student can suggest a better Dim that Variant.
No need you are legend! 🤝
Again if you have any questions, let us know.
All is worked well here are the example results….

Sort By Range.xls
CDE
1
2Max Range
348000000
4Friction13983816
5
61To13983816
713983817To27967632
827967633To41951448
941951449To48000000
10
Sheet1-1


I appreciate your sincere help. I wish you Good Luck!

Kind Regards,

Moti :)
 
Upvote 0
what they all do is delete the previous calculation.
A little risky in my opinion as if there was no previous calculation they delete everything in that header area.
I would also recommend not using one of vba's standard words as a variable name (eg Count) as that can sometimes lead to disastrous results.

Let's try another Dim statement
Here is another option with no Dim statements at all & no looping to create each row individually.
This also deletes any previous calculation but preserves the heading section if no prior calc.

VBA Code:
Sub CreateCustomRange()
  Range("C1", Range("E" & Rows.Count).End(xlUp)).Offset(5).Clear
  With Range("C6:E6").Resize(Range("D3").Value / Range("E4").Value + 1)
    .Formula = Array("=C5+E$4", "To", "=min(E5+E$4,D$3)")
    .Cells(1).Value = 1
    .Value = .Value
    .Columns(2).HorizontalAlignment = xlCenter
    .Font.Bold = True
    If .Cells(.Rows.Count, 1).Value > Range("D3").Value Then .Rows(.Rows.Count).Clear
  End With
End Sub

Example:

motilulla.xlsm
CDE
1
2Max Range
348000000
4Friction13983816
5
61To13983816
713983817To27967632
827967633To41951448
941951449To48000000
10
Sheet4
 
Upvote 1
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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