Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old January 18th, 2005, 08:18 AM
Authorized User
Join Date: Nov 2004
Location: , , .
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default Beginner Question

Can someone explain to me why sometimes coders choose to use "Sub" instead of "Private Sub" or "Public Sub" and what is the difference?


  #2 (permalink)  
Old January 18th, 2005, 09:37 AM
Authorized User
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts

In VB/VBA the keyword "Sub" indicates you are starting a procedure (a set of executable statements). Implicit in it is the "Public" statement, ie. "Sub MyProcedure()" and "Public Sub MyProcedure()" are exactly the same.

The difference between Public and Private is simply the scope you want to give that procedure. In real terms, it means if you want a procedure to be available to other procedures outside of its container module/class/whatever then you will need to declare it as Public; if not, declare it as Private.

It's really down to how you want to organise your code, and/or how much you want exposed to the user. Remember that in VBA projects a Public procedure/function is not available to any other project unless you explicitly set a reference to that project (using Tools | References)

Public and Private statements also apply to variables, constants, properties in the same way.

Similar Threads
Thread Thread Starter Forum Replies Last Post
xslt beginner question cfflexguy XSLT 4 October 16th, 2007 09:11 AM
beginner C++ question p62 proslambano BOOK: Ivor Horton's Beginning Visual C++ 2005 4 February 7th, 2007 04:23 PM
Beginner question on getting only certain elements EstherMStrom XSLT 5 December 10th, 2004 04:38 PM
beginner question saudyonline General .NET 2 September 21st, 2004 01:56 AM
Stupid Question from a BEGINNER Shappy Access VBA 1 May 11th, 2004 03:32 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.