copy dữ liệu từ sheet sang sheets (1 người xem)

Liên hệ QC

Người dùng đang xem chủ đề này

Tôi tuân thủ nội quy khi đăng bài

tuancuongk55

Thành viên mới
Tham gia
15/3/23
Bài viết
2
Được thích
0
Giới tính
Nam
chào mọi người. mong mọi người giải đáp thắc mắc:
mình có sheet Run chứa danh sách sheet name để tạo ra các sheet
sau khi tạo ra các sheet thì copy dữ liệu từ sheet Import_SF sang từ sheet với các vùng dữ liệu thay đổi
1. với item1 là mảng, dùng để lấy vùng chứa dữ liệu từ item1(i) đến item1(i+1).
key là mảng chứa tên của sheets.
=> mình có dòng lệnh dưới đây
" For i = LBound(item1) To UBound(item1)
Sheets("import_SF").Activate
lc = ActiveSheet.Cells(f_row, Columns.Count).End(xlToLeft).Column
ActiveSheet.Range(Cells(item1(i), 1), Cells(item1(i + 1), lc)).Copy _
Destination:=ThisWorkbook.Sheets(key(i)).Range("A1")

Next i
Câu hỏi:
1.
Khi chạy thì câu lệnh được bôi đỏ báo lỗi. Tại sao vậy?
2. Có phải trong phần sheets(key(i)) {key(i): ở đây là tên của sheets} thì tên của sheets không được là biến?

Trân thành cảm ơn!
 

File đính kèm

chào mọi người. mong mọi người giải đáp thắc mắc:
Sửa thế này hết bị lỗi.
Mã:
Sub copy_data()
    Dim key, item1 As Variant
    Dim lastrow, lr As Long
    Dim ws As Worksheet
    Dim i As Integer
    Dim f_column As Integer: f_column = 6
    Dim f_row As Integer: f_row = 20
    Set ws = Sheets("Run")
    lastrow = ws.Cells(ws.Rows.Count, f_column - 1).End(xlUp).Row
    key = Application.WorksheetFunction.Transpose(ws.Range(Cells(4, f_column), Cells(lastrow, f_column)))
    item1 = Application.WorksheetFunction.Transpose(ws.Range(Cells(4, f_column - 1), Cells(lastrow, f_column - 1)))
    For i = LBound(item1) To UBound(item1) - 1
        Sheets("import_SF").Activate
        lc = ActiveSheet.Cells(f_row, Columns.Count).End(xlToLeft).Column
        ActiveSheet.Range(Cells(item1(i), 1), Cells(item1(i + 1), lc)).Copy _
                Destination:=ThisWorkbook.Sheets(key(i)).Range("A1")
    Next i
End Sub
 
Upvote 0
Sửa thế này hết bị lỗi.
Mã:
Sub copy_data()
    Dim key, item1 As Variant
    Dim lastrow, lr As Long
    Dim ws As Worksheet
    Dim i As Integer
    Dim f_column As Integer: f_column = 6
    Dim f_row As Integer: f_row = 20
    Set ws = Sheets("Run")
    lastrow = ws.Cells(ws.Rows.Count, f_column - 1).End(xlUp).Row
    key = Application.WorksheetFunction.Transpose(ws.Range(Cells(4, f_column), Cells(lastrow, f_column)))
    item1 = Application.WorksheetFunction.Transpose(ws.Range(Cells(4, f_column - 1), Cells(lastrow, f_column - 1)))
    For i = LBound(item1) To UBound(item1) - 1
        Sheets("import_SF").Activate
        lc = ActiveSheet.Cells(f_row, Columns.Count).End(xlToLeft).Column
        ActiveSheet.Range(Cells(item1(i), 1), Cells(item1(i + 1), lc)).Copy _
                Destination:=ThisWorkbook.Sheets(key(i)).Range("A1")
    Next i
End Sub
cảm ơn bạn nhiều !
 
Upvote 0
Web KT

Bài viết mới nhất

Back
Top Bottom