+ Reply to Thread
Results 1 to 6 of 6

Defining scope of functions.

Hybrid View

  1. #1
    Registered User
    Join Date
    06-19-2015
    Location
    London
    MS-Off Ver
    2007
    Posts
    3

    Defining scope of functions.

    Hey guys,

    New job.. Lots of excels to work on and I want to use macros to simplify my life.. I'm a Php & Java programmer and VBA gets me frustrated for being too basic.

    I want to use a double conditional. A Select Case within an if statement. however it seems VBA doesn't see the scope of what I am trying to do and simply ends the if before starting the Select Case.

    here is a general overview

    If Cells(x, 1).Value = "" Then total = total + Cells(x, 9).Value Else _
    
            Select Case step
    
                Case 1
                    Cells(9, 13).Value = total
    
                Case 2
                    Cells(12, 13).Value = total
            
                Case 3
                    Cells(15, 13).Value = total
    
                Case 4
                    Cells(18, 13).Value = total
        
                Case Else
                    Cells(21, 13).Value = total
    
            End Select
            total = 0
            step = step + 1
            End If
    In PHP or JAVA the {} would compensate for this issue but since VB doesn't allow them... I have tried to look online and other than making a 20 condition long if, I have found no solutions to what should be a simple poblem.

    I'm sure this is simple so if there is an other thread, feel free to throw me straight to it.

    Thanks everyone

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Defining scope of functions.

    so when the IF is true, you want total to equal total + ..., and also execute the select portion? If that is the logic then how about:

    If Cells(x, 1).Value = "" Then
    
    Total = Total + Cells(x, 9).Value
    
        Select Case step
    
        Case 1
            Cells(9, 13).Value = Total
    
        Case 2
            Cells(12, 13).Value = Total
            
        Case 3
            Cells(15, 13).Value = Total
    
        Case 4
            Cells(18, 13).Value = Total
        
        Case Else
            Cells(21, 13).Value = Total
    
        End Select
        Total = 0
        step = step + 1
    End If
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  3. #3
    Registered User
    Join Date
    06-19-2015
    Location
    London
    MS-Off Ver
    2007
    Posts
    3

    Re: Defining scope of functions.

    if is true -> total = total +
    if is false -> select case & reinitialization of total & increment step

    You missed the "else"

    (this is part of a small loop)

    EDIT:

    here is how I would do this in PHP (hopefully will make things clearer)

    PHP Code: 
    If($cell1 ""){
        
    $total $total $cell2;
    }
    else{
        switch(
    $step){
            case 
    1:
            
    cell3 total;
            break;

            case 
    2:
            
    cell4 total;
            break;

            
    //etc
        
    }
        
    total 0;
        
    step++;

    Last edited by FrenchMassacre; 06-19-2015 at 10:21 AM.

  4. #4
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Defining scope of functions.

    If Cells(x, 1).Value = "" Then
    
    Total = Total + Cells(x, 9).Value
    
    Else
    
        Select Case step
    
        Case 1
            Cells(9, 13).Value = Total
    
        Case 2
            Cells(12, 13).Value = Total
            
        Case 3
            Cells(15, 13).Value = Total
    
        Case 4
            Cells(18, 13).Value = Total
        
        Case Else
            Cells(21, 13).Value = Total
    
        End Select
        Total = 0
        step = step + 1
    End If
    I stepped through this, and it follows the logic. I think the problem with yours is that if what follows "then" is on the same line, it does not see it as a multiple criteria but only does the first IF. In fact I would have thought you'd get an error like End If without If block, or something like that
    Last edited by Arkadi; 06-19-2015 at 10:26 AM.

  5. #5
    Registered User
    Join Date
    06-19-2015
    Location
    London
    MS-Off Ver
    2007
    Posts
    3

    Re: Defining scope of functions.

    Yeah exactly, spent 5 minutes looking for the difference and realized that there was none other than the new line.. Thanks!

  6. #6
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Defining scope of functions.

    You're welcome! Please remember to mark the thread as solved?

+ 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. Quickly defining functions and Buttons
    By RexMan85 in forum Excel General
    Replies: 5
    Last Post: 03-08-2014, 09:18 AM
  2. Replies: 1
    Last Post: 05-18-2012, 05:34 AM
  3. variable scope within functions
    By twd000 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-25-2009, 07:01 PM
  4. Simple math functions with variable "scope"
    By melisen in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-09-2009, 09:30 PM
  5. [SOLVED] Defining Categories for Custom Functions
    By Jim in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-22-2005, 04:06 AM

Tags for this Thread

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