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 9th, 2002, 12:48 PM   #1
Cosmos75
Board Regular
 
Join Date: Feb 2002
Location: Tulsa, OK
Posts: 354
Default

I need a macro to do this

Say I have the number 79298. I need to number each number to the left of the last number from 1 to 4 (or more if there are more numbers), from left to right.

79298
4321

Then multiply the number by the number assigned to it and add them up.

(9*1) + (2*2) + (9*3) + (7*4) = 9 + 4 +27 +28 = 68

After that I need to check that the last number in the sum (i.e. 68) is equal to the last number in the original number (79298) which in this case it is.

p.s. Also, any idea for a shorter title??
Cosmos75 is offline   Reply With Quote
Old Apr 9th, 2002, 01:05 PM   #2
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

This should do it:


Public Sub main()

Dim iNumber As Long
Dim i As Integer
Dim iProduct As Integer

iNumber = 79298

For i = 1 To (Len(iNumber))
iProduct = iProduct + (i * Mid(iNumber, (Len(iNumber) + 1 - i), 1))
Next

If Right(iNumber, 1) = Right(iProduct, 1) Then
MsgBox "Everything's groovy and checks out OK.", , "Valid Number"
Else
MsgBox "Everything's not groovy.", , "Invalid Number"
End If
End Sub


Suggestion for title: "Arghhhh!!!!!!! I hate Excel!!!!!!!"

If you've got any problems, just repost.
__________________
Mark O'Brien

Columbus Ohio Celtic Supporters Club
Mark O'Brien is offline   Reply With Quote
Old Apr 9th, 2002, 01:05 PM   #3
Mark W.
MrExcel MVP
 
Join Date: Feb 2002
Location: Austin, Texas USA
Posts: 11,654
Default

Just for fun... here's the worksheet formula...

{=RIGHT(SUM(MID(LEFT(A1,LEN(A1)-1),ROW(INDIRECT("1:"&LEN(A1)-1)),1)*(LEN(A1)-ROW(INDIRECT("1:"&LEN(A1)-1)))))=RIGHT(A1)}

Note: This is an array formula which must be entered using the Control+Shift+Enter key combination. The outermost braces, { }, are not entered by you -- they're supplied by Excel in recognition of a properly entered array formula.
Mark W. is offline   Reply With Quote
Old Apr 9th, 2002, 01:07 PM   #4
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

That's fun?
What kind of fun do you have?

Some day I'll need to use something more than "SUM" in an excel spreadsheet.


EDIT:: Actually, I tested you formula and it is very good. However, I think brain twisters like that one push people towards UDF's in VBA.
_________________
[b] Mark O'Brien

[ This Message was edited by: Mark O'Brien on 2002-04-09 12:12 ]
Mark O'Brien is offline   Reply With Quote
Old Apr 9th, 2002, 02:13 PM   #5
Cosmos75
Board Regular
 
Join Date: Feb 2002
Location: Tulsa, OK
Posts: 354
Default

Mark W,

Cool! Groovy! THe messagebox was a nice touch. I might only use it to prompt me that a number doesn't check out as I need it to go through a list.

Mark O'Brien,

Thanks for the formula. And I must admit, that even though I am a novice at VBA, I do find it somewhat fun to get excel to bend to my will!!

THANKS!

Edit: Mark W. - What's UDF??

[ This Message was edited by: Cosmos75 on 2002-04-09 13:14 ]
Cosmos75 is offline   Reply With Quote
Old Apr 9th, 2002, 02:24 PM   #6
Mark O'Brien
MrExcel MVP
 
Mark O'Brien's Avatar
 
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
Default

Quote:

Edit: Mark W. - What's UDF??

[ This Message was edited by: Cosmos75 on 2002-04-09 13:14 ]
that was me, there's too many Mark's around here for my liking.

UDF is User Defined Function.

To replicate Mark W's array function using VBA you could do this:


Public Function IsValid(iNumber as Long) as Boolean

Dim i As Integer
Dim iProduct As Integer

For i = 1 To (Len(iNumber))
iProduct = iProduct + (i * Mid(iNumber, (Len(iNumber) + 1 - i), 1))
Next

If Right(iNumber, 1) = Right(iProduct, 1) Then
IsValid = True
Else
IsValid = False
End If
End Sub


Now, to use this. In cell A1 stick in your number 79298 or whatever and in cell B1 put in this formula:

=IsValid(A1)

This will return true or false.

Hope this helps.

__________________
Mark O'Brien

Columbus Ohio Celtic Supporters Club
Mark O'Brien 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 05:22 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