+ Reply to Thread
Results 1 to 16 of 16

Select case with public variable???

  1. #1
    Registered User
    Join Date
    10-27-2011
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    7

    Select case with public variable???

    I am trying to write a code that will look at the values in several worksheets and if the cell it looks at has the same value as the job number input by the user, I want to copy and paste the row into a worksheet called "Tracking Macro". I had the program running but the screen flashes as it runs so I decided to try altering it and use the screen updating = false line. However that meant I had to take out all the selection and active lines I used. Now the sub lookup will not run. Here is the start of the code...

    Please Login or Register  to view this content.
    That all runs ok, then I call the "lookup" sub and I get error message 438: "Object does not support this property or method." After some process of ellimination I believe it is that the computer is not recignizing "StLook" as a range. Help ! I am fairly new to VBA programming. Here is the "lookup" sub...

    Please Login or Register  to view this content.
    Last edited by Kelso89; 10-27-2011 at 11:40 AM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,422

    Re: Select case with public variable???

    You use "screen updating = false" if you want to take the lazy/slow approach and select cells and worksheets, etc. In that case, you don't *need* to remove the selects, although you should.

    You could use that method while you work out the better way by not selecting stuff.

    Regards
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    10-27-2011
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Select case with public variable???

    I'm not sure I understand what you are saying? I am willing to try another approach if you have any suggestions. The method I used the first time I was selecting cells and using the activecell object but then the screenupdating was automatically set to true when I hit those lines of code and my screen started flashing. Are you saying I should be able to use select without this happening?

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,422

    Re: Select case with public variable???

    Are you saying I should be able to use select without this happening?
    I thought so. Unless I'm missing something, you should be able to sitch off screen updating, execute your code, and then switch it on again when you're finished to see the finished effect.

    I stand to be corrected but that's one of the major time savers.

    Note that it is usually called as:

    Please Login or Register  to view this content.

    If you had "Option Explicit" at the top of your code, you'd probably get Variable undefined for ScreenUpdating


    Regards
    Last edited by TMS; 10-27-2011 at 12:12 PM.

  5. #5
    Registered User
    Join Date
    10-27-2011
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Select case with public variable???

    Ya that is what my origional code that does run has... but anytime I have .Select or .Activate ScreenUpdating is automatically set to True.

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,422

    Re: Select case with public variable???

    No, you have "ScreenUpdating = False"

    That's not the same

    Regards

  7. #7
    Registered User
    Join Date
    10-27-2011
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Select case with public variable???

    I ran a test where I had...

    Please Login or Register  to view this content.
    As soon as the code with the .Select method ran the screen moved to the ThisYr & " Invoice Log" worksheetsheet.

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Select case with public variable???

    Do not use names for macros that have been reserve in VBA or Excel: 'format', 'lookup"
    You don't need public variables to pass argument to a macro/function.
    You don't need call to start a macro, just 'call it by it's name.
    Use the reference of a range:

    Please Login or Register  to view this content.



  9. #9
    Registered User
    Join Date
    10-27-2011
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Select case with public variable???

    This is really interesting. What are the c01, c02 ect called? Some kind of variable that does not need to be defined? Sorry I am still quite new to VBA.

    I have been trying to use these in the code but I think I need to understand a little more about them because I am getting all kinds of error messages now haha.

  10. #10
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Select case with public variable???

    If you are new to VBA the best way to start is reading a basic book on VBA.
    Like most other languages it has a certain structure & conventions. So start at the beginning.
    After grasping the fundamentals it's much easier to understand the more complex stuff.

  11. #11
    Registered User
    Join Date
    10-27-2011
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Select case with public variable???

    I have done that. I have read through 2 books and taken a course on it. I have also written programs for about 2 years now it is just that I am mostly selft taught other than the like one week class I took. I have also read about everything I can find about VB on google over the last 2 years. So I do have the basics. I have just never come across these c0# things.

  12. #12
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,422

    Re: Select case with public variable???

    They're just snb's way of using variables. He's not a believer in dimming variables, and he likes to keep his code "concise" ... hence the strap line: Avoid using VBA-code you do not understand.

    If you find it difficult to recognise a variable in VBA code after attending a week long course, you should get your money back


    Regards

  13. #13
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Select case with public variable???

    I use c00, c01, c02, etc. as variables.
    If it's not strictly necessary I do not declare variables (that's a personal style).
    At the same time I try to restrict the amount of variables to a minimum.
    Most of the variables I use are consequently (because of not declaring) variant variables.
    The errors you get are probably the result of 'option explicit'. If you remove that instruction, error messages will disappear.

    I'm surprised you were not instructed in your reading/course not to use 'reserved names' in VBA or Excel.
    Last edited by snb; 10-28-2011 at 09:52 AM.

  14. #14
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,422

    Re: Select case with public variable???

    Create a new workbook
    Add a module
    Copy and paste the following code
    Select the workbook
    Run the code

    What do you see? It should end up on sheet3 with "test" in cell A60 with no sheet swaps or cell selection visible.

    Please Login or Register  to view this content.

    Regards

  15. #15
    Registered User
    Join Date
    10-27-2011
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Select case with public variable???

    snb: Ya I have been told not to use 'reserved names' before... several times actually. It's just when I am naming the sub I don't know what all the 'reserved names' are so I just put whatever seems logical to me. Bad habit I guess.

    I ran that test code and the screen does update. As soon as select or activecell runs the screen flashes and goes to that. Those objects default the screenupdating application to true.

  16. #16
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,422

    Re: Select case with public variable???

    Must be your system. It doesn't do that on mine.

    Regards

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

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