hoanghoa_dtt91
Thành viên chính thức


- Tham gia
- 5/11/13
- Bài viết
- 67
- Được thích
- 0
Giờ mình muốn copy dữ liệu từ một cột Sheet1 (VD A1:A10) sang 2 dòng bên Sheet2 (VD A1:E1 và A2:E2), dùng code VBS giúp mình với. Thak!
Sub t()
' This sub copies a column from a sheet
' divides the data evenly into a number of rows and writes to another sheet
' if the number rows does not divide the number of source cells
' the remainder will be distributed to the first r rows.
' all parameters are given in constants
' modify them to suit your need.
Const SHEETSRC = "Sheet1"
Const SHEETDES = "Sheet2"
Const RANGESRC = "A1:A10" ' the dataset to be copied
Const RANGEDES = "A1" ' first cell of destination
Const NUMROWSDES = 2
Dim rgSrc As Range, rgDes As Range
Set rgSrc = Sheets(SHEETSRC).Range(RANGESRC)
Set rgDes = Sheets(SHEETDES).Range(RANGEDES)
Dim numCols As Integer, numColsRem As Integer
Dim rw As Integer
' this section calculates the number of columns for each row
' if the number does not divide, distribution will favour the first rows
numCols = rgSrc.Rows.Count \ NUMROWSDES
numColsRem = rgSrc.Rows.Count Mod NUMROWSDES
If numColsRem > 0 Then numCols = numCols + 1 ' number does not divide, add extra column
For rw = 0 To NUMROWSDES - 1
rgDes.Offset(rw).Resize(1, numCols) = Application.Transpose(rgSrc.Resize(numCols, 1))
Set rgSrc = rgSrc.Offset(numCols)
If numColsRem > 0 Then
numColsRem = numColsRem - 1 ' subtract 1 from remainder
If numColsRem = 0 Then numCols = numCols - 1 ' remainder has been fully distributed
End If
Next rw
End Sub