Excel Macros
Excel Macros
Sub mac1()
MsgBox "EXCEL IS A analysis tool"
MsgBox 100
End Sub
2. Apply arithmetic operations on two cells using macros.
Sub add_values()
Range("g12").Value = Range("f5").Value - Range("f6").Value
End Sub
3. How to align the text using macros.
Sub align()
Range("a1:c5").HorizontalAlignment = xlLeft
Range("a1:c5").HorizontalAlignment = xlRight
Range("a1:c5").HorizontalAlignment = xlCenter
Range("a1:c5").VerticalAlignment = xlTop
Range("a1:c5").VerticalAlignment = xlBottom
Range("a1:c5").VerticalAlignment = xlCenter
End Sub
Sub background_color()
Range("a1:a10").Interior.Color = vbRed
Range("a1:a10").Interior.Color = vbWhite
Range("a1:a10").Interior.Color = vbGreen
Range("a1:a10").Interior.Color = vbYellow
Range("a1:a10").Interior.Color = vbBlack
Range("a1:a10").Interior.Color = vbBlue
Range("a1:a10").Interior.Color = vbMagenta
Range("a1:a10").Interior.ColorIndex = 1
Range("a1:a10").Interior.ColorIndex = 10
Range("a1:a10").Interior.ColorIndex = 20
Range("a1:a10").Interior.ColorIndex = 56
End Sub
5. How to change the border color and style of the cells using macros.
Sub border()
Range("a1:a10").borders.Color = vbRed
Range("a1:a10").borders.Weight = 3
Range("a1:a10").borders.LineStyle = xlDot
Range("a1:a10").borders.LineStyle = xlDash
Range("a1:a10").borders.LineStyle = xlContinuous
Range("a1:a10").borders.LineStyle = xlDouble
Range("a1:a10").borders.LineStyle = xlNone
End Sub
7. How to copy the data from one cell and paste it into another.
Sub copy_paste()
Range("a1:a10") = "Tutorial"
Range("b1:b10") = Range("a1:a10").Value
'2nd method
Range("a1:a10").Copy
Range("d1:d10").PasteSpecial
Application.CutCopyMode = False
End Sub
8. How to change the font color of the text in a cell using macros.
Sub font_color()
Range("a1:c6").font.Color = vbGreen
Range("b7:c10").font.Color = vbWhite
Range("b7:c10").font.Color = vbYellow
Range("b7:c10").font.Color = vbBlue
Range("b7:c10").font.Color = vbCyan
Range("b7:c10").font.Color = vbMagenta
Range("b7:c10").font.Color = vbRed
Range("b7:c10").font.ColorIndex = 1
Range("b7:c10").font.ColorIndex = 10
Range("b7:c10").font.ColorIndex = 56
Range("a1:c6").font.Color = vbGreen
End Sub