Ask Experts Questions for FREE Help !
Ask
    colbtech's Avatar
    colbtech Posts: 748, Reputation: 66
    Senior Member
     
    #1

    Sep 14, 2009, 03:00 AM
    excel vba returning values from a sub
    How do I return values from a subroutine in Excel VBA? It appears that I have to "temporarily" store a value elsewhere on the spreadsheet and recover when I return to the main part of the program.

    Simply the program runs and asks the user to enter data on several forms, but this data has to be "saved" so it can be used later. Is this a "feature" and my "work-around" is the correct way to achieve this?

    e.g.

    SelectWhat.Show ' Display Form
    WhatToDo = Sheet2.Cells(5, 252) 'recover info
    .
    .
    .
    .
    .
    Private Sub CommandButton1_Click()

    End Sub

    Private Sub SelectOrder_Click()

    Sheet2.Cells(5, 252).Value = "Order" 'Save data for later
    Unload Me
    End Sub

    Private Sub SelectQuote_Click()

    Sheet2.Cells(5, 252).Value = "Quote"
    Unload Me
    End Sub

    Private Sub SelectInvoice_Click()

    Sheet2.Cells(5, 252).Value = "Invoice"
    Unload Me
    End Sub

    Private Sub SelectView_Click()

    Sheet2.Cells(5, 252).Value = "View"
    Unload Me
    End Sub

    Private Sub UserForm_Click()

    End Sub
    JBeaucaire's Avatar
    JBeaucaire Posts: 5,426, Reputation: 997
    Software Expert
     
    #2

    Sep 14, 2009, 11:11 AM

    I don't believe storing values collected from forms on your sheet is a "workaround". Forms are intended for this very sort of thing. If you don't store them somewhere, they go away, yes?

    The most common scenario is as form data is collected it is added permanently to a growing database of information. That information is later used to cull reports and charts, etc.

    When the data collected is temporary, using a garbage sheet to store the values and/or garbage cells is perfectly normal.
    colbtech's Avatar
    colbtech Posts: 748, Reputation: 66
    Senior Member
     
    #3

    Sep 15, 2009, 12:10 AM

    Thanks for the reply. I (foolishly) presumed that variables calculated in a subroutine would still be available after the return. Still all is good. Program works.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Creating a Chart in Excel by including values in X and why Axis [ 2 Answers ]

1) I want to create a chart using the data in my excel sheet. 2) Data in Row 1 should become the x-axis labels in a chart and data in Row 2 should be the y-axis labels and the only data range actually in the chart. 3) When I highlight data in Rows 1 and 2 then click on Chart Wizard both data ...

Excel 2007 to have excel 2003 look? [ 6 Answers ]

Hey I have Microsoft Excel 2007, from my course at college I have been given instructions to do a task. However these instructions are for excel 2003. How do I change my excel 07 to look like excel 03 so that it is easier. I know there is a way, help greatly appreicated thanks.

Multiples of values in excel [ 1 Answers ]

Hi all, I'm trying to find a way of getting multiples of a value so I can use it in an IF statement. Basically I need the multiples of 60, I want to be able to say IF value is a multiple of 60 then this will happen. Is there a way to do this, I've been looking at LCMs, GCDs without any luck.

Filter from 2 Columns (Unique values) in Excel [ 1 Answers ]

I need to filter from both these sets of data in excel Set 1 Set 2 ----------------------- 986 Buchanan Dodsworth 563 67 789 Buchanan 235 689 Dodsworth 789 143


View more questions Search