+ Reply to Thread
Results 1 to 21 of 21

Convert VBA Array from Boolean to Integer

  1. #1
    Forum Contributor
    Join Date
    07-30-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    476

    Convert VBA Array from Boolean to Integer

    Hi,

    I was trying to figure out how to use sumproduct in VBA in the same manner as in excel in lieu of CountIfs and/or Creating Arrays given criteria

    Someone else noticed that VBA isn't converting Boolean Array into Integers automatically like Worksheet does. I looped through to keep it moving, but was wondering if anyone has ideas on converting

    ex. Worksheet: Sumproduct((Array <> variable)*1)

    So I'd hae to convert (Array <> variable) from Boolean to 1, 0)

  2. #2
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Convert VBA Array from Boolean to Integer

    Please Login or Register  to view this content.
    Shows -1 in the immediate window. So if you use CInt(Put Boolean array here) I would think it would convert.
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Convert VBA Array from Boolean to Integer

    This kind of works but I'm not sure how you would use it.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

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

    Re: Convert VBA Array from Boolean to Integer

    I would create UDF before think that way.
    Please Login or Register  to view this content.
    OOps, CountIfs...
    Please Login or Register  to view this content.
    Last edited by jindon; 11-05-2013 at 05:35 PM.

  5. #5
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Convert VBA Array from Boolean to Integer

    Please Login or Register  to view this content.
    Only slightly different from Norie's suggestion.

  6. #6
    Forum Contributor
    Join Date
    07-30-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    476

    Re: Convert VBA Array from Boolean to Integer

    Thanks guys,

    1. Tried code below, get Type Mismatch error still
    Please Login or Register  to view this content.
    2. Trying to see if I can plug Norie's version in as the overall goal is to see if it's possible to convert from Boolean to Integer without looping

    3. I may be wrong, but I'm trying to build things without touching Excel as much as possible, trying to avoid UDF, etc.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Convert VBA Array from Boolean to Integer

    Why are you trying to do everything in VBA and not take advantage of Excel's built-in functionality?

    For example, if you wanted to do a simple, straight down the line, no quirks sum, why not use Excel's SUM worksheet function.

    If you really need it in code then use WorksheetFunction or Evaluate.

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

    Re: Convert VBA Array from Boolean to Integer

    Quote Originally Posted by cmore View Post
    2. Trying to see if I can plug Norie's version in as the overall goal is to see if it's possible to convert from Boolean to Integer without looping
    You should be aware that Evaluate method only accepts the substring less than 256 characters.

  9. #9
    Forum Contributor
    Join Date
    07-30-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    476

    Re: Convert VBA Array from Boolean to Integer

    Cool, thanks, forgot about that limit. What's the use of 'Join' in that syntax though?

  10. #10
    Forum Contributor
    Join Date
    07-30-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    476

    Re: Convert VBA Array from Boolean to Integer

    Quote Originally Posted by Norie View Post
    Why are you trying to do everything in VBA and not take advantage of Excel's built-in functionality?

    For example, if you wanted to do a simple, straight down the line, no quirks sum, why not use Excel's SUM worksheet function.

    If you really need it in code then use WorksheetFunction or Evaluate.
    Agreed not doing everything in code, just mainly the things that are data hogs, like array formulas and trimming down named ranges to only the essentials, and then for formulas, will put formula in ranges and then .value them out.

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Convert VBA Array from Boolean to Integer

    'data hogs'?

  12. #12
    Forum Contributor
    Join Date
    07-30-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    476

    Re: Convert VBA Array from Boolean to Integer

    the formulas I was initially using made it really difficult continue to build the model effectively calcs ran too long or required more and more real estate. So I'm trying to manage using both the worksheet and VBA. For instance most of the tool right now is just data manipulation no real calcs

  13. #13
    Forum Contributor
    Join Date
    07-30-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    476

    Re: Convert VBA Array from Boolean to Integer

    Quote Originally Posted by Norie View Post
    This kind of works but I'm not sure how you would use it.
    Please Login or Register  to view this content.
    Would you mind explaining the arrInt formula a little? I don't follow the syntax in { }

  14. #14
    Forum Contributor
    Join Date
    07-30-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    476

    Re: Convert VBA Array from Boolean to Integer

    Quote Originally Posted by Norie View Post
    This kind of works but I'm not sure how you would use it.
    Please Login or Register  to view this content.
    Would you mind explaining the arrInt formula a little? I don't follow the syntax and why in the world does the 1-Based array not work?

    Please Login or Register  to view this content.

  15. #15
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Convert VBA Array from Boolean to Integer

    1. The string argument to the Evaluate function is evaluated by Excel's formula evaluator, not VBA, and

    2. Your arrBool array is 2D, and Join only accepts a 1D array. 1-based, 0-based, or anything else-based doesn't matter.
    Last edited by shg; 11-05-2013 at 07:39 PM.
    Entia non sunt multiplicanda sine necessitate

  16. #16
    Forum Contributor
    Join Date
    07-30-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    476

    Re: Convert VBA Array from Boolean to Integer

    Get the Join part, just cannot get everything else to speak to me, working........

    Thanks shg! didn't realize the 2D requirement. Stupid question, Loading range from sheet without looping automatically creates 2D? I thought I could get a nice 1D that way. Used Transpose to get a 1D, but a little baffled

    Holy beez wax, this is the most exacting piece of code I've worked with so far. I know I'm obviously missing some things:

    Please Login or Register  to view this content.
    Last edited by cmore; 11-05-2013 at 09:04 PM.

  17. #17
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Convert VBA Array from Boolean to Integer

    Please Login or Register  to view this content.
    I know you don't want to hear it, but you're wasting your time if the goal is code efficiency. Evaluate is slow.

  18. #18
    Forum Contributor
    Join Date
    07-30-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    476

    Re: Convert VBA Array from Boolean to Integer

    No trust me, I hear you, I'm still looping. Just wanted to see if I can get this to work and see if I could possibly get Sumproduct or other things that required boolean to int conversion.

    Just a note though, I came across something that said Worksheet.Evaluate and [] are relatively faster, not sure if you had any thoughts there?

  19. #19
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Convert VBA Array from Boolean to Integer

    The brackets are just a shortcut to the evaluate function.

    Worksheet.Evaluate controls the context (the worksheet for unqualified range references) in which the formula is evaluated; I doubt there's any difference between that and Application.Evaluate in execution time.

  20. #20
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Convert VBA Array from Boolean to Integer

    cmore

    What is it you are trying to get to work?

  21. #21
    Forum Contributor
    Join Date
    07-30-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    476

    Re: Convert VBA Array from Boolean to Integer

    Sorry took so long to respond.

    So things are workingish, I'm just trying to find easier ways to create some of the sub-arrays needed and easier way for counts. For instance. There are a few formulas on the Summary Tab I haven't automated yet, and then there's info. that populates on the Data tab, based on the Summary Info. that's honestly kind of sluggish right now and finally, I am going to need to populate some chart data.

    In Excel going from boolean to to integers makes things a lot more flexible, please let me know if I'm off base
    Attached Files Attached Files
    Last edited by cmore; 11-10-2013 at 01:48 PM.

+ 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. [SOLVED] Convert Date/Time array to a integer array
    By bradja in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-17-2013, 03:08 AM
  2. Convert a String Name to Declared Boolean
    By bdb1974 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-16-2012, 05:41 PM
  3. Integer defined array vs variant defined array ..error making assignment to integer
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-07-2011, 01:48 PM
  4. Convert Boolean to 0/1
    By slide in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-20-2008, 02:38 PM
  5. Replies: 2
    Last Post: 11-15-2006, 06:36 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