MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Apr 30th, 2002, 01:33 PM   #1
waderw
Board Regular
 
Join Date: Apr 2002
Posts: 85
Default

I am looking for some help/code to test to see if certain values in a selected cell are consecutive. For ex. in cell "B6" I have the numbers "1,2,3,5,7,8,9". I want to format "1,2,3" and "7,8,9" as "1-3" and "7-9" and then add the 5 so my output in the cell looks like this: "1-3,5,7-9".

Any suggestions or recommendations?
waderw is offline   Reply With Quote
Old Apr 30th, 2002, 04:52 PM   #2
Chris Davison
MrExcel MVP
 
Join Date: Feb 2002
Location: Millbank, London, UK
Posts: 1,790
Default

do you digits only go as high as "9" or do they go higher ?

(ie 456)

are they all integers, or could some be decimals

(ie 45.6)

how many numbers are in your cell ? is it always 7 as per your example or could it be less and/or more ?

(ie 1,2,3,4,5,7,8,9,10,11,12,13,14)

Are they always seperated by commas ?

and lastly....(!).... do they always run from minimum to maximum, left to right ?
Chris Davison is offline   Reply With Quote
Old Apr 30th, 2002, 07:00 PM   #3
waderw
Board Regular
 
Join Date: Apr 2002
Posts: 85
Default

There could be 'n' numbers in the cell. Ranging from 0-nnn. They are all integers, separted by commas and can be in any order.

Thanks!
waderw is offline   Reply With Quote
Old Apr 30th, 2002, 07:11 PM   #4
Tom Schreiner
Board Regular
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

This sounds like a fun little project...
Will you email me the sheet?
TsTom@Hotmail.com
Thanks,
TOm
Tom Schreiner is offline   Reply With Quote
Old Apr 30th, 2002, 07:16 PM   #5
zacemmel
Board Regular
 
zacemmel's Avatar
 
Join Date: Apr 2002
Location: Redmond, WA
Posts: 636
Default

I was thinking it would be fairly easy to do that, but there are some variables that would make it difficult. The fact that integer length can change would make it tough. I think I can do this for you, though. Just give me a few minutes here and I should have that for you.

Zac
zacemmel is offline   Reply With Quote
Old Apr 30th, 2002, 09:24 PM   #6
zacemmel
Board Regular
 
zacemmel's Avatar
 
Join Date: Apr 2002
Location: Redmond, WA
Posts: 636
Default

This code should work, just add it to a module:

Sub number()
Dim sPos As Integer
Dim lPos As Integer
Dim cPos As Integer
Dim xTime As Integer
Dim curVar As Integer
Dim lasVar As Integer
Dim nVar As Integer
Dim nPos As Integer
cPos = 1
sPos = 1
lasVar = InStr(sPos, Range("a1").Value, ",")
Do While cPos > 0
xTime = xTime + 1
cPos = InStr(sPos, Range("a1").Value, ",")
nPos = InStr(cPos + 1, Range("a1").Value, ",")
If xTime > 1 Then
If cPos = 0 Then
curVar = Mid(Range("a1").Value, sPos, Len(Range("a1").Value))
Else
curVar = Mid(Range("a1").Value, sPos, cPos - lPos - 1)
If nPos > 0 Then
nVar = Mid(Range("a1").Value, cPos + 1, nPos - cPos - 1)
End If
End If
Else
curVar = Mid(Range("a1").Value, sPos, cPos - 1)
nVar = Mid(Range("a1").Value, cPos + 1, nPos - cPos - 1)
End If
If nVar - 1 = curVar Then
gotime = 1
Else
If gotime = 1 Then
If xTime > 1 Then
output = output & "," & lasVar & "-" & curVar
Else
output = lasVar & "-" & curVar
End If
Else
If xTime > 1 Then
output = output & "," & curVar
Else
output = curVar
End If
End If
lasVar = nVar
gotime = 0
End If
sPos = cPos + 1
lPos = cPos
Loop
MsgBox output
End Sub
zacemmel is offline   Reply With Quote
Old Apr 30th, 2002, 09:42 PM   #7
giacomo
Board Regular
 
