Thursday, August 6, 2009

Some More Basics

Hi guys,
here are some basics which will help you always while writing the macro
1) To declare the workbook:
Dim wb1 As Workbook
Dim ws1 As Worksheet

2) To set the workbook to be used in program :

Set wb1 = Workbooks("Book1.xls")
Set ws1 = wb1.Sheets(1)

now, "ws1" will correspond to first sheet of "Book1.xls"
2) To write a value in a cell:
if i want to write "Hello excel" in cell B3 then just set the workbook as tole in 1 and 2

ws1.Cells( 3,2 ) = "Hello Excel"

here 3 --> row and 2--> column i.e. B in this case.

3) to take the value of the cell for operations (for eg : compare value with some constant):

Dim abc as integer
abc = ws1.Cells( 3,2 ).Value

This will assign the content of cell "B3" to variable "abc"
Next, I will give some sample code snippets to do magic with excel.
As always , please let me know anything you would like to do with excel @ kashiwar.h@gmail.com
Thanks,
Himanshu

No comments:

Post a Comment