Convert numbers into intervals

boboivan

Board Regular
Joined
Feb 18, 2013
Messages
68
Hi guys!


Please let me know if I can convert a particular number (e.g. 5) into an interval (1-5) and if I can count them as follows:


Column M Column N
(Number of pages) (Number interval)
2 1-2
5 3-7
3 8-10
... ...


Thanks!
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Try this, assuming your data starts in M2 & N2 - You only need to enter the formula in N3 and drag down

Excel 2010
MN
1PagesIntervals
221-2
353-7
438-10
5411-14
62515-39
75540-94
81295-106
91000107-1106

<tbody>
</tbody>
Sheet4

Worksheet Formulas
CellFormula
N2=1&"-"&M2+0
N3=SUBSTITUTE(RIGHT(N2,FIND("-",N2)),"-","")+1&"-"&SUM($M$2:M3)
N4=SUBSTITUTE(RIGHT(N3,FIND("-",N3)),"-","")+1&"-"&SUM($M$2:M4)
N5=SUBSTITUTE(RIGHT(N4,FIND("-",N4)),"-","")+1&"-"&SUM($M$2:M5)
N6=SUBSTITUTE(RIGHT(N5,FIND("-",N5)),"-","")+1&"-"&SUM($M$2:M6)
N7=SUBSTITUTE(RIGHT(N6,FIND("-",N6)),"-","")+1&"-"&SUM($M$2:M7)
N8=SUBSTITUTE(RIGHT(N7,FIND("-",N7)),"-","")+1&"-"&SUM($M$2:M8)
N9=SUBSTITUTE(RIGHT(N8,FIND("-",N8)),"-","")+1&"-"&SUM($M$2:M9)

<tbody>
</tbody>

<tbody>
</tbody>

Hello there again!

Running some tests today with the formulas, I noticed 2 problems.
1. If the interval has in the left side 1 digit number and in the right side more than 2 digits number, in the next row (the left side of the interval) appears error, as in the example below:

Pages Interval
1 1-1
7 2-8
98 9-106
91 7-197
6 98-203
<table class="wysiwyg_dashes" verdana,="" arial,="" tahoma,="" calibri,="" geneva,="" sans-serif;"="" style="width: " width=""><tbody></tbody></table>
2. If I add filters in the table, the intervals remain unchanged, which is not helpful at all to me. Is there anything to do about it?
Thanks a lot!
 
Last edited:
Upvote 0
Fill your M column with page numbers then run this code.

Code:
Option Explicit

Sub calc()
    
    Dim nPage As Long
    Dim prevInterval As String
    Dim prevIntervalArr() As String
    Dim newInterval1 As Long
    Dim newInterval2 As Long
    Dim i As Long
    Dim j As Long
    
    Dim ref As Worksheet
    Set ref = ThisWorkbook.Sheets("Sheet1")
    
    For i = 2 To ref.Range("M" & Rows.Count).End(xlUp).Row
            
        If i = 2 Then
            ref.Range("N2").Value = CStr(1 & " - " & ref.Range("M2").Value)
        Else
        
        nPage = ref.Range("M" & i).Value
        
        prevIntervalArr = Split(ref.Range("N" & i).Offset(-1, 0).Value, " - ", 2)
        For j = LBound(prevIntervalArr) To UBound(prevIntervalArr)
            prevInterval = prevIntervalArr(1)
        Next j


        newInterval1 = CLng(prevInterval) + 1
        newInterval2 = newInterval1 + nPage - 1
        
        ref.Range("N" & i).Value = CStr(newInterval1 & " - " & newInterval2)
        End If
    Next i
    
End Sub

ALT + F11 to open VBA editor. Right click to add a new module -> paste this code then hit F5 to run! Good luck
 
Upvote 0
I've adjusted the formula to handle this. Should work now.

=RIGHT(N2,LEN(N2)-FIND("-",N2))+1&"-"&SUM($M$2:M3)
 
Upvote 0
Fill your M column with page numbers then run this code.

Code:
Option Explicit

Sub calc()
    
    Dim nPage As Long
    Dim prevInterval As String
    Dim prevIntervalArr() As String
    Dim newInterval1 As Long
    Dim newInterval2 As Long
    Dim i As Long
    Dim j As Long
    
    Dim ref As Worksheet
    Set ref = ThisWorkbook.Sheets("Sheet1")
    
    For i = 2 To ref.Range("M" & Rows.Count).End(xlUp).Row
            
        If i = 2 Then
            ref.Range("N2").Value = CStr(1 & " - " & ref.Range("M2").Value)
        Else
        
        nPage = ref.Range("M" & i).Value
        
        prevIntervalArr = Split(ref.Range("N" & i).Offset(-1, 0).Value, " - ", 2)
        For j = LBound(prevIntervalArr) To UBound(prevIntervalArr)
            prevInterval = prevIntervalArr(1)
        Next j


        newInterval1 = CLng(prevInterval) + 1
        newInterval2 = newInterval1 + nPage - 1
        
        ref.Range("N" & i).Value = CStr(newInterval1 & " - " & newInterval2)
        End If
    Next i
    
End Sub

ALT + F11 to open VBA editor. Right click to add a new module -> paste this code then hit F5 to run! Good luck

Thanks maestro! Unfortunately it doesn't work. I followed all the steps. When I tried to debug, after pressing F5, the following line of the code has been marked: Set ref = ThisWorkbook.Sheets("Sheet1"). I've changed the name of the sheet and still nothing happens.
 
Upvote 0
Whats your sheet name then? you can go by sheet number instead? Has an error been thrown? what was it
 
Upvote 0
I've adjusted the formula to handle this. Should work now.

=RIGHT(N2,LEN(N2)-FIND("-",N2))+1&"-"&SUM($M$2:M3)


Thank you again real excel guru! :)
Works perfectly now.

Any idea about the second problem I have? This is very important for the whole idea of the table. When apply filters in my table I'd like the intervals to get changed accordingly to the filtered values in column M.
 
Upvote 0
Whats your sheet name then? you can go by sheet number instead? Has an error been thrown? what was it

The sheet name is 2013. I've tried sheet number, sheet name, all options I know. Nothing! It stills marking the line "Set ref = ThisWorkbook.Sheets("Sheet1")"
 
Upvote 0
Set ref = ThisWorkbook.Sheets("2013")

what error are you getting, run time? what number what does it say

what version of excel are you using

maybe
set ref = ActiveWorkbook.Sheets("2013")
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,791
Members
451,589
Latest member
Harold14

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