Excel VBA basics

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mstearns
    New Member
    • Jan 2007
    • 1

    Excel VBA basics

    I'm attempting to write a simple VBA procedure to do pressure unit conversions. Basically, I want to be able to convert from one of several units of measure to another one of several units of measure. The script needs to examine the value entered by the user as well as the "from" unit...and based on that unit selected from a dropdown list, go to a subroutine to compute the "to" value (like "from" n.nnn millibars "to" n.nnn inches of mercury).

    I need to know two very basic things:

    1. How do I get the values from my cell into the script from the worksheet, and how do I get the computed values out of the script back into the worksheet?
    2. How do I use an IF THEN ELSE statement to send the script to the right sub procedure to do the computation? (I'm getting an "undefined label" error message.)

    Anyone's help is much appreciated.
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Originally posted by mstearns
    I'm attempting to write a simple VBA procedure to do pressure unit conversions. Basically, I want to be able to convert from one of several units of measure to another one of several units of measure. The script needs to examine the value entered by the user as well as the "from" unit...and based on that unit selected from a dropdown list, go to a subroutine to compute the "to" value (like "from" n.nnn millibars "to" n.nnn inches of mercury).

    I need to know two very basic things:

    1. How do I get the values from my cell into the script from the worksheet, and how do I get the computed values out of the script back into the worksheet?
    2. How do I use an IF THEN ELSE statement to send the script to the right sub procedure to do the computation? (I'm getting an "undefined label" error message.)

    Anyone's help is much appreciated.
    Could you show us the code you have written so far?

    I'm not expert in the Excel/VBA area, but I believe you can reference a cell by simply using (for example) Range("A1").Val ue. You can set values the same way.

    IF/THEN/ELSE is pretty straightforward and simple to use - I think we need to see what you have, to be able to help with this.

    Comment

    • Caterism
      New Member
      • Jan 2008
      • 1

      #3
      Hi,

      I've been writing Excel VBA scripts for a couple of years now so hopefully I can help!

      A better way than calling the value directly from a cell is to put it into a variable, this will make your code writing easier to read and understand. You can also change the cell which contains the value to be converted without having to change your code multiple times.

      Also depending on the number of different conversions you want to do you may want to use a Select Case statement. They are a little tidier than IF...THEN...ELS E statements.

      As Killer42 said if you post your code I'll take a look.

      Comment

      • bobbystrain
        New Member
        • Sep 2007
        • 11

        #4
        Don't bother. Get Uconeer software. It has conversion factors for everything! And if you still want to code your own, It is a good format to follow.

        Bobby

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Thanks for the suggestions people, but given that it's over a year now, it might be a bit late. :)

          Caterism, I suspect using a variable rather than a cell reference would also improve execution speed slightly, but don't know for sure.
          Last edited by Killer42; Jan 8 '08, 02:06 AM.

          Comment

          Working...