+ Reply to Thread
Results 1 to 8 of 8

Help with For loop and value enter check

  1. #1
    Registered User
    Join Date
    03-17-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    8

    Help with For loop and value enter check

    Hi All,

    I'm new in VBA and learning a lot (reading VBA excel 2007 for dummies lol) but for work I need a very simple routine to create so please help on this.

    So I have a calculation to do (see cell Q4 on image attached) where the variables to enter are s1,g1, s2,g2 etc.... In some cases I will only have s1 and g1 and the rest (s2,g2 etc...) will be zeros. As you can guess I cannot put 0's because I will get errors, however I can not either put a empty cell.

    So this is why I want to create my first subroutine, where the variables will be from F5 to P5 and be able to drag down the formula to compute the same calculation for the lower rows.

    Could someone give a hand on how to start this? As I said, I'm reading a book but the examples they give are so basic and takes a lot of time.

    Thank you,
    Last edited by Maradona_10; 03-24-2014 at 11:47 AM.

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,934

    Re: Help with For loop and value enter check

    You should post the formula that you are using instead of a picture. For VBA, you would want to use a UDF (User-Defined-Function) where your code is along the lines of
    Please Login or Register  to view this content.
    and you would use it like

    =MyNewFunction(F5:P5)

    and then when you copy it, the references will update like any other Excel function.
    Last edited by Bernie Deitrick; 03-23-2014 at 08:56 AM.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    03-17-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    8

    Help with For loop and value enter check

    Hi,

    Thank you , but I need a loop that checks the values of si and gi and makes the sum of it as: Sum [Si2 / 4*gi*dh].
    I tried something but I still need help to make it work

    Please Login or Register  to view this content.
    Can someone please help me?

    Thx
    Last edited by Maradona_10; 03-22-2014 at 09:37 PM. Reason: Rules not read

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,934

    Re: Help with For loop and value enter check

    Maybe

    Please Login or Register  to view this content.
    Last edited by Bernie Deitrick; 03-23-2014 at 08:57 AM.

  5. #5
    Registered User
    Join Date
    03-17-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    8

    Help with For loop and value enter check

    Hi all,

    Sorry but the code above do not have the workbook. So here I'm attaching it.

    So the code i've worked on is (not working):

    Please Login or Register  to view this content.
    I'm new in this so i tried to code the easiest part first (the for loop) however, I want to include in this code a smart 'value check' that is if there are empty cells or 0's, then the code would skip the si and gi for the next si+1 and gi+1.
    What I mean is that lets say s2 and g2 are 0 and 0 but s3 and g3 are 50 and 50. Then I would like my code to skip the calculation of s2 and g2 (because mathematical we cannot divide by 0) but continue with s3 and g3 etc...

    Thank you in advance.
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,934

    Re: Help with For loop and value enter check

    Quote Originally Posted by Maradona_10 View Post
    .... include in this code a smart 'value check' that is if there are empty cells or 0's, then the code would skip
    Try this one:

    Bolt fixed.xlsm

    which uses this code

    Please Login or Register  to view this content.
    Last edited by Bernie Deitrick; 03-23-2014 at 09:14 AM.

  7. #7
    Registered User
    Join Date
    03-17-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Help with For loop and value enter check

    Thank you Bernie, it works awesome!!!

    Just one last thing to close this thread: How can I count the columns selected in the "rngData As Range" input and make it a variable(say VariableColumn)? So that in the For Loop I don't have to put 1 to 9 but rather 1 to VariableColumn.

    Thank you!!!

  8. #8
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,934

    Re: Help with For loop and value enter check

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. run subroutine
    By miso.dca in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-31-2013, 02:03 AM
  2. Using a value from one subroutine in another.
    By rmar2011 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-08-2011, 03:48 PM
  3. Subroutine for XIRR
    By Charlie100 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-11-2010, 10:11 PM
  4. Run a VBA subroutine?
    By barryleajo in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-23-2010, 05:04 AM
  5. [SOLVED] end subroutine
    By melric in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-30-2005, 08:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1