VBA Code

The difference between "ThisWorkbook" and "ActiveWorkbook" refers to the workbook that is currently in the active window, whereas "ThisWorkbooK" refers to the workbook where the code is actually running from.


loop through all the worksheets
loop through all the workbooks in a folder



Activating and Selecting

Workbooks("Wbk1.xls").Activate 

Can be used if the workbook is new and has not yet been saved.

Workbooks("Book3").Activate 
Workbooks("Book3.xls").Activate

assuming that the workbook has been saved

Workbooks.Item(2).Activate 

This refers to the workbook that contains the code.

ThisWorkbook. 


ThisWorkbook

This is always the workbook that contain the code.

Application.ThisWoorkbook 
ThisWorkbook


ActiveWorkbook

This is the worksheet that is currently active or selected

Application.ActiveWorkbook 
ActiveWorkbook
ActiveWorkbook.Path = "C:\Temp"
ActiveWorkbook.Name = "Book2"


Application.Height 
Application.Left
Application.Top
Application.Width

Application.UsuableWidth 
Application.UsuableHeight



Application.ProductCode 
Application.Hwnd
Application.MemoryUsed
Application.UsedObjects

objWorkbook.CreateBackup = False 


ThisWorkbook.NewWindow 
Windows(2).Activate

FullName

There is only a folder path when the workbook has been saved
This property is equivalent to the Path property, followed by the current file system separator, followed by the Name property.

ActiveWorkbook.FullName 


Determines the version of a workbook, i.e. which version of Excel created this workbook.

ActiveWorkbook.FileFormat 

16 = Excel 2
29 = Excel 3
33 = Excel 5
39 = Excel 5/96
-4143 = Excel 97/2000/2002/2003



undo the last action performed by the user interface.

Application.Undo 
Application.Repeat


Returns the collection of recently opened files

Application.RecentFiles 

Read-only returns an object allowing manipulation of the office assistant.

Application.Assistant 

Some of the content in this topic may not be applicable to some languages.


Application Caption

Application.Caption = "message" 

Status Bar

It is possible to add your own messages to the status bar.
This can be used as a way to keep the user informed of the current action when a macro takes quite a long time to run.

Application.StatusBar = "Processing .." 
Application.StatusBar = False

It is important to reset the Statusbar property to False when your macro has finished.
Otherwise your last message will remain in the statusbar until Excel is closed.


Whenever referencing workbooks or worksheets always enclose them in single speech marks "WshName" as they could contain spaces and/or unusual characters.
When you open a workbook using VBA even if you have the Application.DisplayAlerts = False you will be prompted whether to update links. To prevent being prompted change the "updatelinks" property in the Open method.
You can write an event procedure that will work for any workbook that is open but you need to use a class module
If you want something to happen to all the workbooks in a workbook use the "SheetActivate" event for "ThisWorkbook".
In previous versions of Excel the Auto_Open event was used to execute when a workbook is opened. It has since been supplemented / replaced with the Workbook-Open event which is stored in the ThisWorkbook module. Workbook_Open is executed before the Auto_Open event.
Event sequences are not always in an obvious order. The SheetActiveWorkbook event actually occurs before the WorkbookNewSheet. Application event when a new worksheet is added to a workbook.
Workbook event procedures must be in the Code Module for the "ThisWorkbook" Object. If they are anywhere else they will be ignored.
If you want to obtain the number of workbooks that are open you must remember if you use workbooks.count this will include any hidden workbooks (including your Personal.xls) You must check for visibility as well.
If you are referencing a value in another workbook use Workbook(---.Sheets(---).Range().value instead of "Worksheets".


Application.WindowState = xlMinimised 
ActiveWorkbook.Windows.Arrange ArrangeStyle := xlArrangeStyleTiled, SyncHorizontal := True

ActiveWorkbook.Visible = False 
ThisWorkbook.CustomViews.Add "my view name"

This returns the file name

ThisWorkbook.Name 

This returns the folder path

ThisWorkbook.Path 

This returns the folderpath and filename

ThisWorkbook.FullName 

To make sure that code works on both Windows and Macintosh use: Since the windows uses "\" and macs use ":".

Application.PathSeparator 


Application.Workbooks.Count 
sFolderPathAndFileName = ThisWorkbook.FullName
Workbooks(Range("A2").Worksheet.Parent).Name
?? = Workbooks.Count
?? = Workbooks.Item(3)
ThisWorkbook.ChangeFileAccess xlReadOnly

Workbooks("Book2.xls").RefreshAll 
Set wbktemp = Workbooks.Filename :="temp.xls"

Speech (new 2002)

Excel 2002 offers a new Speech object that allows the control of automatic speech output and the output of text via the sound card'


AnswerWizard

There's only one Answer Wizard per application, and all changes to the AnswerWizard or the AnswerWizardFiles collection affect the active Office application immediately.

Application.AnswerWizard 
Application.AnswerWizard.ResetFileList


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext