+ Reply to Thread
Results 1 to 12 of 12

What are Dim, Modifier and Sub ( ) vs Private Sub ( )?

  1. #1
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    What are Dim, Modifier and Sub ( ) vs Private Sub ( )?

    Hi,

    I know above are the things explailed all over the web and one does not need much google to find these.

    But nobody has explained these things for a layman who is new to programing world.

    I have made some good macros but still I don't know what are following things;

    1- What the _ell is the dim word? What if it were never born?

    2- Why do we call them modifiers?

    3- Why some people prefer to write Private Sub Name() instead of simply writing Sub Name()

    Please explain above things to a layman.

    (I am not a lay man)
    Last edited by caabdul; 01-26-2014 at 03:16 PM.

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: What are Dim, Modifier and Sub ( ) vs Private Sub ( )?

    Selam.

    Dim is short for Dimension. You are declaring a variable.

    Most of the time you do not need to use it and your macro will work.

    However. If you are using arrays it is a good idea to dimension it before you try and use it.


    a macro with the name format
    Please Login or Register  to view this content.
    can be called manually, through a command button or from another macro.

    a macro with the name format
    Please Login or Register  to view this content.
    can only be called from within Visual Basic maybe by another macro or a command button on a user form. They cannot be called manually as they do not appear on your run macro list.

    This is useful because if you had every Macro on your run macro list you could not cope. For example in some userforms I can have 100 command buttons and text boxes.
    If each associated macro were on your run macro list you could not cope.

  3. #3
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    Re: What are Dim, Modifier and Sub ( ) vs Private Sub ( )?

    I didn't get to the dim but your explanation for Private Sub () is much appreciated! Thank you

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,621

    Re: What are Dim, Modifier and Sub ( ) vs Private Sub ( )?

    Dim is one of the ways to allocate the computer's memory. The amount of memory allocated will depend on the TYPE that you declare the variable as (e.g. Long, Integer, Double, String, ...). You should always Dim every variable you use in your macros; also you should always use OPTION EXPLICIT at the top of the module before any sub's. If you do not, then misspelling a variable in you code will always cause VB to create a new (different variable than the one you thought you were using).
    Ben Van Johnson

  5. #5
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    Re: What are Dim, Modifier and Sub ( ) vs Private Sub ( )?

    Pro! Your explanation for dim was PRO. Thank you

    I hope there is no other functioning of dim except that it makes the code memory efficient, that is what I understand.


    But, I don't remember when I asked about OPTION EXPLICIT.

  6. #6
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    Re: What are Dim, Modifier and Sub ( ) vs Private Sub ( )?

    Now what is this OPTION EXPLICIT

  7. #7
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,621

    Re: What are Dim, Modifier and Sub ( ) vs Private Sub ( )?

    OPTION EXPLICIT
    causes VB to post an error message ("variable not defined") if you try to use a variable that has not been Dim'd (maybe because of a misspelling). Also, if you try to assign data to variable which does not match the TYPE in the Dim statement you will get an error ("type mismatch"). For instance if you define, say TOTALCOUNT as Long, then somewhere in your code you say: TOTALCOUNT= "testing 123" (a string) you will get that error.
    OPTION EXPLICIT makes troubleshooting code much easier and you should insist on using it always.

  8. #8
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    Re: What are Dim, Modifier and Sub ( ) vs Private Sub ( )?

    Hmmm! It means OPTION EXPLICIT is really

    Thank you PRO Sir!

  9. #9
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    Re: What are Dim, Modifier and Sub ( ) vs Private Sub ( )?

    I have seen the following macro name;

    "Public Sub Initialize(ByVal Number As String, ByVal name As String, ByVal Importance As String, ByVal Estimation As String, _
    ByVal Note As String, ByVal HowToDemo As String, ByVal Complete As String)"

    Is this some new gener?
    Also, I have learned that a macro name can't contain spaces, but above is working fine. How?

  10. #10
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,621

    Re: What are Dim, Modifier and Sub ( ) vs Private Sub ( )?

    The name of the macro is 'Initialize" there are no spaces,
    It is subroutine and not a User Defined Function
    It is being called by some other macros in the project which pass it values (ByVal Number as String, ... ) to operate on.

  11. #11
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: What are Dim, Modifier and Sub ( ) vs Private Sub ( )?

    Which raises the question: "What's this ByVal thing...?"

    Might suggest the OP would benefit from having a read of Chip Pearsons' introduction to VBA. In particular:
    • Declaring Variables (Includes the use of 'Option Explicit'
    • Scope of Variables and Procedures
    • Passing Variables ByRef and ByVal


    And, as a bonus for when he really gets into it:
    • Error Handling in VBA


    The index page is here

    (I would have included direct links except for the limit imposed by the board - instead listed the topic titles as they appear on the Index page.

  12. #12
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    Re: What are Dim, Modifier and Sub ( ) vs Private Sub ( )?

    cytop! are you asking something or explaining something?

+ 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. Change result of applying modifier
    By Excelnoob1234 in forum Excel General
    Replies: 1
    Last Post: 09-10-2012, 10:37 AM
  2. [SOLVED] Using a NOT modifier within Search()
    By weeble33 in forum Excel General
    Replies: 6
    Last Post: 06-21-2012, 02:11 PM
  3. Modifier based sum
    By Janbi in forum Excel General
    Replies: 12
    Last Post: 11-10-2009, 05:53 AM
  4. Key modifier to toolbar button -- HELP
    By JuergenL in forum Excel General
    Replies: 0
    Last Post: 04-08-2005, 11:47 AM
  5. Key modifier to toolbar button ??
    By JuergenL in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-08-2005, 08:28 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