Offset trong VBA cũng gần giống vậy!
Cú pháp: Là địa chỉ ô nguồn rồi (.)Offset(hàng, cột). Ví dụ:
Range("A1").Offset(5, 2).Value = ô C6
Range("A1").Offset(, 2).Value = ô C1
Range("A1").Offset(5).Value = ô A6
Vầy chắc bạn đã hiểu rồi chứ!
Thân.
Nếu vùng thì có hai cách!
C1: Bạn lấy range của ô đầu vùng đó rồi dùng Offset như trên.
C2: Bạn Set 1 vùng tạm rồi dùng nó làm 1 mảng lấy dữ liệu. Ví dụ:
PHP:
Sub thu()
Set Vung = range("A1:D10")
k = 1
For i = 1 to 10
For j = 1 to 4
Cells(k,6) = vung.cells(i,j).Value
k = k +1
next
next
End Sub
Cài hàm Vung.Cells(i,j) nghĩa là lấy hàng i cột j ở trong vùng đó. Và code sẽ quét cột trước hàng sau rồi xếp vào 1 cột mới.
Thân.
Sau một hồi suy ngẫn kết quả từ hộp thoại kết xuất, bạn thử thay dần các số khác vô chổ số 0 & số 1 trong mệnh đề Offsẻt(i, j) xem sau ( Kể cả số âm, nhưng là số âm lớn, VD:= -1)
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = 1 Then
Range("B" & Target.Row & ":T" & Target.Row).Select
End If
End Sub
Hàm Offset trong VBA dùng để giời vị trí chọn từ 1 cell gốc đi.
Cấu trúc của hàm Offset là Offset(hàng, cột)
Ví dụ:
Nếu Cell gốc tại A1 và ta muốn chọn ở ô B3 thì ta viết như sau: Range("A1").Offset(2,1)
Nếu Cell gốc tại A1 và ta muốn chọn ở ô D1 thì ta viết như sau: Range("A1").Offset(,3)
Nếu Cell gốc tại A1 và ta muốn chọn ở ô A10 thì ta viết như sau: Range("A1").Offset(9)
Thân.
Đó là ta không thể hiểu hay diễn dịch qua tiếng Việt đây là 1 hàm, mà là 'Thuộc tính'; Sau đây là phần giúp đỡ của VBE một khi ta nhập từ 'OFFSET' trong cửa sổ này & nhấn {F1}:
Offset Property
See Also . . . Applies To . . . . Example
Returns a Range object that represents a range that’s offset from the specified range. Read-only.
expression.Offset(RowOffset, ColumnOffset)
expression Required. An expression that returns a Range object.
RowOffset Optional Variant. The number of rows (positive, negative, or 0 (zero)) by which the range is to be offset. Positive values are offset downward, and negative values are offset upward. The default value is 0.
ColumnOffset Optional Variant. The number of columns (positive, negative, or 0 (zero)) by which the range is to be offset. Positive values are offset to the right, and negative values are offset to the left. The default value is 0.
Offset property as it applies to the TickLabels object.
Returns or sets the distance between the levels of labels, and the distance between the first level and the axis line. The default distance is 100 percent, which represents the default spacing between the axis labels and the axis line. The value can be an integer percentage from 0 through 1000, relative to the axis label’s font size. Read/write Long.
expression.Offset
expression Required. An expression that returns a TickLabels object.
Example
As it applies to the Range object.
This example activates the cell three columns to the right of and three rows down from the active cell on Sheet1.
This example assumes that Sheet1 contains a table that has a header row. The example selects the table, without selecting the header row. The active cell must be somewhere in the table before the example is run.
PHP:
Set tbl = ActiveCell.CurrentRegion
tbl.Offset(1, 0).Resize(tbl.Rows.Count - 1, tbl.Columns.Count).Select