Select case .... End select

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
984
Office Version
  1. 2010
Platform
  1. Windows
I am trying to code the body metabolic rate by this code but I don't know where I make the mistake, please, somebody give me a hand here.
GENDERFBMRBASAL METABOLIC RATE
WEIGHT115kilograms
HEIGHT175cm
AGE59year

VBA Code:
Option Explicit
Sub bodymetabolic()
Dim gender As String
Dim weight, height, age As Integer
Dim bmr As Long

    gender Range("B1")
    weight Range("B3").Value
    height Range("B5").Value
    age Range("B7").Value
   
    Select Case UCase(genger)
           Case "F": bmr = 447.593 + (9.247 * Range("B3").Value) + (3.098 * Range("B5").Value) - (5.677 * Range("B7").Value)
           Case "M": bmr = 88.362 + (13.397 * Range("B3").Value) + (4.799 * Range("B5").Value) - (5.677 * Range("B7").Value)
          
    End Select
   
    Range("F1") = bmr
   
          
   
   
End Sub
 
Last edited by a moderator:

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
The line:
Select Case UCase(genger)

Word: genger

I suppose that should spell: gender
the same as the variable= Dim gender As String
 
Upvote 0
montecarlo2012,

Sample raw data:

Book1
ABCDEFG
1GENDERFBMRBASAL METABOLIC RATE
2
3WEIGHT115kilograms
4
5HEIGHT175cm
6
7AGE59year
Sheet1


After the macro:

Book1
ABCDEFG
1GENDERFBMR1718.205BASAL METABOLIC RATE
2
3WEIGHT115kilograms
4
5HEIGHT175cm
6
7AGE59year
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

VBA Code:
Sub bodymetabolic_V2()
' hiker95, 10/30/2014, ME815166
Dim gender As String
Dim weight As Long, height As Long, age As Long, bmr As Double
gender = Range("B1").Value
weight = Range("B3").Value
height = Range("B5").Value
age = Range("B7").Value
Select Case UCase(gender)
  Case "F": bmr = 447.593 + (9.247 * Range("B3").Value) + (3.098 * Range("B5").Value) - (5.677 * Range("B7").Value)
  Case "M": bmr = 88.362 + (13.397 * Range("B3").Value) + (4.799 * Range("B5").Value) - (5.677 * Range("B7").Value)
End Select
With Range("F1")
  .Value = bmr
  .NumberFormat = "0.000"
  .Font.Bold = True
  .HorizontalAlignment = xlCenter
End With
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the bodymetabolic_V2 macro.
 
Last edited by a moderator:
Upvote 0
Solution
montecarlo2012,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0
Code:
Dim weight, height, age As Integer
Two further remarks about the above line of code as advice for the future.

1. You have only declared "age" as integer. "weight" and "height" will be Variant type because you haven't stated a type for them.

2. vba converts Integer data type to Long data type to work with it so you might as well declare it as Long to start with (& it's shorter to type :))

hiker has addressed both of those issues with this line:
Code:
Dim weight As Long, height As Long, age As Long
 
Upvote 0

Forum statistics

Threads
1,221,497
Messages
6,160,151
Members
451,625
Latest member
sukhman

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