+ Reply to Thread
Results 1 to 24 of 24

VBA Noob, "Compile Error: Invalid Use of Property" On First line of Code (great..)

  1. #1
    Registered User
    Join Date
    05-31-2013
    Location
    Seattle
    MS-Off Ver
    Excel 2013
    Posts
    48

    VBA Noob, "Compile Error: Invalid Use of Property" On First line of Code (great..)

    Hi all,

    I'm trying to create a code for cell G20 (see attached workbook), which represents a value for "intercept". This "intercept" value changes, depending on multiple conditions, which is why I need to write a complex nesting IF function with multiple AND conditions to get the correct value. Currently, I have only six nesting IFs, but eventually I will need 963. I am just testing this out to see if I can get six going and then I will worry about the remaining 957 lol.

    So here is my code to accomplish this:

    Sub IfThenNesting()

    If C3 = "CCI" And C5 = "Case-Shiller National Home Price Index" And C9 = 5 Then Range("I4").Value
    End If
    ElseIf C3 = "CCI" And C5 = "National Unemployment Rate" And C9 = 0 Then Range("I5").Value
    End If
    ElseIf C3 = "CCI" And C5 = "S&P 500 Stock Prices" And C9 = 0 Then Range("I6").Value
    End If
    ElseIf C3 = "CCI" And C5 = "# of House Units Starts" And C9 = 3 Then Range("I7").Value
    End If
    ElseIf C3 = "National Unemployment Rate" And C5 = "S&P 500 Stock Prices" And C9 = 0 Then Range("I8").Value
    End If
    ElseIf C3 = "S&P 500 Stock Prices" And C5 = "Nordstrom Stock Price" And C9 = 0 Then Range("I9").Value
    End If
    End Sub
    and my workbook is attached here:
    VBA IF test.xlsx

    I want to do a nesting IF statement with multiple conditions. It all makes sense if you look at the worksheet. I want the calculation of the IF statement to be entered into cell G20.

    Any help is GREATLY appreciated on getting me started. THANK YOU.
    Last edited by tvwright; 10-08-2015 at 12:49 PM.

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,947

    Re: VBA Noob, "Compile Error: Invalid Use of Property" On First line of Code (great..)

    Get rid of every "End If" and "ElseIf" - you really are not using nesting at all.

    But your code has other problems: if your C3, C5, and C9 are cells , then use

    If Range("C3").Value = "CCI" And Range("C5").Value = "Case-Shiller National Home Price Index" And Range("C9").Value = 5 Then Range("G19").Value = Range("I4").Value

    But if you expect 900+ nested ifs then you really should be doing something different.
    Last edited by Bernie Deitrick; 10-08-2015 at 12:41 PM.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    05-31-2013
    Location
    Seattle
    MS-Off Ver
    Excel 2013
    Posts
    48

    Re: VBA Noob, "Compile Error: Invalid Use of Property" On First line of Code (great..)

    Quote Originally Posted by Bernie Deitrick View Post
    Get rid of every "End If" and "ElseIf"

    But your code has other problems: if your C3, C5, and C9 are cells , then use

    If Range("C3").Value = "CCI" And Range("C5").Value = "Case-Shiller National Home Price Index" And Range("C9").Value = 5 Then Range("I4").Value = "Something"
    OK Great, did that. Still get the same "Compile Error: Invalid Use of Property" on the very first line of code.

    The error is highlighting this line of code:

    Sub IfThenNesting()
    FirstLine.png

    Also, how do I get it so that it enters this IF formula into cell G20? Thank you!

  4. #4
    Registered User
    Join Date
    05-31-2013
    Location
    Seattle
    MS-Off Ver
    Excel 2013
    Posts
    48

    Re: VBA Noob, "Compile Error: Invalid Use of Property" On First line of Code (great..)

    Quote Originally Posted by Bernie Deitrick View Post
    Get rid of every "End If" and "ElseIf" - you really are not using nesting at all.

    But your code has other problems: if your C3, C5, and C9 are cells , then use

    If Range("C3").Value = "CCI" And Range("C5").Value = "Case-Shiller National Home Price Index" And Range("C9").Value = 5 Then Range("G19").Value = Range("I4").Value

    But if you expect 900+ nested ifs then you really should be doing something different.
    OK - I still get the same error with the first line even after making those 'Range("C9").Value' updates for all of my cell values. what are other options that I should be doing other than nesting IF statements? Keep in mind, I am not a VBA coder so I was trying to keep it simple even though it is very repetitive.

    My code now currently reads like this and still get the error on first line:

    Sub Test()
    If Range("C3").Value = "CCI" And Range("C5").Value = "Case-Shiller National Home Price Index" And Range("C9").Value = 5 Then Range("I4").Value

    If Range("C3").Value = "CCI" And Range("C5").Value = "National Unemployment Rate" And Range("C9").Value = 0 Then Range("I5").Value

    If Range("C3").Value = "CCI" And Range("C5").Value = "S&P 500 Stock Prices" And Range("C9").Value = 0 Then Range("I6").Value

    If Range("C3").Value = "CCI" And Range("C5").Value = "# of House Units Starts" And Range("C9").Value = 3 Then Range("I7").Value

    If Range("C3").Value = "National Unemployment Rate" And C5 = "S&P 500 Stock Prices" And Range("C9").Value = 0 Then Range("I8").Value

    If Range("C3").Value = "S&P 500 Stock Prices" And C5 = "Nordstrom Stock Price" And Range("C9").Value = 0 Then Range("I9").Value

    End Sub
    Last edited by tvwright; 10-08-2015 at 12:54 PM.

  5. #5
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,947

    Re: VBA Noob, "Compile Error: Invalid Use of Property" On First line of Code (great..)

    First, you are putting your code into the wrong place. Delete the code that you have, then double-click the Module1 object in your project, and paste this into the window that appears. You were getting the error on the first line because the sub was not compiling, because you were using

    .... Then Range("I4").Value

    without assigning a value to I4 or using I4's value in any way.

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    05-31-2013
    Location
    Seattle
    MS-Off Ver
    Excel 2013
    Posts
    48

    Re: VBA Noob, "Compile Error: Invalid Use of Property" On First line of Code (great..)

    Quote Originally Posted by Bernie Deitrick View Post
    First, you are putting your code into the wrong place. Delete the code that you have, then double-click the Module1 object in your project, and paste this into the window that appears. You were getting the error on the first line because the sub was not compiling, because you were using

    .... Then Range("I4").Value

    without assigning a value to I4 or using I4's value in any way.

    Please Login or Register  to view this content.
    Thank you. Copying and pasting the above code worked!!! WOW. I am very thankful for your help and it makes sense. Except I made a typo in my first post and said G19 when I meant G20 so I will change it. You're awesome! THANK YOU.

    Except now I have to press "run" anytime I want G20 to update. How do I have it automatically run anytime someone changes any of the criteria in cells C3, C5, and or C9?
    Last edited by tvwright; 10-08-2015 at 01:43 PM.

  7. #7
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,947

    Re: VBA Noob, "Compile Error: Invalid Use of Property" On First line of Code (great..)

    Great! Get yourself a good book - I always recommend John Walkenbach's "Excel XXXX Power Programming With VBA" series - available for older versions used (and cheaper), but the basics still apply, so it is a good start:

    http://www.amazon.com/gp/offer-listi...condition=used

  8. #8
    Registered User
    Join Date
    05-31-2013
    Location
    Seattle
    MS-Off Ver
    Excel 2013
    Posts
    48

    Re: VBA Noob, "Compile Error: Invalid Use of Property" On First line of Code (great..)

    Quote Originally Posted by Bernie Deitrick View Post
    Great! Get yourself a good book - I always recommend John Walkenbach's "Excel XXXX Power Programming With VBA" series - available for older versions used (and cheaper), but the basics still apply, so it is a good start:

    http://www.amazon.com/gp/offer-listi...condition=used
    Thank you. I will definitely get a book. I added an "edit" after I already hit submit... one more quick question: how do I get the code to automatically run anytime the conditions in cells C3, C5 and/or C9 are changed?

  9. #9
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,947

    Re: VBA Noob, "Compile Error: Invalid Use of Property" On First line of Code (great..)

    1) Copy this code.
    2) Right-Click the sheet tab of interest.
    3) Select "View Code"
    4) Paste the code into the window that appears.
    5) Save the file as a macro-enabled .xlsm file.
    6) Make changes as needed to the macro name

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    05-31-2013
    Location
    Seattle
    MS-Off Ver
    Excel 2013
    Posts
    48

    Re: VBA Noob, "Compile Error: Invalid Use of Property" On First line of Code (great..)

    Wooohoooo!!!! Thank you

  11. #11
    Registered User
    Join Date
    05-31-2013
    Location
    Seattle
    MS-Off Ver
    Excel 2013
    Posts
    48

    Re: VBA Noob, "Compile Error: Invalid Use of Property" On First line of Code (great..)

    Quote Originally Posted by Bernie Deitrick View Post
    1) Copy this code.
    2) Right-Click the sheet tab of interest.
    3) Select "View Code"
    4) Paste the code into the window that appears.
    5) Save the file as a macro-enabled .xlsm file.
    6) Make changes as needed to the macro name

    Please Login or Register  to view this content.
    Hi Bernie,

    I ordered that book from Amazon and it hasn't come yet. But I'm excited to be getting it next week. So I have another question regarding this project for you since you have been so great at helping! Attached is my updated workbook. Everything is working correctly and flawlessly, thanks to your help.

    Right now all of my data and calculations are on the same sheet, but I would like to move the data to sheet 2, and have my calcuations on sheet 1 so I don't have to be looking at all the data on the form. Here is the workbook for reference:

    La la.xlsm

    Here is my code for reference:

    In Module1
    Please Login or Register  to view this content.
    In Sheet 2
    Please Login or Register  to view this content.
    So all of the above code works perfectly. Except now I have all the data on sheet named "Calculation" in the same position except on sheet named "data". I want the VBA code to use the cells on worksheet named "Data" instead of the current cell ranges it is using on worksheet named "calculation.

    I tried changing all of the above to
    Please Login or Register  to view this content.
    but this did not work? Can you send me in the right direction to how to keep the code working properly by changing the data to another sheet?

    Thank you! <3
    Last edited by tvwright; 10-14-2015 at 02:03 PM.

  12. #12
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,947

    Re: VBA Noob, "Compile Error: Invalid Use of Property" On First line of Code (great..)

    You don't have a worksheet name "data" in the posted file.

    So try this. Change
    Please Login or Register  to view this content.
    to

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    05-31-2013
    Location
    Seattle
    MS-Off Ver
    Excel 2013
    Posts
    48

    Re: VBA Noob, "Compile Error: Invalid Use of Property" On First line of Code (great..)

    I Am so sorry. I updated the worksheet names and didn't save it before I sent you the file. You got the older version. Here is the new version. Will your code still work? I will go ahead and try it in a minute.

    La la.xlsm

  14. #14
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,947

    Re: VBA Noob, "Compile Error: Invalid Use of Property" On First line of Code (great..)

    You should just use a formula in those three cells:

    B28:

    =SUMPRODUCT((F4:F15=C3)*(G4:G15=C5)*I4:I15)

    B29:

    =SUMPRODUCT((F4:F15=C3)*(G4:G15=C5)*H4:H15)

    C19

    =INDEX(J:J,SUMPRODUCT((F4:F15=C3)*(G4:G15=C5)*ROW(H4:H15)))

    When you have a larger table just change the 15s to the a number higher than the number or rows in your table.


    In code, you would use:

    Please Login or Register  to view this content.
    Last edited by Bernie Deitrick; 10-14-2015 at 04:03 PM.

  15. #15
    Registered User
    Join Date
    05-31-2013
    Location
    Seattle
    MS-Off Ver
    Excel 2013
    Posts
    48

    Re: VBA Noob, "Compile Error: Invalid Use of Property" On First line of Code (great..)

    Quote Originally Posted by Bernie Deitrick View Post
    You don't have a worksheet name "data" in the posted file.

    So try this. Change
    Please Login or Register  to view this content.
    to

    Please Login or Register  to view this content.
    Hi Bernie,

    Thank you. It says I have a Run Time Error when I enter that code. I replaced all cell values to have the sheet reference preceding it. Here is my code now after following your update:

    Please Login or Register  to view this content.
    and I get "Run-Time error '424' Object Required" pop up. It highlights my sixth line of code, see picture below:

    delete.png

  16. #16
    Registered User
    Join Date
    05-31-2013
    Location
    Seattle
    MS-Off Ver
    Excel 2013
    Posts
    48

    Re: VBA Noob, "Compile Error: Invalid Use of Property" On First line of Code (great..)

    Also here is my workbook now. It has no data in the calculation tab.


    La la.xlsx

    Any idea on why I'm getting that error? Thank you!

  17. #17
    Registered User
    Join Date
    05-31-2013
    Location
    Seattle
    MS-Off Ver
    Excel 2013
    Posts
    48

    Re: VBA Noob, "Compile Error: Invalid Use of Property" On First line of Code (great..)

    OK I got it to work. THe issue was I did not have a T in the "shtd" or "shtc" in this part:

    Corrected version:
    Please Login or Register  to view this content.
    it used to be..
    Incorrect version:
    Please Login or Register  to view this content.

    I added the T, and it works when I manually "run" the code, but it is no longer automatically updating. The code you gave me to get it to automatically update is no longer applicable because I changed the sheet.How do I get it to run automatically again? I copied the code into the "data" sheet's code dialogue, but this does not work.

    EDIT: OK so weirdly enough, i closed out of excel and re-opened it and now it all works automatically without any problems. this is so confusing! Thank you again for all of your help.
    Last edited by tvwright; 10-14-2015 at 06:58 PM.

  18. #18
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,947

    Re: VBA Noob, "Compile Error: Invalid Use of Property" On First line of Code (great..)

    Great - glad to hear you got it to work, despite my poor typing skills

  19. #19
    Registered User
    Join Date
    05-31-2013
    Location
    Seattle
    MS-Off Ver
    Excel 2013
    Posts
    48

    Re: VBA Noob, "Compile Error: Invalid Use of Property" On First line of Code (great..)

    Quote Originally Posted by Bernie Deitrick View Post
    1) Copy this code.
    2) Right-Click the sheet tab of interest.
    3) Select "View Code"
    4) Paste the code into the window that appears.
    5) Save the file as a macro-enabled .xlsm file.
    6) Make changes as needed to the macro name

    Please Login or Register  to view this content.
    Hi Bernie,

    I have a few more macros that I would like to automatically run when those same cells are changed. The above code currently only works to run my first macro called "Test". I have 5 additional macros now that I would also like to run in the same way, named "Test1" "Test2" "Test3" "Test4" and "Test5" without the quotation marks. How do I edit your above code to get them to run in the same way as my first macro called "Test"?

    I attempted to change the code to this, but it did not work:

    Please Login or Register  to view this content.
    How do you advise?

    Also I got the book you recommended and have already read Part 1 - it was just the introductory stuff. The next part is more advanced, and I'm very excited to read it tonight!

  20. #20
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,947

    Re: VBA Noob, "Compile Error: Invalid Use of Property" On First line of Code (great..)

    Please Login or Register  to view this content.

  21. #21
    Registered User
    Join Date
    05-31-2013
    Location
    Seattle
    MS-Off Ver
    Excel 2013
    Posts
    48

    Re: VBA Noob, "Compile Error: Invalid Use of Property" On First line of Code (great..)

    Quote Originally Posted by Bernie Deitrick View Post
    Please Login or Register  to view this content.
    Thanks, but when I do that it triggers a window with a drop down menu prompting me to select which single macro of the 11 I would like to run. See image below:


    MacroList.png

    I would like to select ALL macros and run them at the same time. I had to make 11 different macros, because I kept getting an error that the procedure was "too long" so I had to divide the one long procedure into multiple procedures. Do you have any suggestions?

    Thank you so much!

  22. #22
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,947

    Re: VBA Noob, "Compile Error: Invalid Use of Property" On First line of Code (great..)

    Post your workbook.

    Did you look at using the formula that I posted instead of macro code?

  23. #23
    Registered User
    Join Date
    05-31-2013
    Location
    Seattle
    MS-Off Ver
    Excel 2013
    Posts
    48

    Re: VBA Noob, "Compile Error: Invalid Use of Property" On First line of Code (great..)

    Quote Originally Posted by Bernie Deitrick View Post
    Post your workbook.

    Did you look at using the formula that I posted instead of macro code?
    Sorry for the late reply Bernie, usually I get an email notification when you respond but this time I did not for some reason. I did not look at the formula you posted, no. Do you think I should do that still - here is my workbook:

    La la (Autosaved).xlsm

    Let me know! Thank you.

    EDIT:

    It appears that your formula method is working!!!!!!!!!!!!!!! WOW. THANK YOU~
    Last edited by tvwright; 10-20-2015 at 04:33 PM.

  24. #24
    Registered User
    Join Date
    05-31-2013
    Location
    Seattle
    MS-Off Ver
    Excel 2013
    Posts
    48

    Re: VBA Noob, "Compile Error: Invalid Use of Property" On First line of Code (great..)

    If you're interested, here is the final worksheet (for now... )

    La la Final.xlsm
    Last edited by tvwright; 10-20-2015 at 05:22 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. "Compile Error: Invalid Qualifier"
    By pooky in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-23-2014, 09:43 AM
  2. [SOLVED] ComboBox - "Error: Invalid Property Used" while restricting the dropdown values
    By SunOffice in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-05-2013, 04:26 AM
  3. [SOLVED] Compile Error: Invalid Use of Property on stupidly simple code
    By QueeniePie in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-18-2013, 11:41 AM
  4. Run Time Error-380 "Could not set the value property. Invalid property value"
    By Cijo in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-29-2012, 04:04 AM
  5. Replies: 1
    Last Post: 06-07-2012, 11:38 AM
  6. Replies: 3
    Last Post: 04-28-2010, 05:54 PM
  7. what does "Compile Error : Invalid Qualifier" mean?
    By funkymonkUK in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-05-2006, 04:48 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