giacomo's Avatar
 
Join Date: Feb 2002
Posts: 1,802
Default

I tested this on a couple different data sets and this worked for me...

Sub fcn()
Dim intArray(), index, char As Integer
Dim strIn, strOut As String

index = 1
comma = 1
char = 1
strIn = ActiveCell.Value & ","

Do
ReDim Preserve intArray(index)
comma = InStr(char, strIn, ",", 0)
intArray(index) = Int(Mid(strIn, char, (comma - char)))
char = comma + 1
index = index + 1
Loop While InStr(char, strIn, ",", 0) > 0

strOut = "" & intArray(1)

For index = 2 To UBound(intArray)
If intArray(index) <> intArray(index - 1) + 1 Then
strOut = strOut & "," & intArray(index)
Else
If index <> UBound(intArray) Then
If intArray(index) <> intArray(index + 1) - 1 Then
strOut = strOut & "-" & intArray(index)
End If
Else
strOut = strOut & "-" & intArray(index)
End If
End If
Next index
MsgBox (strOut)
End Sub

[ This Message was edited by: giacomo on 2002-04-30 20:53 ]
giacomo is offline   Reply With Quote
Old Apr 30th, 2002, 10:05 PM   #8
Tom Schreiner
Board Regular
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

I tested giacomo's code and it works nicely.
Tom
Tom Schreiner is offline   Reply With Quote
Old Apr 30th, 2002, 10:14 PM   #9
waderw
Board Regular
 
Join Date: Apr 2002
Posts: 85
Default

I appreciate it. I'll let you know how it turns out.

Regards,
waderw is offline   Reply With Quote
Old May 2nd, 2002, 06:52 AM   #10
waderw
Board Regular
 
Join Date: Apr 2002
Posts: 85
Default

Well, I decided to let Excel do some of the work for me since I was under a little bit of a time constraint to have this completed. I used the "Text to Data" function then sorted the selection ascending. The rest of the code is to compare numbers to find ranges(if any).
The code is a little sloppy right now. I need to clean it up a little.

Function FindRanges()
Dim WorkCell, LastCell, SortedList As String
Dim ColCount, NumList As Integer
Dim Lo, temp, ConsecTemp As Integer
Dim isConsec As Boolean

WorkCell = ActiveCell.Offset(12, 15).Address
isConsec = False

Do Until ActiveCell = vbNullString
LastCell = ActiveCell.Address
Application.DisplayAlerts = False
Selection.TextToColumns Destination:=Range(WorkCell), DataType:=xlDelimited, TextQualifier:=xlNone, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1))
Application.DisplayAlerts = True
ActiveCell.Value = vbNullString
Range(WorkCell).Select
Range(ActiveCell, Selection.End(xlToRight).Address).Select 'sort
Selection.Sort Key1:=Range(WorkCell), Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlLeftToRight ', DataOption1:=xlSortNormal
NumList = Selection.Columns.count
Range(WorkCell).Select
SortedList = ActiveCell.Value

For ColCount = 1 To NumList
Lo = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
temp = ActiveCell.Value
If isConsec And temp = vbNullString Then
SortedList = SortedList & "-" & ConsecTemp
isConsec = False
Range(WorkCell, Selection.End(xlToRight).Address).Select
Selection.Clear
Exit For
ElseIf temp = vbNullString Then
Range(WorkCell, Selection.End(xlToRight).Address).Select
Selection.Clear
Exit For
ElseIf (Lo + 1) = temp Then
ConsecTemp = temp
isConsec = True
ElseIf (Lo + 1) <> temp And isConsec Then
SortedList = SortedList & "-" & Lo & ", " & temp
isConsec = False
Else
SortedList = SortedList & "," & temp
End If
Next

Range(LastCell).Select
ActiveCell = SortedList
Lo = vbNullString
temp = vbNullString
ConsecTemp = 0
SortedList = vbNullString
ActiveCell.Offset(1, 0).Select
Loop
End Function

let me know if you have any suggestions or thoughts.

Regards,
Robert
waderw is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 02:22 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes