Professional Documents
Culture Documents
Intersect
Intersect
Trong cửa sổ VBA ta gỏ Intersect , quét chọn toàn bộ và nhấn {F1} ta sẽ nhận được
phần trợ giúp về phương thức Intersect như sau:
Intersect Method
Returns a Range object that represents the rectangular intersection of two or more
ranges.
expression.Intersect(Arg1, Arg2, ...)
expression Optional. An expression that returns an Application object.
Arg1, Arg2, ... Required Range. The intersecting ranges. At least two Range objects
must be specified.
Example
This example selects the intersection of two named ranges, rg1 and rg2, on Sheet1.
If the ranges don't intersect, the example displays a message.
Worksheets("Sheet1").Activate
Set isect = Application.Intersect(Range("rg1"), Range("rg2"))
If isect Is Nothing Then
MsgBox "Ranges do not intersect"
Else
isect.Select
End If
End If
End Sub
Nếu ta đã đặt tên cho vùng nào đó trong bảng tính là "MyRang" thì khi ta đụng đến 1
ô trong vùng đó, sẽ nhận được thông báo:
End Sub
3./ Tô màu nền của vùng được nhập các số ngẫu nhiên
Khi ta chọn vùng từ A7 đến A35, sau đó nhập vô thanh công thức chuỗi:
=INT(19*RAND())+32. Chúng ta kết thúc bằng tổ hợp CTRL+ENTER thì đoạn mã sau
sẽ tô màu nền theo trị trong ô
Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim Rang As Range
Set Rang = Union([A1], [A3], [A5], [A7], [A9], [B1], [B3], [B5],
[B7], [B9], [C1], [C3], [C5], [C7], [C9])
Set Rang = Union(Rang, [E2], [E4], [E6], [E8], [F2], [F4], [F6],
[F8], [G2], [G4], [G6], [G8], [H2])
Code:
Sub CopyRows()
Dim UniRange As Range, Rng As Range
For Each Rng In Selection
With Rng
If .Value = "" And .Offset(0, 1).Value <> "" Then
If UniRange Is Nothing Then
Set UniRange = .EntireRow
Else
Set UniRange = Application.Union(UniRange,
.EntireRow)
End If: End If
End With
Next Rng ' MsgBox UniRange.Address
UniRange.Copy
Destination:=Sheets("S2").Range("A65536").End(xlUp).Offset(1, 0)
Exit Sub: End Sub
5./ Một cách khác để biến các chuỗi nhập vô cột ‘D’ đều viết hoa.
End Sub
6./ Một cách nhập tự động ngày hiện hành vô trường [NgThang] của CSDL
Nếu ta có CSDL mà cột B chứa [MaHg] & cột C chứa ngày nhập, cột F chứa ngày
xuất thì đoạn mã sau sẽ cho phép tự động nhập ngày hiện hành khi ta nhập vô cột
trước nó là mã vật tư, hàng hoá nhập hay xuất.
Private Sub Worksheet_Change(ByVal Target As Range)