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 8th, 2002, 09:48 PM   #1
youbet7469
New Member
 
Join Date: Mar 2002
Posts: 12
Default

Dear group... Can anyone tell me how to program formulas into excel to count in bases other than 10. (i.e. base six would be 1,2,3,4,5,6,11,12,13,14,15,16,21)

Ideally I'd like to be able to work with all the different bases up through 22..

Any advice would be greatly appreciated.

Thanks

Greg

youbet7469 is offline   Reply With Quote
Old Apr 8th, 2002, 10:21 PM   #2
Ivan F Moala
MrExcel MVP
 
Ivan F Moala's Avatar
 
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
Default

Quote:
On 2002-04-08 20:48, youbet7469 wrote:
Dear group... Can anyone tell me how to program formulas into excel to count in bases other than 10. (i.e. base six would be 1,2,3,4,5,6,11,12,13,14,15,16,21)

Ideally I'd like to be able to work with all the different bases up through 22..

Any advice would be greatly appreciated.

Thanks

Greg

One way is to use this UDF
Press Alt F11
Click > Insert > Module
Paste this code in.


Function BaseConv(InputNum, BaseNum)
Dim Quotient, Remainder As Single
Dim Answer As String

Quotient = InputNum ' Set quotient to number to convert.
Remainder = InputNum ' Set remainder to number to convert.
Answer = ""

Do While Quotient <> 0 ' Loop while quotient is not zero.
' Store the remainder of the quotient divided by base number in a
' variable called remainder.
Remainder = Quotient Mod BaseNum
' Reset quotient variable to the integer value of the quotient
' divided by base number.
Quotient = Int(Quotient / BaseNum)
' Reset answer to contain remainder and the previous answer.
Answer = Remainder & Answer
' Convert answer variable to a number.
Loop
BaseConv = Val(Answer)
End Function



Just refernce it as a Std formula in your
workbook As

=BaseConv(256,19)

Just a word of caution...it uses loops which
can be a little slow depending on the base
and the number of formulas you have in your
worksheet....

_________________
Kind Regards,
Ivan F Moala
http://<font color="green"><a href="...r Construction

[ This Message was edited by: Ivan F Moala on 2002-04-08 21:24 ]
Ivan F Moala is offline   Reply With Quote
Old Apr 8th, 2002, 10:30 PM   #3
Ivan F Moala
MrExcel MVP
 
Ivan F Moala's Avatar
 
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
Default

Woops....sorry that CONVERTS from Base10
to your base......sorry....will have another
play...
__________________
Kind Regards,
Ivan F Moala From the City of Sails
Ivan F Moala is offline   Reply With Quote
Old Apr 8th, 2002, 11:04 PM   #4
youbet7469
New Member
 
Join Date: Mar 2002
Posts: 12
Default

Ivan, you lost me.... I don't have any experience programming in Visual Basic... Is there any way to do this just within excel? If not ...I'm game for learning the VB that needs to be learned, its just a matter of whats the quickest and easiest

Thank you for your assistance

Greg
youbet7469 is offline   Reply With Quote
Old Apr 9th, 2002, 02:35 PM   #5
Chris Davison
MrExcel MVP
 
Join Date: Feb 2002
Location: Millbank, London, UK
Posts: 1,790
Default

Evening all,

would this be the same as the =MOD function ?

(I dont know, hence the question)

Chris Davison is offline   Reply With Quote
Old Apr 9th, 2002, 03:19 PM   #6
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

It's not the same as the MOD function.

The only built in bases that excel have are the fairly standard ones.

DEC, OCT, BIN and HEX.

If you want to use the other bases, you'll almost certainly have to use VBA.

To convert from base 6 to decimal I guess you would have to do something like this in VBA:

Base 6 = 666
Dec = 6(6^2) + 6(6^1) + 6(6^0)

Which may actually be a reasonably easy thing to code generically in VBA. I may give this a shot tonight.
__________________
Mark O'Brien

Columbus Ohio Celtic Supporters Club
Mark O'Brien is offline   Reply With Quote
Old Apr 9th, 2002, 04:42 PM   #7
Damon Ostrander
MrExcel MVP
 
Damon Ostrander's Avatar
 
Join Date: Feb 2002
Location: Denver, Colorado USA
Posts: 4,014
Default

Hi YouBet7469,

Here is a UDF that converts an Excel number to any base from 2 up to 37. Enjoy.


Function ToBase(ByVal X As Double, Base As Integer) As String

