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


- Tham gia
- 3/4/07
- Bài viết
- 71
- Được thích
- 0
Sub CopySang2SheetTheoDieuKien()
Dim Tinh As String
Dim Cls As Range, Rng As Range, Sh As Worksheet
Dim lRw As Long
Tinh = [N5].Value
For Each Cls In Range([E5], [E5].End(xlDown))
If Cls.Value = Tinh Then
Set Sh = ThisWorkbook.Worksheets("Sheet2")
lRw = Sh.[A65500].End(xlUp).Row
Cls.Interior.ColorIndex = 35
Else
Set Sh = ThisWorkbook.Worksheets("Sheet3")
lRw = Sh.[A65500].End(xlUp).Row
Cls.Interior.ColorIndex = 38
End If
Cls.Offset(, -3).Resize(, 3).Copy Destination:=Sh.Cells(lRw + 1, "B")
Sh.Cells(lRw + 1, "A").Value = 1 + Sh.Cells(lRw, "A").Value
Next Cls
End Sub
Cách khácEm chào anh/chị. Em muốn copy các dòng trong excel thỏa điều kiện. Em đính kèm file nhờ a/c giúp em với ạ. Cảm ơn anh chị
Sub ABC()
Dim rng As Range
Dim DK$, Sh$, Sh2$, Sh3$, eRow&,sRow&, i&
Application.ScreenUpdating = False
Sh2 = "Sheet2": Sh3 = "Sheet3"
With Sheets(Sh2)
eRow = .Range("E" & Rows.Count).End(xlUp).Row
If eRow > 7 Then .Range("A8:E" & eRow).Clear
End With
With Sheets(Sh3)
eRow = .Range("E" & Rows.Count).End(xlUp).Row
If eRow > 7 Then .Range("A8:E" & eRow).Clear
End With
With Sheets("Sheet1")
DK = .Range("N5").Value
Set rng = .Range("B5", .Range("E" & Rows.Count).End(xlUp))
End With
sRow = rng.Rows.Count
For i = 1 To rng.Rows.Count
If rng(i, 4).Value = DK Then Sh = Sh2 Else Sh = Sh3
With Sheets(Sh)
eRow = .Range("E" & Rows.Count).End(xlUp).Row
rng(i, 1).Resize(, 4).Copy .Range("B" & eRow + 1)
End With
Next i
Application.ScreenUpdating = True
End Sub
Lập trình mà để cột STT trống rỗng là 1 điều thiếu sót?Em chào anh/chị. Em muốn copy các dòng trong excel thỏa điều kiện. Em đính kèm file nhờ a/c giúp em với ạ. Cảm ơn anh chị
Lập trình mà để cột STT trống rỗng là 1 điều thiếu sót?
Cảm ơn bạn nhiều ạ.Bạn thử với con macro này xem sao:
PHP:Sub CopySang2SheetTheoDieuKien() Dim Tinh As String Dim Cls As Range, Rng As Range, Sh As Worksheet Dim lRw As Long Tinh = [N5].Value For Each Cls In Range([E5], [E5].End(xlDown)) If Cls.Value = Tinh Then Set Sh = ThisWorkbook.Worksheets("Sheet2") lRw = Sh.[A65500].End(xlUp).Row Cls.Interior.ColorIndex = 35 Else Set Sh = ThisWorkbook.Worksheets("Sheet3") lRw = Sh.[A65500].End(xlUp).Row Cls.Interior.ColorIndex = 38 End If Cls.Offset(, -3).Resize(, 3).Copy Destination:=Sh.Cells(lRw + 1, "B") Sh.Cells(lRw + 1, "A").Value = 1 + Sh.Cells(lRw, "A").Value Next Cls End Sub
Cảm ơn bạn Hiếu CD rất nhiều ạ. Bạn hiệu chỉnh code giúp mình chỉ copy đến cột D (không copy đến cột E) sửa như thế nào ạ.Cách khác
Mã:Sub ABC() Dim rng As Range Dim DK$, Sh$, Sh2$, Sh3$, eRow&,sRow&, i& Application.ScreenUpdating = False Sh2 = "Sheet2": Sh3 = "Sheet3" With Sheets(Sh2) eRow = .Range("E" & Rows.Count).End(xlUp).Row If eRow > 7 Then .Range("A8:E" & eRow).Clear End With With Sheets(Sh3) eRow = .Range("E" & Rows.Count).End(xlUp).Row If eRow > 7 Then .Range("A8:E" & eRow).Clear End With With Sheets("Sheet1") DK = .Range("N5").Value Set rng = .Range("B5", .Range("E" & Rows.Count).End(xlUp)) End With sRow = rng.Rows.Count For i = 1 To rng.Rows.Count If rng(i, 4).Value = DK Then Sh = Sh2 Else Sh = Sh3 With Sheets(Sh) eRow = .Range("E" & Rows.Count).End(xlUp).Row rng(i, 1).Resize(, 4).Copy .Range("B" & eRow + 1) End With Next i Application.ScreenUpdating = True End Sub
Vì mình dùng công thức chỗ đó nhảy số thứ tự rồi ạ.Lập trình mà để cột STT trống rỗng là 1 điều thiếu sót?
rng(i, 1).Resize(, 4).Copy .Range("B" & eRow + 1)Cảm ơn bạn nhiều ạ.
Bài đã được tự động gộp:
Cảm ơn bạn Hiếu CD rất nhiều ạ. Bạn hiệu chỉnh code giúp mình chỉ copy đến cột D (không copy đến cột E) sửa như thế nào ạ.
Bài đã được tự động gộp:
Vì mình dùng công thức chỗ đó nhảy số thứ tự rồi ạ.
Bạn xem lại giúp, sao cho ra kết quả copy có 1 dòng à bạnrng(i, 1).Resize(, 4).Copy .Range("B" & eRow + 1)
Số 4 là copy 4 cột từ cột B đến cột E
Chỉnh lại
rng(i, 1).Resize(, 3).Copy .Range("B" & eRow + 1)
Mình quên chỉnh dòng cuốiBạn xem lại giúp, sao cho ra kết quả copy có 1 dòng à bạn
Sub ABC()
Dim rng As Range
Dim DK$, Sh$, Sh2$, Sh3$, eRow&, i&
Application.ScreenUpdating = False
Sh2 = "Sheet2": Sh3 = "Sheet3"
With Sheets(Sh2)
eRow = .Range("E" & Rows.Count).End(xlUp).Row
If eRow > 7 Then .Range("A8:E" & eRow).Clear
End With
With Sheets(Sh3)
eRow = .Range("E" & Rows.Count).End(xlUp).Row
If eRow > 7 Then .Range("A8:E" & eRow).Clear
End With
With Sheets("Sheet1")
DK = .Range("N5").Value
Set rng = .Range("B5", .Range("E" & Rows.Count).End(xlUp))
End With
sRow = rng.Rows.Count
For i = 1 To rng.Rows.Count
If rng(i, 4).Value = DK Then Sh = Sh2 Else Sh = Sh3
With Sheets(Sh)
eRow = .Range("B" & Rows.Count).End(xlUp).Row
rng(i, 1).Resize(, 3).Copy .Range("B" & eRow + 1)
End With
Next i
Application.ScreenUpdating = True
End Sub
Rút gọn code khó hiểu hơnBạn xem lại giúp, sao cho ra kết quả copy có 1 dòng à bạn
Sub ABC()
Dim rng As Range, aSh
Dim DK$, n&, eRow&, i&
Application.ScreenUpdating = False
aSh = Array("Sheet2", 8, "Sheet3", 8)
For i = 0 To 2 Step 2
eRow = Sheets(aSh(i)).Range("E" & Rows.Count).End(xlUp).Row
If eRow > 7 Then Sheets(aSh(i)).Range("A8:E" & eRow).Clear
Next i
With Sheets("Sheet1")
DK = .Range("N5").Value
Set rng = .Range("B5", .Range("E" & Rows.Count).End(xlUp))
End With
sRow = rng.Rows.Count
For i = 1 To rng.Rows.Count
If rng(i, 4).Value = DK Then n = 0 Else n = 2
With Sheets(aSh(n))
rng(i, 1).Resize(, 3).Copy .Range("B" & aSh(n + 1))
aSh(n + 1) = aSh(n + 1) + 1
End With
Next i
Application.ScreenUpdating = True
End Sub
Hy vọng bạn hiểuSub ABCnew()
Dim rng As Range
Dim DK$, Sh$, Sh2$, Sh3$, eRow&, i&
Application.ScreenUpdating = False
Sh2 = "Sheet2": Sh3 = "Sheet3"
With Sheets(Sh2)
eRow = .Range("E" & Rows.Count).End(xlUp).Row
If eRow > 7 Then .Range("A8:E" & eRow).Clear
End With
With Sheets(Sh3)
eRow = .Range("E" & Rows.Count).End(xlUp).Row
If eRow > 7 Then .Range("A8:E" & eRow).Clear
End With
With Sheets("Sheet1")
DK = .Range("N5").Value
Set rng = .Range("B5", .Range("E" & Rows.Count).End(xlUp))
End With
sRow = rng.Rows.Count
For i = 1 To rng.Rows.Count
If rng(i, 4).Value = DK Then Sh = Sh2 Else Sh = Sh3
With Sheets(Sh)
eRow = .Range("B" & Rows.Count).End(xlUp).Row
rng(i, 1).Resize(, 3).copy .Range("B" & eRow + 1)
End With
Next i
Application.ScreenUpdating = True
End Sub
Cảm ơn bạn nhiều lắm. Code chạy rồi nhưng mình chưa hiểu hết các dòng lệnh. Nhờ bạn chú thích giúp ạ.
Sub ABC()
Dim rng As Range
Dim DK$, Sh$, Sh2$, Sh3$, eRow&, sRow&, i&
Application.ScreenUpdating = False
Sh2 = "Sheet2": Sh3 = "Sheet3"
With Sheets(Sh2)
eRow = .Range("B" & Rows.Count).End(xlUp).Row 'Dòng cuoi cot B
If eRow > 7 Then .Range("A8:D" & eRow).Clear 'Xoa Tu dong 8 toi dong cuoi tu cot A toi cot D
End With
With Sheets(Sh3)
eRow = .Range("B" & Rows.Count).End(xlUp).Row
If eRow > 7 Then .Range("A8:D" & eRow).Clear
End With
With Sheets("Sheet1")
DK = .Range("N5").Value 'Dieu kien
eRow = .Range("E" & Rows.Count).End(xlUp).Row 'Dòng cuoi cot E
Set rng = .Range("B5:E" & eRow) 'Vung du lieu "rng" tu dong 5 toi dong cuoi, tu cot B toi cot E
End With
sRow = rng.Rows.Count 'So dong Vung du lieu "rng"
For i = 1 To sRow
If rng(i, 4).Value = DK Then Sh = Sh2 Else Sh = Sh3
With Sheets(Sh) 'Sheet ket qua
eRow = .Range("B" & Rows.Count).End(xlUp).Row 'Dòng cuoi cot B
rng(i, 1).Resize(, 3).Copy .Range("B" & eRow + 1) ' copy dong i, 3 cot tu cot B (B,C,D)
End With
Next i
Application.ScreenUpdating = True
End Sub
Không rỏ ý bạn như thế nàoBạn giúp mình thêm tý:Nếu copy qua sheet3 bỏ 1 cột thì sửa sao ạ. ví dụ muốn copy sheet1 từ cột B đến D qua sheet3 cột B đến C và cột D bên sheet1 thì copy qua cột E bên sheet3 ạ.
Sub ABC()
Dim rng As Range
Dim DK$, Sh$, Sh2$, Sh3$, eRow&, sRow&, i&
Application.ScreenUpdating = False
Sh2 = "Sheet2": Sh3 = "Sheet3"
With Sheets(Sh2)
eRow = .Range("B" & Rows.Count).End(xlUp).Row 'Dòng cuoi cot B
If eRow > 7 Then .Range("A8:D" & eRow).Clear 'Xoa Tu dong 8 toi dong cuoi tu cot A toi cot D
End With
With Sheets(Sh3)
eRow = .Range("B" & Rows.Count).End(xlUp).Row
If eRow > 7 Then .Range("A8:E" & eRow).Clear
End With
With Sheets("Sheet1")
DK = .Range("N5").Value 'Dieu kien
eRow = .Range("E" & Rows.Count).End(xlUp).Row 'Dòng cuoi cot E
Set rng = .Range("B5:E" & eRow) 'Vung du lieu "rng" tu dong 5 toi dong cuoi, tu cot B toi cot E
End With
sRow = rng.Rows.Count 'So dong Vung du lieu "rng"
For i = 1 To sRow
If rng(i, 4).Value = DK Then
With Sheets(Sh2) 'Sheet2
eRow = .Range("B" & Rows.Count).End(xlUp).Row 'Dòng cuoi cot B
rng(i, 1).Resize(, 3).Copy .Range("B" & eRow + 1) ' copy dong i, 3 cot tu cot B (B,C,D)
End With
Else
With Sheets(Sh3) 'Sheet3
eRow = .Range("B" & Rows.Count).End(xlUp).Row 'Dòng cuoi cot B
rng(i, 1).Resize(, 4).Copy .Range("B" & eRow + 1) ' copy dong i, 4 cot tu cot B (B,C,D,E)
End With
End If
Next i
Application.ScreenUpdating = True
End Sub
Mình đã làmđược rồi bạn Hiếu ơi.Nhờ bạn chú thích á. Nhưng nó chạy chậm lắm bạn, đợi tầm mấy phút luôn.Bạn giúp mình thêm tý:Nếu copy qua sheet3 bỏ 1 cột thì sửa sao ạ. ví dụ muốn copy sheet1 từ cột B đến D qua sheet3 cột B đến C và cột D bên sheet1 thì copy qua cột E bên sheet3 ạ.
rng(i, 1).Resize(, 4).Copy .Range("B" & eRow + 1) ' copy dong i, 4 cot tu cot B (B,C,D,E)Không rỏ ý bạn như thế nào
Mã:Sub ABC() Dim rng As Range Dim DK$, Sh$, Sh2$, Sh3$, eRow&, sRow&, i& Application.ScreenUpdating = False Sh2 = "Sheet2": Sh3 = "Sheet3" With Sheets(Sh2) eRow = .Range("B" & Rows.Count).End(xlUp).Row 'Dòng cuoi cot B If eRow > 7 Then .Range("A8:D" & eRow).Clear 'Xoa Tu dong 8 toi dong cuoi tu cot A toi cot D End With With Sheets(Sh3) eRow = .Range("B" & Rows.Count).End(xlUp).Row If eRow > 7 Then .Range("A8:E" & eRow).Clear End With With Sheets("Sheet1") DK = .Range("N5").Value 'Dieu kien eRow = .Range("E" & Rows.Count).End(xlUp).Row 'Dòng cuoi cot E Set rng = .Range("B5:E" & eRow) 'Vung du lieu "rng" tu dong 5 toi dong cuoi, tu cot B toi cot E End With sRow = rng.Rows.Count 'So dong Vung du lieu "rng" For i = 1 To sRow If rng(i, 4).Value = DK Then With Sheets(Sh2) 'Sheet2 eRow = .Range("B" & Rows.Count).End(xlUp).Row 'Dòng cuoi cot B rng(i, 1).Resize(, 3).Copy .Range("B" & eRow + 1) ' copy dong i, 3 cot tu cot B (B,C,D) End With Else With Sheets(Sh3) 'Sheet3 eRow = .Range("B" & Rows.Count).End(xlUp).Row 'Dòng cuoi cot B rng(i, 1).Resize(, 4).Copy .Range("B" & eRow + 1) ' copy dong i, 4 cot tu cot B (B,C,D,E) End With End If Next i Application.ScreenUpdating = True End Sub
Bạn xem sheet ạ.Không rỏ ý bạn như thế nào
Mã:Sub ABC() Dim rng As Range Dim DK$, Sh$, Sh2$, Sh3$, eRow&, sRow&, i& Application.ScreenUpdating = False Sh2 = "Sheet2": Sh3 = "Sheet3" With Sheets(Sh2) eRow = .Range("B" & Rows.Count).End(xlUp).Row 'Dòng cuoi cot B If eRow > 7 Then .Range("A8:D" & eRow).Clear 'Xoa Tu dong 8 toi dong cuoi tu cot A toi cot D End With With Sheets(Sh3) eRow = .Range("B" & Rows.Count).End(xlUp).Row If eRow > 7 Then .Range("A8:E" & eRow).Clear End With With Sheets("Sheet1") DK = .Range("N5").Value 'Dieu kien eRow = .Range("E" & Rows.Count).End(xlUp).Row 'Dòng cuoi cot E Set rng = .Range("B5:E" & eRow) 'Vung du lieu "rng" tu dong 5 toi dong cuoi, tu cot B toi cot E End With sRow = rng.Rows.Count 'So dong Vung du lieu "rng" For i = 1 To sRow If rng(i, 4).Value = DK Then With Sheets(Sh2) 'Sheet2 eRow = .Range("B" & Rows.Count).End(xlUp).Row 'Dòng cuoi cot B rng(i, 1).Resize(, 3).Copy .Range("B" & eRow + 1) ' copy dong i, 3 cot tu cot B (B,C,D) End With Else With Sheets(Sh3) 'Sheet3 eRow = .Range("B" & Rows.Count).End(xlUp).Row 'Dòng cuoi cot B rng(i, 1).Resize(, 4).Copy .Range("B" & eRow + 1) ' copy dong i, 4 cot tu cot B (B,C,D,E) End With End If Next i Application.ScreenUpdating = True End Sub
Sub COPYROW()
Dim rng As Range
Dim DK$, Sh$, Sh2$, Sh3$, eRow&, sRow&, i&
Application.ScreenUpdating = False
Sh2 = "Sheet2": Sh3 = "Sheet3"
With Sheets(Sh2)
eRow = .Range("B" & Rows.Count).End(xlUp).Row 'Dòng cuoi cot B
If eRow > 12 Then .Range("B13:H" & eRow).Clear 'Xoa Tu dong 13 toi dong cuoi tu cot A toi cot D
End With
With Sheets(Sh3)
eRow = .Range("B" & Rows.Count).End(xlUp).Row
If eRow > 12 Then .Range("B13:F" & eRow).Clear
'.Range("I13:I" & eRow).Clear ' Xoa cot I
End With
With Sheets("Sheet1")
DK = .Range("AA5").Value 'Dieu kien
eRow = .Range("H" & Rows.Count).End(xlUp).Row 'Dòng cuoi cot E
Set rng = .Range("B4:H" & eRow) 'Vung du lieu "rng" tu dong 5 toi dong cuoi, tu cot B toi cot E
End With
sRow = rng.Rows.Count 'So dong Vung du lieu "rng"
For i = 1 To sRow
If rng(i, 7).Value <> DK Then
With Sheets(Sh2) 'Sheet2
eRow = .Range("B" & Rows.Count).End(xlUp).Row 'Dòng cuoi cot B
rng(i, 1).Resize(, 6).copy .Range("B" & eRow + 1) ' copy dong i, 3 cot tu cot B (B,C,D)
End With
Else
With Sheets(Sh3) 'Sheet3
eRow = .Range("B" & Rows.Count).End(xlUp).Row 'Dòng cuoi cot B
rng(i, 1).Resize(, 5).copy .Range("B" & eRow + 1) ' copy dong i, 4 cot tu cot B (B,C,D,E)
rng(i, 6).Resize(, 1).copy .Range("I" & eRow + 1) ' copy dong i, 4 cot tu cot B (B,C,D,E)
End With
End If
Next i
Application.ScreenUpdating = True
End Sub
Chạy code mớiCảm ơn bạn Hiếu CD, mình đã làm được rồi ạ. Nhưng bạn có thể cải tiến để chạy nhanh hơn được không ạ.Mã:Sub COPYROW() Dim rng As Range Dim DK$, Sh$, Sh2$, Sh3$, eRow&, sRow&, i& Application.ScreenUpdating = False Sh2 = "Sheet2": Sh3 = "Sheet3" With Sheets(Sh2) eRow = .Range("B" & Rows.Count).End(xlUp).Row 'Dòng cuoi cot B If eRow > 12 Then .Range("B13:H" & eRow).Clear 'Xoa Tu dong 13 toi dong cuoi tu cot A toi cot D End With With Sheets(Sh3) eRow = .Range("B" & Rows.Count).End(xlUp).Row If eRow > 12 Then .Range("B13:F" & eRow).Clear '.Range("I13:I" & eRow).Clear ' Xoa cot I End With With Sheets("Sheet1") DK = .Range("AA5").Value 'Dieu kien eRow = .Range("H" & Rows.Count).End(xlUp).Row 'Dòng cuoi cot E Set rng = .Range("B4:H" & eRow) 'Vung du lieu "rng" tu dong 5 toi dong cuoi, tu cot B toi cot E End With sRow = rng.Rows.Count 'So dong Vung du lieu "rng" For i = 1 To sRow If rng(i, 7).Value <> DK Then With Sheets(Sh2) 'Sheet2 eRow = .Range("B" & Rows.Count).End(xlUp).Row 'Dòng cuoi cot B rng(i, 1).Resize(, 6).copy .Range("B" & eRow + 1) ' copy dong i, 3 cot tu cot B (B,C,D) End With Else With Sheets(Sh3) 'Sheet3 eRow = .Range("B" & Rows.Count).End(xlUp).Row 'Dòng cuoi cot B rng(i, 1).Resize(, 5).copy .Range("B" & eRow + 1) ' copy dong i, 4 cot tu cot B (B,C,D,E) rng(i, 6).Resize(, 1).copy .Range("I" & eRow + 1) ' copy dong i, 4 cot tu cot B (B,C,D,E) End With End If Next i Application.ScreenUpdating = True End Sub
Sub ABC()
Dim DK$, Sh2$, Sh3$, eRow&
Application.ScreenUpdating = False
Sh2 = "Sheet2": Sh3 = "Sheet3"
With Sheets(Sh2)
eRow = .Range("B" & Rows.Count).End(xlUp).Row 'Dòng cuoi cot B
If eRow > 7 Then .Range("A8:E" & eRow).Clear 'Xoa Tu dong 8 toi dong cuoi tu cot A toi cot D
End With
With Sheets(Sh3)
eRow = .Range("B" & Rows.Count).End(xlUp).Row
If eRow > 7 Then .Range("A8:E" & eRow).Clear
End With
With Sheets("Sheet1")
DK = .Range("N5").Value 'Dieu kien
eRow = .Range("E" & Rows.Count).End(xlUp).Row 'Dòng cuoi cot E
.Range("B4").AutoFilter Field:=5, Criteria1:=DK 'Loc theo dieu kien
.Range("B5:E" & eRow).Copy Sheets(Sh2).Range("B8") 'Copy
.Range("B4").AutoFilter Field:=5, Criteria1:="<>" & DK 'Loc theo khác dieu kien
.Range("B5:C" & eRow).Copy Sheets(Sh3).Range("B8") 'Copy
.Range("D5:D" & eRow).Copy Sheets(Sh3).Range("E8") 'Copy
.Range("B4").AutoFilter
End With
Application.ScreenUpdating = True
End Sub
Chạy code mới
Mã:Sub ABC() Dim DK$, Sh2$, Sh3$, eRow& Application.ScreenUpdating = False Sh2 = "Sheet2": Sh3 = "Sheet3" With Sheets(Sh2) eRow = .Range("B" & Rows.Count).End(xlUp).Row 'Dòng cuoi cot B If eRow > 7 Then .Range("A8:E" & eRow).Clear 'Xoa Tu dong 8 toi dong cuoi tu cot A toi cot D End With With Sheets(Sh3) eRow = .Range("B" & Rows.Count).End(xlUp).Row If eRow > 7 Then .Range("A8:E" & eRow).Clear End With With Sheets("Sheet1") DK = .Range("N5").Value 'Dieu kien eRow = .Range("E" & Rows.Count).End(xlUp).Row 'Dòng cuoi cot E .Range("B4").AutoFilter Field:=5, Criteria1:=DK 'Loc theo dieu kien .Range("B5:E" & eRow).Copy Sheets(Sh2).Range("B8") 'Copy .Range("B4").AutoFilter Field:=5, Criteria1:="<>" & DK 'Loc theo khác dieu kien .Range("B5:C" & eRow).Copy Sheets(Sh3).Range("B8") 'Copy .Range("D5:D" & eRow).Copy Sheets(Sh3).Range("E8") 'Copy .Range("B4").AutoFilter End With Application.ScreenUpdating = True End Sub
For i = 1 To sRow
If rng(i, 7).Value <> DK Then
With Sheets(Sh2) 'Sheet2
eRow = .Range("B" & Rows.Count).End(xlUp).Row 'Dòng cuoi cot B
rng(i, 1).Resize(, 6).copy .Range("B" & eRow + 1) ' copy dong i, 6 cot tu cot B (B,C,D,E,F,G)
End With
Else
With Sheets(Sh3) 'Sheet3
eRow = .Range("B" & Rows.Count).End(xlUp).Row 'Dòng cuoi cot B
rng(i, 1).Resize(, 5).copy .Range("B" & eRow + 1) ' copy dong i, 5 cot tu cot B (B,C,D,E,F)
rng(i, 6).Resize(, 1).copy .Range("I" & eRow + 1) ' copy dong i, 1 cot tu G
End With
End If
Next i
Thử code sau:Mình đã làmđược rồi bạn Hiếu ơi.Nhờ bạn chú thích á. Nhưng nó chạy chậm lắm bạn, đợi tầm mấy phút luôn.
rng(i, 1).Resize(, 4).Copy .Range("B" & eRow + 1) ' copy dong i, 4 cot tu cot B (B,C,D,E)
Code này mình muốn copy 2 cột từ cột B(B C) còn cột D thì copy đến cột E ạ
Bài đã được tự động gộp:
Bạn xem sheet ạ.
Sub Copy_DieuKien()
Dim DongCuoi As Long
Dim DieuKien As String
Sheet2.Range("B7").CurrentRegion.Offset(1).Clear
Sheet3.Range("B7").CurrentRegion.Offset(1).Clear
With Sheet1
DongCuoi = .Cells(Rows.Count, 1).End(xlUp).Row
DieuKien = .Range("N6").Value
.Range("A4").AutoFilter Field:=5, Criteria1:=DieuKien
.Range("B5:D" & DongCuoi).Copy Sheet2.Range("B8")
.Range("A4").AutoFilter Field:=5, Criteria1:="<>" & DieuKien
.Range("B5:C" & DongCuoi).Copy Sheet3.Range("B8")
.Range("E5:E" & DongCuoi).Copy Sheet3.Range("D8")
.Range("D5:D" & DongCuoi).Copy Sheet3.Range("E8")
.Range("B4").AutoFilter
End With
End Sub