Skip to content

Instantly share code, notes, and snippets.

@TGDUY
Last active May 2, 2018 07:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TGDUY/56031f0b2f36320efc2ee977e65d33dc to your computer and use it in GitHub Desktop.
Save TGDUY/56031f0b2f36320efc2ee977e65d33dc to your computer and use it in GitHub Desktop.
Sub Resize2Darr()
Dim InputArr As Variant, ColExport As Variant, ColVar As Variant, I As Long 'Khai báo Mảng nguồn, mảng chứa các cột xuất ra, mảng chứa các hàng cần xuất ra
InputArr = Sheet1.Range("A1:G" & Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row).Value 'Lấy mảng nguồn từ bảng tính
ColExport = [{1,3,5,7,6,2}]'Các cột xuất ra theo số thứ tự chúng ta mong muốn
ReDim ColVar(1 To UBound(InputArr, 1), 1 To 1) ' Chỉnh kích thước cho mảng chứa các hàng cần xuất ra , ở đây là toàn bộ số hàng của mảng nguồn
For I = LBound(InputArr, 1) To UBound(InputArr, 1)'cho vòng lặp chạy từ phần tử đầu tiên tới phần tử cuối cùng của mảng nguồn
ColVar(I, 1) = I 'ghi số hàng vào mảng chứa các các hàng cần xuất
Next
Sheet1.Range("K1").Resize(UBound(InputArr, 1), UBound(ColExport, 1)) = Application.Index(InputArr, ColVar, ColExport)' Đưa dữ liệu vào sheet tính từ ô K1
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment