MS-Excel-VBA-Macros Basic functions to begin with

This is my third blog on MS-Excel-VBA-Macros. Earlier, I have posted two blogs
Enabling VBA in Microsoft Office and Excel-VBA-Macros Tips and Tricks. They will prepare one to shun and shed initial inhibitions with Excel-VBA-Macros. This blog will teach how to activate a workbook, sheet or select a cell, execute those inbuilt excel functions. As usual the teaching will be supported by equally good examples.

Controlling Workbooks
  • to open a workbooks -   
dim D as workbook
set D =workbooks.open("name of the excel file with full path")
  • to activate a workbook (in case we have multiple workbook) - D.activate
  • to save a workbook - D.save
  • to close a workbook - D.close

Controlling Sheets
  • Adding a new sheet - sheets.add
  • Activating a sheet - sheets(sheet number).activate
  • Deleting a sheet - sheets(sheet number).delete
  • Renaming a sheet - sheets(sheet number).name="XXXXXXX"

 Accessing a cell or range (Collection of cells)
  A cell is unique in itself. Unique row and column number differentiate one cell with other.
  • Selecting a cell - cells(x,y).select
  • Assigning a value to a cell - cells(x,y).value=k
  • Accessing a cell value - t=cells(x,y).value
  • Selecting a range - range(cells(x,y),cells(x1,y1)).select
  • Selecting a entire row/column - row(x).select or column(x).select 


VBA Code with comments

 Accessing MS-Excel in built functions
MS-Excel is one of powerful tools available. It supports good chunk of mathematical functions which make our life easier and simpler as well. 

cells(x,y).value = Application.worksheetfunction.Excel function Name (Range of cells)

There is an important point which requires our attention. If one analyzes the examples, he/she will easily point out the same. A cell/range is referenced with respect to an active worksheet which in turn is a part of active workbook. An absolute reference to a cell can be made D.sheets(1).cells(x,y).value=k

Hope this helps reader out there in accessing a workbook, sheet and more importantly a cell. One needs to practice and read a lot to unleash power of VBA. 

Readers who wish to learn and read more on Excel and VBA can find and buy books online HERE.

Happy VBA.....ing