'Converts a number X to any base from 2 up to 37. The result is
'a string value. For bases larger than 10, digits 10, 11, 12, etc.,
'are represented A, B, C, etc., as is traditional for representing
'hexadecimal numbers.

'Example: =ToBase(34,12) yields "2A", or 2 x 12 + 10

Dim Digit As Integer
Dim K As Integer
Dim i As Integer
If X > 0 Then
K = Int(Log(X) / Log(Base))
For i = K To 0 Step -1
Digit = X Base ^ i
If Digit < 10 Then
ToBase = ToBase & CStr(Digit)
Else
ToBase = ToBase & Chr(Digit + 55)
End If
X = X - Digit * Base ^ i
Next i
Else
ToBase = "0"
End If

End Function

__________________
Keep Excelling.

Damon

VBAexpert Excel Consulting
(My other life: http://damonostrander.com )
Damon Ostrander is offline   Reply With Quote
Old Apr 9th, 2002, 09:03 PM   #8
youbet7469
New Member
 
Join Date: Mar 2002
Posts: 12
Default

Gentlemen, thanks for your help.... any suggestions as to where to go to learn how to enter the above formulas in using Visual Basic? I'm a complete newbie when it comes to working with that.

I really appreciate your input and assistance.

Best Regards,
Greg
youbet7469 is offline   Reply With Quote
Old Apr 9th, 2002, 09:28 PM   #9
youbet7469
New Member
 
Join Date: Mar 2002
Posts: 12
Default

Hey Guys, Me again...

I figured out a little of what one needs to do in VB to get it into excel.. I've run into a couple of things hoping you can give me a little guidance.

1. When I first cut and pasted the formula that Damon gave for the bases in VB I got a message that there was an error in the formula. It highlighted the line that reads

Digit = X \ Base ^ i

What I did was delete one of the "" figuring that it was just a typo in the formula.. Is that correct.

2. After doing some quick programing I am wondering if there might be an error in the formula. I am not that familiar with counting in bases other than 10 so I might be wrong, please feel free to correct me if you know for sure, but for example it was my understanding that when counting in base 5 you would count as follows:

1,2,3,4,5,11,12,13,14,15,21,22,23,24,25,31...

The formula u gave me gives me the following for base 5 counting

1,2,3,4,5,10,11,12,13,14,15,20,21,22,23,24,25,30,31

Another example is counting in base 9

My understanding of base 9 counting is that it should count as follows:

1,2,3,4,5,6,7,8,9,11,12,13,14,15,16,17,18,19,21,22,23,24,25,26,27,28,29,31

The formula that u gave me gives the following for base 9 counting:

1,2,3,4,5,6,7,8,9,11,12,13,14,15,16,17,18,20,21,22,23,24,25,26,27,28,30

Is my understanding of counting in different bases wrong or is it possible that there is a slight "glitch" in the formula?

Thanks again for your assistance, it is greatly appreciated!

Best Regards,
Greg


youbet7469 is offline   Reply With Quote
Old Apr 9th, 2002, 10:48 PM   #10
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Hi Greg,

Base 5 numbers are from 0-4, so you can never have a 5 as a digit. Base 9 goes from 0-8 so you can never have a 9 in the result.

When testing converting from base 10 to base 2-9, Damon's formula returns the correct results for me, except for specific cases in base 3 and base 9.

In base 3, the exponents of 3 return incorrect results

3^1 = 3, should be 10
3^2 = 30 should be 100
3^3 = 300 should be 1000
.
.
.

9^1 = 9, should be 10
9^2 = 90, should be 100
9^3 = 900, should be 1000
.
.
.

The formula returns the correct result for *ALL* other values (didn't check 11-37).

I tested Damon's formula vs. a formula posted by Leo Heuser, which is limited to base 2-9 conversion.

--------------------
Function Convert(Number As Long, NumberSystem As Integer, NumberOfDigits As Integer) As String
Dim Result As String
While Number > 0
Result = Number Mod NumberSystem & Result
Number = Int(Number / NumberSystem)
Wend
If NumberOfDigits = 0 Then
Convert = Result
Else
Convert = Right(String(NumberOfDigits - Len(Result), "0") & Result, NumberOfDigits)
End If
End Function
--------------------------

Both of these are really nice. Kudos to both. I have to think how these calculate to determine what steps would be required to correct Damon's, as it is more convenient and far more powerful (much larger conversion range).

Possibly Damon or others can jump in, too.

Regards,
Jay
Jay Petrulis 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 04:06 AM.


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