+ Reply to Thread
Results 1 to 18 of 18

Excel/VBA declaring variables

  1. #1
    Registered User
    Join Date
    08-27-2017
    Location
    Modesto, CA
    MS-Off Ver
    Microsoft Office Excel 2003
    Posts
    91

    Excel/VBA declaring variables

    here is my code:
    # Function Blah(InputArray)

    If TypeName(InputArray) Like "*()" Then
    If TypeName(InputArray) = "Boolean()" then
    Dim outputArray() As Boolean
    Elseif TypeName(InputArray) = "Date()" then
    Dim outputArray() As Date
    End If
    End if #

    I get an error message on the Dim outputArray() As Date line: "Duplicate declaration in current scope"

    How do I declare a variable as the same type as the input array--InputArray?

    Thanks.

    Edit / Delete Edit Post Quick reply to this message Reply Reply With Quote Reply With Quote Multi-Quote This Message

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Excel/VBA declaring variables

    You have declared outputArray twice. You can not use the same variable name twice despite the types are different.
    Dim outputArray() As Boolean
    Dim outputArray() As Date

  3. #3
    Registered User
    Join Date
    08-27-2017
    Location
    Modesto, CA
    MS-Off Ver
    Microsoft Office Excel 2003
    Posts
    91

    Re: Excel/VBA declaring variables

    I think you missed the point. The question I am asking is how do I declare a variable as the same Data Type as that of the input array--InputArray? Obviously it can't be done the way I tried; how can it be done?

  4. #4
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Excel/VBA declaring variables

    This is a duplicate of this post- https://www.excelforum.com/excel-pro...variables.html
    and you still haven't used code tags, I'm afraid.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  5. #5
    Registered User
    Join Date
    08-27-2017
    Location
    Modesto, CA
    MS-Off Ver
    Microsoft Office Excel 2003
    Posts
    91

    Re: Excel/VBA declaring variables

    I tried to. I put the # symbol before and after the code. I would very much like to but I'm afraid I just don't understand what's being asked of me. I think that despite my inadequacies my question to the forum is clear; can't someone address it? Or steer me to how to present the code in a satisfactory format?

  6. #6
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Excel/VBA declaring variables

    You aren't supposed to put # before and after the code. You either select the code and then press the # button on the toolbar above the reply box, or you manually add the code tags like this [code]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/code].

    If you review the moderator reply to your original thread, you'll see that we are not allowed to help until you comply. As soon as you do, we'll be delighted to assist you!

  7. #7
    Registered User
    Join Date
    08-27-2017
    Location
    Modesto, CA
    MS-Off Ver
    Microsoft Office Excel 2003
    Posts
    91

    Re: Excel/VBA declaring variables

    I didn't see a # icon so I did it manually as suggested. Here is my code:
    Please Login or Register  to view this content.
    I get an error message on the Dim outputArray() As Date line: "Duplicate declaration in current scope"

    How do I declare a variable as the same type as the input array--InputArray?

    Thanks.

  8. #8
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Excel/VBA declaring variables

    I think a more important question is why do you think you need to? Your only real option, that I can think of, is to declare an array variable for all possible input types and then assign that to another variant variable, but I can't see any benefit to it at all.

  9. #9
    Registered User
    Join Date
    08-27-2017
    Location
    Modesto, CA
    MS-Off Ver
    Microsoft Office Excel 2003
    Posts
    91

    Re: Excel/VBA declaring variables

    Thanks for replying. The function Blah is a function that will return the Transpose of a 1-D or 2-D array or a single-area range. Unless you're of the view that a function to return the Tranpose of an array is not useful--I'm not of that view--it seems to me that it would be desirable that the returned output, if it is an array that has been input rather than a range, be of the same Data Type as the array that was input to the function. In the code I provided there is no way that more than one declaration could ever be operative, since they are mutually exclusive, so I don't feel there's any real duplication. But that's Microsoft's call. Thanks for your suggestion of a different variable for each Data Type, e.g., outBoolean(), outDate(), outInteger(), outLong() etc. for all the built-in types. Cumbersome, but doable. Thanks again for replying.

    Alan Beban, former Microsoft Excel MVP.

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,534

    Re: Excel/VBA declaring variables

    I don't understand what you are trying to do though,,, one way
    Please Login or Register  to view this content.

  11. #11
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Excel/VBA declaring variables

    Quote Originally Posted by Alan Beban View Post
    Unless you're of the view that a function to return the Tranpose of an array is not useful
    I'm not sure how you could infer that from what I said, but that was not my meaning.

  12. #12
    Registered User
    Join Date
    08-27-2017
    Location
    Modesto, CA
    MS-Off Ver
    Microsoft Office Excel 2003
    Posts
    91

    Re: Excel/VBA declaring variables

    Replying to xinitwit, I inferred it from your question "I think a more important question is why do you think you need to? " followed by a useful suggestion how to do it, followed by the comment "but I can't see any benefit to it at all." As I said in a previous reply, the benefit I see is to return the Transpose of an array with same Data Type as the array that was transposed. The only way I couldn't see that as a benefit is if the function itself is not useful.

    Replying to jindon, thanks. I believe your code works. My previous post, #9 in this thread, will give you a better understanding of what I'm trying to do. Thanks again. How do I indicate "Problem solved"?

  13. #13
    Registered User
    Join Date
    08-27-2017
    Location
    Modesto, CA
    MS-Off Ver
    Microsoft Office Excel 2003
    Posts
    91

    Re: Excel/VBA declaring variables

    Further reply to Jindon. Interestingly, your code doesn't seem to work without including the dimensions.

    Please Login or Register  to view this content.
    works, for example, though
    Please Login or Register  to view this content.
    throws a Syntax error.
    I have to go back and see whether coding those dimensions at that point is more or less preferable than using a separate variable for each Data Type. Thanks again.

  14. #14
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Excel/VBA declaring variables

    Quote Originally Posted by Alan Beban View Post
    Replying to xinitwit, I inferred it from your question "I think a more important question is why do you think you need to? " followed by a useful suggestion how to do it, followed by the comment "but I can't see any benefit to it at all." As I said in a previous reply, the benefit I see is to return the Transpose of an array with same Data Type as the array that was transposed. The only way I couldn't see that as a benefit is if the function itself is not useful.
    My point was that I don't see the benefit to being able to declare an array of the relevant type. The input to your function is a Variant, which may or may not contain an array. If you simply declare a Variant in your function and assign the input argument to it, you will have a Variant that contains the same data type as the input, to which you can do whatever you need- resize, transpose, whatever. There is no need to attempt multiple declarations. Here is a very simplistic demonstration
    Please Login or Register  to view this content.
    Last edited by xlnitwit; 11-30-2017 at 07:40 AM.

  15. #15
    Registered User
    Join Date
    08-27-2017
    Location
    Modesto, CA
    MS-Off Ver
    Microsoft Office Excel 2003
    Posts
    91

    Re: Excel/VBA declaring variables

    Thanks a heap! That's the answer to my original question--how do I declare a variable with the same Data Type as the input array.
    By the way, the input to the function will not necessarily be a Variant, as you stated. It will be whatever the user inputs, and if it's not a range or 1-D or 2-D array an error message will be returned. In your example, for example, InputArray was a Double.

    Also, by the way, if in your Sub test() v were a Range, the MsgBox would display Variant().
    Please Login or Register  to view this content.
    instead of
    Please Login or Register  to view this content.
    obviates the problem.

    Thanks again.

  16. #16
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Excel/VBA declaring variables

    Quote Originally Posted by Alan Beban View Post
    By the way, the input to the function will not necessarily be a Variant, as you stated.
    As written the input to the function always comes in as a Variant- it doesn't matter what type of data you passed to it. Indeed, that is why you can pass any kind of data to it. Your InputArray argument has no declared type and is therefore a Variant, and your function has no declared return type so will always return a Variant.

    The code I posted was merely to demonstrate a point- it was by no means intended as a robust solution!

  17. #17
    Registered User
    Join Date
    08-27-2017
    Location
    Modesto, CA
    MS-Off Ver
    Microsoft Office Excel 2003
    Posts
    91

    Re: Excel/VBA declaring variables

    I don't understand.

    Please Login or Register  to view this content.
    n

    The MsgBox displays Double. What am I misunderstanding?

  18. #18
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Excel/VBA declaring variables

    Typename returns the data type that is contained inside the Variant. Look at the variables in the Locals window while stepping through the code.

+ 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. Declaring variables and arrays in Excel VBA.
    By oddbjorn-j in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-22-2016, 12:21 PM
  2. [SOLVED] Declaring Variables
    By forrestgump1980 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-16-2014, 01:45 PM
  3. Declaring variables in excel vba
    By clueless2 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 04-03-2013, 04:52 PM
  4. Declaring Variables - Why?
    By The Drone in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-02-2012, 09:01 AM
  5. Declaring Variables
    By Weston33 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-06-2012, 05:39 AM
  6. Declaring of variables
    By pspyve in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-08-2011, 08:44 PM
  7. Declaring variables
    By freekrill in forum Excel General
    Replies: 0
    Last Post: 07-18-2006, 01:15 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