Monday, May 24, 2010

EXPERIMENTS 001

All experiments will start the way as shown and written below.
Open MS Excel. Save it as 'Book1'. Press Alt + F11. Following screen appears:

Right click 'VBAPROJECT (Book1).  Select Insert à  Module. A module in the name of 'Module1' opens.  The codes are to be written on this module. You can start another module by repeating the step at written above. After you have written down the code, come out of Visual Basic window by closing it.

The command button is created by right clicking menu bar, clicking 'Forms'. 'Forms' window opens. Click 'Button' and drop onto the excel sheet. It appears with its default caption 'Button 1' or 'Button 2'. Double click the caption, edit the name the way you like and as you do in MS Word. A prompt window will appear asking you to 'Assign Module' to this button. If you have already some corresponding module written in MS Visual Basic window, you can assign the same to it. The click of button will run it.

EXPERIMENT 001: CREATING LIST OF SERIAL NUMBERS AGAINST ANOTHER LIST.
Please see the given figure. The names are given in 'Name' column. The first column is 'No,' we have created a command button, which when clicked, fills the serial number against each name starting from 1.
Open MS Visual Basic window by pressing Alt + F11. Write the code given below:
Obviously the line of code starting with a single quote i.e. ' is the remark where the coding has been explained.

Public Sub pCountF()
'pCountF is the name of the procedure.
Dim P As Integer, I As Integer

Range("B3").Select
While ActiveCell.Value <> ""
ActiveCell.Offset(1, 0).Select
P = P + ActiveCell.Count
Wend
'The code written above works like this.
'The cell 'B3" is selected, where first name appears.
'All the cells down this cell are selected one by one.
'An integer 'P' created for this work, stores the value

Range("C1").Select
ActiveCell.Value = P
'The cell 'C1" is selected, where the value of P is written.
'Though it is not necessary to take the value of P on excel sheet.
'In big programmes, you may need to use it at later stage, hence taken.

Range("A2").Select
For I = 1 To P
ActiveCell.Offset(1, 0).Select
ActiveCell.Value = P - (P - I)  '=I
Next I
'The code written above works like this.
'The cell 'A2" is selected, below which the counts have to begin.
'All the cells down this cell are selected one by one.
'The serial numbers starting from 1 to P are written one by one
'on empty cell below one another.

Range("A2").Select
Exit Sub
End Sub

Assign this procedure to command button. The click of button will run it.





No comments:

Post a Comment