Sunday, April 14, 2019

ĐỐI TƯỢNG RANGE TRONG EXCEL VBA

Range là một đối tượng (Object) trong Excel VBA, Range có thể là một cell hoặc nhiều cell (ô), Range là một thuộc tính của sheet, một sheet có thể có nhiều Range nhưng một Range thì chỉ có một sheet.
Range-600x360

1. Thuộc tính – Propertys
  • Thuộc tính Font – Formatting font
Range bao gồm các cell do đó nó có các thuộc tính font, một số thuộc tính font như: name, bold, itatic, underline, Size, Fontstyle, Colorindex, Color những thuộc tính này chúng ta có thể sử dụng cùng với nhau:
Range (“YourRange”).Font.Bold=True
Range (“YourRange”).Font.Italic=True
Range (“YourRange”).Font.Underline=True
Hoặc
Range (“YourRange”).Font.FontStyle=”Bold Italic”
Ví dụ 1
Private Sub CommandButton1_Click()
Range(“A1:A3”).Font.Bold = True
Range(“A1:A3”).Font.Italic = True
Range(“A1:A3”).Font.Underline = True
Range(“A1:A3”).Font.Size = 20
Range(“A1:A3”).Font.FontStyle = “Bold Italic Underline”
Range(“A1:A3”).Font.Name = “Time News Roman”
End Sub
Ví dụ 2: ColorIndex
Private Sub CommandButton2_Click()
Range(“A4:B10”).Font.ColorIndex = 4
End Sub
Ví dụ 4: Formular
Ví dụ 4.1
Range_vidu4-600x360
Private Sub CommandButton1_Click()
Range(“A1:B3”).Columns(3).Formula = “=A1+B1”
Range(“A1:B3”).Columns(3).Formula = “=Sum(A1:B1)” ‘ tổng
Range(“A1:B3”).Columns(3).Formula = “=Average(A1:B1)” ‘ giá trị trung bình
End Sub
Ví dụ 4.2
Range_vidu4-1-600x360
Private Sub CommandButton1_Click()
Range(“A1:E4”).Columns(6).Formula = “=Mode(A1:E1)”  ‘yếu vị trong 1 dòng
Range(“A1:E4”).Columns(6).Formula = “=Median(A1:E1)”  ‘ Giá trị trung bình
Range(“A1:E4”).Columns(6).Font.Bold = True
Range(“A1:E4”).Columns(6).Font.ColorIndex = 3
End Sub
Ví dụ 5: Màu nền Range
Private Sub CommandButton1_Click()
Range(“A1:A3”).Interior.Color = vbYellow
End Sub
2. Phương pháp – The Range Methods
Ví dụ 6: phương pháp Autofill  – Autofill Method 
Private Sub CommandButton1_Click ()
Set myRange = Range (“A1:A2”)
Set targetRange = Range (“A1:A20”)
myRange.AutoFill Destination: =targetRange
End Sub
Ví dụ 7: Select, Copy and Paste Methods
Private Sub CommandButton1_Click ()
Range (“C1:C2”).Select
Selection.Copy
Range (“D1:D2”).Select
ActiveSheet.Paste
End Sub
Ví dụ 7: Copy and PasteSpecial Methods 
Range_vidu8-600x360
Private Sub CommandButton1_Click ()
Range(“C1:C2”).Copy
Range(“D1:D2”).PasteSpecial Paste:=xlPasteValues
Range(“E1:E2”).PasteSpecial Paste:=xlPasteFormulas
Range(“F1:F2”).PasteSpecial Paste:=xlPasteFormats
Range(“G1:G2”).PasteSpecial Paste:=xlPasteAll
End Sub

No comments:

Post a Comment

Over 50 Ebooks Excel and VBA free Download

1. Statistics and Probability for Engineering Applications With Microsoft Excel by W.J. DeCoursey - PDF Free Download Download Siz...