Está en la página 1de 4

Record Macro Open xlmacros.

xls, sheet 1 Go to Tools on Menu bar and select Macros Record macro Fill in the required details in Macros dialog box. Select cells A1 to D10 Apply the following formats to selected cells Change font to Tahoma and font size to 12 Right align and bold Change font color to red and pattern to yellow Stop recording Macro View Macro Code in Visual Basic Editor Run Macro OOPs - Object Oriented Programming What are the objects in Excel?? Sheet Cells - Rows, columns, range Workbook Charts "Object.Method" or "Object.Property" Range Property Range("A1:D10").select Range("A1,D10").select Range("A1").select Cells Property Cells(row num, col num).selct Cells(2,1).select Cells(3,5).select Rows Property rows(rowindex).select rows(10).select Column Property columns("A:A").select Columns("A:B").select.....?! Worksheets Property worksheets("JunkFood").range("A1:B10").select sheet1.range("A1:B10").select Sheet2.cells(2,3).select Workbooks("trail_book1.xls").sheet1.cells(1,1).select Write a simple program to display sum of two cells in a message box.

Sub sum() Dim a, b, total as integer a = cells(1,1).value b = cells(2,1).value total = a + b Cells(3,1).value = total End Sub Conditional Statements 1. 2. 3. 4. Simple IF IF and ELSE IF ELSEIF Nested IF

Codes: Sub sum() 'code to add 2 nums from the sheet Dim a, b, total As Double Sheet2.Activate a = Cells(1, 1).Value b = Cells(2, 1).Value total = a + b 'Cells(3, 1).Value = total MsgBox "The sum total is " & total End Sub Sub try() Sheet1.Activate Range("A1:D5").Select Selection.Interior.ColorIndex = 5 End Sub Sub condition() 'code to check the value in a single cell and color code it Sheet2.Activate dim i as integer For i = 2 To 20 If Sheet2.Cells(i, 3).Value > 40 Then Sheet2.Cells(i, 3).Interior.ColorIndex = 7 ElseIf Sheet2.Cells(i, 3).Value > 30 Then Sheet2.Cells(i, 3).Interior.ColorIndex = 9 End If Next i End Sub Looping Statements:

Initialization Condition Increment For Loop -> For.... Next For i = 1 to 10 . . . Next i "=> i=i+1" Do While Loop i = 1 ->Initial Do while <condition> i<=20 . . . . i=i+1 -> increment Loop Do Until Loop Do.... Loop While Do.... Loop Until Sub condition12() 'code to check the value in a single cell and color code it Dim i As Integer Sheet2.Activate i = 2 Do While Sheet2.Cells(i, 3).Value <> "" ......... ==> Only if condn is satisfie d, control enters the loop If Sheet2.Cells(i, 3).Value > 40 Then Sheet2.Cells(i, 3).Interior.ColorIndex = 7 ElseIf Sheet2.Cells(i, 3).Value > 30 Then Sheet2.Cells(i, 3).Interior.ColorIndex = 9 ElseIf Sheet2.Cells(i, 3).Value > 20 Then Sheet2.Cells(i, 3).Interior.ColorIndex = 12 End If i = i + 1 MsgBox i Loop End Sub Sub condition13() 'code to check the value in a single cell and color code it Dim i As Integer Sheet2.Activate i = 2 Do Until Sheet2.Cells(i, 3).Value = "" ......... ==> Only if condn fails, contr ol enters the loop If Sheet2.Cells(i, 3).Value > 40 Then Sheet2.Cells(i, 3).Interior.ColorIndex = 7 ElseIf Sheet2.Cells(i, 3).Value > 30 Then

Sheet2.Cells(i, 3).Interior.ColorIndex = 9 ElseIf Sheet2.Cells(i, 3).Value > 20 Then Sheet2.Cells(i, 3).Interior.ColorIndex = 12 End If i = i + 1 MsgBox i Loop End Sub

También podría gustarte