+ Reply to Thread
Results 1 to 13 of 13

Problem: How to define my variable?

  1. #1
    Registered User
    Join Date
    03-06-2015
    Location
    Milan
    MS-Off Ver
    2013
    Posts
    6

    Problem: How to define my variable?

    Good morning everybody,

    This is my code:

    Dim Avrg As ????
    Avrg = WorksheetFunction.Average(LS)

    Dim FirstQ As ????
    FirstQ = WorksheetFunction.CountIf(LS, "< " & Avrg)

    Where LS is a Range of numbers with, usually, a lot of decimals. Using Variant to define Avrg (thats is simply the mean of LS) i obtain what im looking for (ex: 0,78271728392). BUT the problem is that when i use Avrg in the countif function i always obtain 0 (zero). The question is: Which type of variables I have to use in order to count LS without loosing decimals in the variable Avrg? (ex: Integer works but rounding 0,78271728392 in 1 is not what i want ).

    Thank you

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

    Re: Problem: How to define my variable?

    The code you posted works for me with both Avrg and FirstQ declared as Variant and LS being a range.

    For example.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    03-06-2015
    Location
    Milan
    MS-Off Ver
    2013
    Posts
    6

    Re: Problem: How to define my variable?

    Thank you Norie for your availability.

    Unfotunately, what works for you it doesnt for me. The only difference with your code is that in my code LS range is defined with Cells(,).
    I tried both with Variant and Double but i think the problem is that numbers in LS range have decimals and the countif function seems not to be able to "read" their average. For example if a put Integer to define the average, the function works!

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

    Re: Problem: How to define my variable?

    Can you show how you defined LS?

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

    Re: Problem: How to define my variable?

    Here's the code I posted using Cells and Range.

    The range used in the code is A1:A15 and it's populated using the RAND function with decimal values under 1.
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    03-06-2015
    Location
    Milan
    MS-Off Ver
    2013
    Posts
    6

    Re: Problem: How to define my variable?

    Yes sure

    Please Login or Register  to view this content.
    x,y,z are other variables (Long), because all the code is inside a For loop, so they change every next.

  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: Problem: How to define my variable?

    Looks fine to me, and there's no reason using Cells instead of Range should make a difference, you should still end up with a range object.

    Also, the numbers having decimals shouldn't make a difference either.

    If you were to put the same formulas you have in the code into worksheet cells what results do you get?

  8. #8
    Registered User
    Join Date
    03-06-2015
    Location
    Milan
    MS-Off Ver
    2013
    Posts
    6

    Re: Problem: How to define my variable?

    Thats really strange..

    I tried exactly your code, and countif works! but it gave me a count of 15, thats the number of the entire range! How is it possible that a countif function above the mean of a range has the entire range self count as result?

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

    Re: Problem: How to define my variable?

    Can you upload an example workbook?

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

  10. #10
    Registered User
    Join Date
    03-06-2015
    Location
    Milan
    MS-Off Ver
    2013
    Posts
    6

    Re: Problem: How to define my variable?

    Here it is
    Attached Files Attached Files

  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: Problem: How to define my variable?

    The code in that workbook works fine for me, mind you it is the code I posted as far as I can see.

  12. #12
    Registered User
    Join Date
    03-06-2015
    Location
    Milan
    MS-Off Ver
    2013
    Posts
    6

    Re: Problem: How to define my variable?

    I can not believe it!! I found the problem! The only difference between me and you, is that may windows system was set up with "." as thousands separator and "," as decimals separator. I just changed this configuration from the control panel and now it works!!

    OMG finally..

    Thank you Norie

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

    Re: Problem: How to define my variable?

    I was actually wondering if that might have been the problem but dismissed it as I thought things would automatically adjust.

    Perhaps because VBA is kind of US-centric you need to use the US decimal/thousand separators.

+ 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] how to define a variable?
    By inf3 in forum Excel General
    Replies: 5
    Last Post: 05-09-2014, 05:49 PM
  2. using a variable workbook's name to define another variable
    By lordterrin in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-30-2013, 05:57 PM
  3. VBA Define Variable
    By Jiptastic in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-18-2012, 12:55 PM
  4. Using a variable to define a range in VBA
    By MysticGenius in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-01-2011, 06:20 PM
  5. How to define variable
    By kishore in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-13-2005, 06:06 AM

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