+ Reply to Thread
Results 1 to 25 of 25

Declaring Variables with Public

  1. #1
    Forum Contributor
    Join Date
    03-08-2018
    Location
    Denmark
    MS-Off Ver
    2016 for Windows
    Posts
    413

    Declaring Variables with Public

    I have just started on vba and at the age of 50, it is not easy :-(
    But it's so exciting and I love it...... when it works.
    I am not sure what i have done wrong in my kode, I have tried to make the code more simpel than first and now it dosent work.
    Please can somebody have a look at it for me, it vil be much apriciated



    Please Login or Register  to view this content.
    Last edited by nordicdust; 04-11-2018 at 01:18 PM. Reason: Poor title

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,830

    Re: Varible problem or Noobie either way please help

    Welcome to the forum! Please take a moment to re-read forum rule #1 and then amend your thread title to something that better explains your problem. Changing your thread title is not optional, which means you must change it. Thanks!

    • Use concise, accurate thread titles.
    • Your post title should describe your problem, not your anticipated solution.
    • Use terms appropriate to a Google search - poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice tell us nothing.
    • Responding to a request to change your thread title by doing so is mandatory.

    To change a title go to your first post, click EDIT then Go Advanced and change your title.

    No help to be offered, please, until the OP complies with this request.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  3. #3
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,830

    Re: Declare Varible

    Sorry - that still won't do. You need to give more detail in your title.

  4. #4
    Forum Contributor
    Join Date
    02-09-2009
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    380

    Re: Declare Varible

    Hi nordicdust, welcome to the forum. Is this really one of the first macros you've tampered with? This isn't easy, you've gut all sorts of subs all calling each other and I only learned a little about this the other day (been tinkering for 2 years with vba now thanks to this article https://excelmacromastery.com/excel-vba-sub/).

    I'm not even sure exactly how a private sub is restricted so it's too much for my capabilities but assuming GOTO_DATA is the calling sub. Running a password that's not Admin just jumps to Wrong Password.
    Inputting Admin as the password goes to WS.Unprotect pw which looks like it wants to be a call to a function/ sub called unprotect but there's no unprotect method there in the project. Unless you are intentionally applying a property called unprotect to the worksheet object?
    Attached Images Attached Images

  5. #5
    Forum Contributor
    Join Date
    03-08-2018
    Location
    Denmark
    MS-Off Ver
    2016 for Windows
    Posts
    413

    Re: Declare Varible

    Hi Ali
    Sorry for that, I hope my title is better now.
    I will keep this in mind for future help.

  6. #6
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,830

    Re: Declaring Variables with Public

    Thank you - that will do.

  7. #7
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Declare Varible

    Hi,

    Perhaps it would be best of you uploaded the workbook and explained in a narrative, with reference to sheets/cells where necessary, what the overal aim is and how you wish to interact with it. We're less interested in what your code is attempting to do so don't describe that. We can often suggest better and more efficient ways of achieving your goal.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  8. #8
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Declaring Variables with Public

    Hi,

    I think CPearson has lots of information to help with this topic. Read it at:

    http://www.cpearson.com/excel/Scope.aspx

    I also find MSDN a good source.

    https://msdn.microsoft.com/en-us/vba...ring-variables
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  9. #9
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Declaring Variables with Public

    @Dal123, the significance of public/private is that Public variables, subs, functions are available to VBA calls from any Module, while Private ones are only available to the Module they reside in.

    @nordicdust, You say the code is not working, but it is not clear to me what the actual problem is. I have never tried to use "Dim" outside of a module (right under "Public" section, you have 3 "Dim"). Depending on your issue, changing those Dim to Public COULD help...
    Last edited by Arkadi; 04-11-2018 at 02:57 PM. Reason: fixed typo
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  10. #10
    Forum Contributor
    Join Date
    03-08-2018
    Location
    Denmark
    MS-Off Ver
    2016 for Windows
    Posts
    413

    Re: Declare Varible

    Hi Dal

    No, not my first, my 3rd workbook with macros. I have been at this for about 2-3 months.
    There is at lot of help around and here, just by reading others problems and youtube is also great.

    The code i put up is just a litle of it, the rest is mostly small macros, but it is in the start something is wrong, and i am sure it is with my declaring.
    All sheets are locked so only the cells for input are unlocked. Workbook is also locked so the users can't ad sheets.
    As most of my macros need to unlock and after lock, i have made the Public Sub "UnlockAndBoost" macro that the macros call to. There should be both unlock and lock in the macro.
    I am not sure (of anything) but i think that declaring a sub with public makes it avalible for calling whithin the module as Excel dosent clear it from memory.
    Again, i am not sure of anything :-)
    But thanks for taking a lock :-)
    Last edited by nordicdust; 04-12-2018 at 12:14 PM.

  11. #11
    Forum Contributor
    Join Date
    02-09-2009
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    380

    Re: Declaring Variables with Public

    Thanks Marvin & Arkadi.

    Nordicdust by the way, VBA hasn't been developed in around 8 years and Microsoft have confirmed they are no longer developing it.
    I found this out by another helpful member the other week but I've been looking through my previous posts and can't find it to link for your reference (apologies). So all the work we're putting in could be wasted. As I'm sure you've thought to yourself VBA can be quite a silly language sometimes and makes things really difficult for us and I've personally struggled with it's logic, sometimes it just makes no sense whatsoever !
    Last edited by Dal123; 04-12-2018 at 04:30 AM. Reason: Thanking missed people due to lag.

  12. #12
    Forum Contributor
    Join Date
    03-08-2018
    Location
    Denmark
    MS-Off Ver
    2016 for Windows
    Posts
    413

    Re: Declaring Variables with Public

    Dal, you just ruined my life
    Yes, I was told that it is more web based today. But I belive it will come in handy for me anyway.

  13. #13
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Declaring Variables with Public

    The correct tool to solve a problem is the one that solves that problem.

    Excel has survived because it has LOTS of tools built into it. Tools like: Sort, Filter, Conditional Formatting, AutoFilter, Format Cells, Formulas (hundreds of them), and many more. Excel even has old tools that might be useful like "Data Form Tool" (https://www.lifewire.com/excel-data-entry-form-3123427) which is hidden from normal use. VBA hasn't been improved for many years but is still a tool that I use, and has solved many of my problems.

    The newer tool(s) in Excel come in the Data Tab and using "Get & Transform" (sometimes called Power Query) to combine large sets of data. With more data in the world, Excel is trying to keep up with tools that help solve this newer problem. Power Query is a different egg than VBA as it uses a scripting type language. I find many of my VBA tasks can be done easier using PQ, like combining all the files in a single folder into a single file. This used to be much harder using VBA.

  14. #14
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Declaring Variables with Public

    Well put Marvin.

    And on the 'old' tools front I still occasionally use the old Excel4 'macros' which I sometimes find more useful than the common or garden modern stuff we have.

  15. #15
    Forum Contributor
    Join Date
    03-08-2018
    Location
    Denmark
    MS-Off Ver
    2016 for Windows
    Posts
    413

    Re: Declare Varible

    Hi Richard
    Thanks for your reply.
    I have attached the file, I hope you can find the problem.
    Hmmm i am not sure i know how to attach the file
    Last edited by nordicdust; 04-11-2018 at 03:38 PM.

  16. #16
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Declare Varible

    I don't see the file.

    The paperclip attach item doesn't work - no don't ask. Use the Go Advanced button on your post and then the 'Manage Attachments' option underneath the post area.

  17. #17
    Forum Contributor
    Join Date
    03-08-2018
    Location
    Denmark
    MS-Off Ver
    2016 for Windows
    Posts
    413

    Re: Declaring Variables with Public

    There, i did it :-)
    Attached Files Attached Files

  18. #18
    Forum Contributor
    Join Date
    03-08-2018
    Location
    Denmark
    MS-Off Ver
    2016 for Windows
    Posts
    413

    Re: Declare Varible

    Did I do it right, with uploading?

  19. #19
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Declaring Variables with Public

    Quote Originally Posted by Arkadi View Post
    [...]I have never tried to use "Dim" outside of a module (right under "Public" section, you have 3 "Dim"). Depending on your issue, changing those Dim to Public COULD help...
    Any declarations using Dim inside a module before the first Sub or Function are global to the module and also static, but not visible outside the module. (I haven't analyzed whether such a change will help in this case.)
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  20. #20
    Forum Contributor
    Join Date
    03-08-2018
    Location
    Denmark
    MS-Off Ver
    2016 for Windows
    Posts
    413

    Re: Declaring Variables with Public

    I changed them to Public, But no difference.

  21. #21
    Forum Contributor
    Join Date
    03-08-2018
    Location
    Denmark
    MS-Off Ver
    2016 for Windows
    Posts
    413

    Re: Declaring Variables with Public

    I have read that 'UserInterfaceOnly' can't be used with 'Protect WorkBook' Can that be my problem?

  22. #22
    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: Declaring Variables with Public

    That is correct. You should remove that argument from the Workbook_Open code.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  23. #23
    Forum Contributor
    Join Date
    02-09-2009
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    380

    Re: Declaring Variables with Public

    My impression of the problem (and take this with a pinch of salt nordicdust as the other members who have posted are far more knowledgeable than myself and have kindly helped me greatly) is:
    • The Workbook & Worksheet object variables are not actually set (a value isn't assigned to them).
    VBA's reading through the code and it's saying to itself "What workbook and worksheet are you talking about man? I know you're referring to a workbook or worksheet; but which one?". I've made a few changes (mainly passing the activeworksheet and activeworkbook as an optional parameter; probably would be better to set this as an explicit parameter as it's needed to run the code). This article helped me with passing subs/ function parameters.
    I'm running into out of range errors as I don't have the worksheets in my workbook as I'm running from your original code posted but it seems that as you mentioned you don't have the object variables set. An object variable is assigned similarly to a normal variable except the set keyword must be used first:
    Please Login or Register  to view this content.
    You'll run into these problems again when you jump into another procedure/ sub that doesn't have the value (or possibly even the declaration of the variables/ object variables) which are in that particular subroutine/ procedure.

    Hopefully this makes a bit of sense but I'm not the clearest in explaining my points.

    As I say I know very little, however making these changes gets your code to run a lot further than original; when playing around with your original code on my system.
    Please Login or Register  to view this content.

  24. #24
    Forum Contributor
    Join Date
    03-08-2018
    Location
    Denmark
    MS-Off Ver
    2016 for Windows
    Posts
    413

    Re: Declaring Variables with Public

    Thanks Dal, I will look into 'passing subs/ function parameters' as that is rather confusing
    If you are up to it, I uploaded the full file yesterday, look 6 or 7 posts up
    I will have a look at your corrections when I get home this evening.
    But, really thanks for your help and effort, deeply appreciated

  25. #25
    Forum Contributor
    Join Date
    02-09-2009
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    380

    Re: Declaring Variables with Public

    You're welcome nordicdust, I'll try to have a look at your file but I've got a mountain of work on at the moment so I can't guarantee I can.

    Yes passing subs/ procedures is very confusing; but very powerful! That's what got me so shocked as you're doing advanced stuff so early on your third macro, you're calling lots of subs/ functions and they refer to variables/ object variables (which is the whole point of vba; not having a go here) but it's awkward to get your head around at first (especially if this is your third macro).
    I've been tinkering with vba for 18 months now, 3-4 months full-time 12-13 hours a day and passing subs/ functions is only starting to click now. Paul Kelly's article I posted is what did it for me though a massive amount of time reading every article out there on objects, functions, returning values inbetween, and Paul's other articles on arrays, dictionaries and the like also helped me a great deal. Another great resource is Wise Owls VBA Tutorials on Youtube.

    Do not get frustrated, passing subs/ functions is considered a fundamental aspect of coding, however it is complex and you will not get it instantly! Have a little look at what I've done already and we'll try to tackle each problem as we move forward; you'll learn quicker and better that way!

+ 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. Hello, excel noobie here !
    By lovecuffs in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 05-05-2016, 01:54 PM
  2. Noobie
    By TheDirtyZombie in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 10-14-2014, 05:34 PM
  3. Noobie into
    By biederboat in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 04-21-2014, 08:10 PM
  4. Greetings everyone! Excel VBA noobie here :D
    By someyoungguy in forum Hello..Introduce yourself
    Replies: 3
    Last Post: 07-11-2013, 06:10 AM
  5. Noobie
    By zuckey04 in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 12-11-2012, 01:36 PM
  6. Noobie Question.
    By Oswald in forum Excel General
    Replies: 1
    Last Post: 07-15-2010, 08:53 AM
  7. File name varible problem
    By Casey in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-06-2006, 01:28 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