Vậy bạn sửa code cho nút Copy như sau:Chào mọi người, tôi có 1 file copy nhiều sheet sang 1 sheet, tôi muốn hỏi là có cách nào khi copy thì copy kèm theo cả tên sheet ko? Chân thành cám ơn
View attachment 167956
Sub Button1_Click()
Dim Sh As Worksheet
Application.ScreenUpdating = False
[a1].CurrentRegion.Offset(1, 0).ClearContents
For Each Sh In Worksheets
If Sh.Name <> "Total" Then
With [B65500].End(xlUp).Offset(1)
Sh.[a1].CurrentRegion.Offset(1, 0).Copy Destination:=.Offset()
End With
[COLOR=#ff0000][B] Range([A65500].End(xlUp).Offset(1), [B65500].End(xlUp).Offset(, -1)) = Sh.Name[/B][/COLOR]
End If
Next Sh
Application.ScreenUpdating = True
End Sub
Bạn ơi, tên sheet thì lấy đc nhưng mà nó copy luôn sheet "TOTAL"Vậy bạn sửa code cho nút Copy như sau:
Mã:Sub Button1_Click() Dim Sh As Worksheet Application.ScreenUpdating = False [a1].CurrentRegion.Offset(1, 0).ClearContents For Each Sh In Worksheets If Sh.Name <> "Total" Then With [B65500].End(xlUp).Offset(1) Sh.[a1].CurrentRegion.Offset(1, 0).Copy Destination:=.Offset() End With [COLOR=#ff0000][B] Range([A65500].End(xlUp).Offset(1), [B65500].End(xlUp).Offset(, -1)) = Sh.Name[/B][/COLOR] End If Next Sh Application.ScreenUpdating = True End Sub
Là do chổ màu đỏ này nè bạn, tên Sheet bạn đặt chữ hoa còn điều kiện bạn thì chữ thường.Bạn ơi, tên sheet thì lấy đc nhưng mà nó copy luôn sheet "TOTAL"
Sub Button1_Click()
Dim Sh As Worksheet
Application.ScreenUpdating = False
[a1].CurrentRegion.Offset(1, 0).ClearContents
For Each Sh In Worksheets
If Sh.Name <> "[COLOR=#ff0000][B]TOTAL[/B][/COLOR]" Then
With [B65500].End(xlUp).Offset(1)
Sh.[a1].CurrentRegion.Offset(1, 0).Copy Destination:=.Offset()
End With
Range([A65500].End(xlUp).Offset(1), [B65500].End(xlUp).Offset(, -1)) = Sh.Name
End If
Next Sh
Application.ScreenUpdating = True
End Sub
Ra lỗi do mình, cảm ơn bạn nhiều!!Là do chổ màu đỏ này nè bạn, tên Sheet bạn đặt chữ hoa còn điều kiện bạn thì chữ thường.
Mã:Sub Button1_Click() Dim Sh As Worksheet Application.ScreenUpdating = False [a1].CurrentRegion.Offset(1, 0).ClearContents For Each Sh In Worksheets If Sh.Name <> "[COLOR=#ff0000][B]TOTAL[/B][/COLOR]" Then With [B65500].End(xlUp).Offset(1) Sh.[a1].CurrentRegion.Offset(1, 0).Copy Destination:=.Offset() End With Range([A65500].End(xlUp).Offset(1), [B65500].End(xlUp).Offset(, -1)) = Sh.Name End If Next Sh Application.ScreenUpdating = True End Sub
Bạn cho mình hỏi thêm vấn đề nữa, có cách nào để copy lần lượt các cột ko? ví dụ như cột A-CLà do chổ màu đỏ này nè bạn, tên Sheet bạn đặt chữ hoa còn điều kiện bạn thì chữ thường.
Mã:Sub Button1_Click() Dim Sh As Worksheet Application.ScreenUpdating = False [a1].CurrentRegion.Offset(1, 0).ClearContents For Each Sh In Worksheets If Sh.Name <> "[COLOR=#ff0000][B]TOTAL[/B][/COLOR]" Then With [B65500].End(xlUp).Offset(1) Sh.[a1].CurrentRegion.Offset(1, 0).Copy Destination:=.Offset() End With Range([A65500].End(xlUp).Offset(1), [B65500].End(xlUp).Offset(, -1)) = Sh.Name End If Next Sh Application.ScreenUpdating = True End Sub
Có chứ, để copy từng cột bạn xem thử cách này.Bạn cho mình hỏi thêm vấn đề nữa, có cách nào để copy lần lượt các cột ko? ví dụ như cột A-C
Sub Button1_Click()
Dim Sh As Worksheet
Application.ScreenUpdating = False
[a1].CurrentRegion.Offset(1, 0).ClearContents
For Each Sh In Worksheets
If Sh.Name <> "TOTAL" Then
With [B65500].End(xlUp).Offset(1)
'Copy cot B o cac sheet sang cot C cua sheet TOTAL
Sh.Range("B2", Sh.[B65500].End(xlUp)).Copy Destination:=.Offset(, 1)
'Copy cot D o cac sheet sang cot E cua sheet TOTAL
Sh.Range("D2", Sh.[D65500].End(xlUp)).Copy Destination:=.Offset(, 3)
End With
Range([A65500].End(xlUp).Offset(1), [C65500].End(xlUp).Offset(, -2)) = Sh.Name
End If
Next Sh
Application.ScreenUpdating = True
End Sub
Có chứ, để copy từng cột bạn xem thử cách này.
Mã:Sub Button1_Click() Dim Sh As Worksheet Application.ScreenUpdating = False [a1].CurrentRegion.Offset(1, 0).ClearContents For Each Sh In Worksheets If Sh.Name <> "TOTAL" Then With [B65500].End(xlUp).Offset(1) 'Copy cot B o cac sheet sang cot C cua sheet TOTAL Sh.Range("B2", Sh.[B65500].End(xlUp)).Copy Destination:=.Offset(, 1) 'Copy cot D o cac sheet sang cot E cua sheet TOTAL Sh.Range("D2", Sh.[D65500].End(xlUp)).Copy Destination:=.Offset(, 3) End With Range([A65500].End(xlUp).Offset(1), [C65500].End(xlUp).Offset(, -2)) = Sh.Name End If Next Sh Application.ScreenUpdating = True End Sub
Xin lỗi lại làm phiền bạn, nhưng mình có chỉnh format của file tí, mình đã sửa lại code nhưng lại bị lỗi, cảm phiền bạn có thể xem qua giúp mình đc ko!!Có chứ, để copy từng cột bạn xem thử cách này.
Mã:Sub Button1_Click() Dim Sh As Worksheet Application.ScreenUpdating = False [a1].CurrentRegion.Offset(1, 0).ClearContents For Each Sh In Worksheets If Sh.Name <> "TOTAL" Then With [B65500].End(xlUp).Offset(1) 'Copy cot B o cac sheet sang cot C cua sheet TOTAL Sh.Range("B2", Sh.[B65500].End(xlUp)).Copy Destination:=.Offset(, 1) 'Copy cot D o cac sheet sang cot E cua sheet TOTAL Sh.Range("D2", Sh.[D65500].End(xlUp)).Copy Destination:=.Offset(, 3) End With Range([A65500].End(xlUp).Offset(1), [C65500].End(xlUp).Offset(, -2)) = Sh.Name End If Next Sh Application.ScreenUpdating = True End Sub
Sub Button1_Click()
Dim Sh As Worksheet
Application.ScreenUpdating = False
[a1].CurrentRegion.Offset(3, 0).ClearContents
For Each Sh In Worksheets
If Sh.Name <> "TOTAL" Then
With [B65500].End(xlUp).Offset(1)
Sh.Range("A4", Sh.[A65500].End(xlUp)).Resize(, 9).Copy Destination:=.Offset()
End With
Range([A65500].End(xlUp).Offset(1), [B65500].End(xlUp).Offset(, -1)) = Sh.Name
End If
Next Sh
Application.ScreenUpdating = True
End Sub
Bạn sửa lại thế này nhé.
Mã:Sub Button1_Click() Dim Sh As Worksheet Application.ScreenUpdating = False [a1].CurrentRegion.Offset(3, 0).ClearContents For Each Sh In Worksheets If Sh.Name <> "TOTAL" Then With [B65500].End(xlUp).Offset(1) Sh.Range("A4", Sh.[A65500].End(xlUp)).Resize(, 9).Copy Destination:=.Offset() End With Range([A65500].End(xlUp).Offset(1), [B65500].End(xlUp).Offset(, -1)) = Sh.Name End If Next Sh Application.ScreenUpdating = True End Sub
mình dựa theo code của bạn mình chỉnh lại đc rồi, cám ơn bạn nhiềuBạn sửa lại thế này nhé.
Mã:Sub Button1_Click() Dim Sh As Worksheet Application.ScreenUpdating = False [a1].CurrentRegion.Offset(3, 0).ClearContents For Each Sh In Worksheets If Sh.Name <> "TOTAL" Then With [B65500].End(xlUp).Offset(1) Sh.Range("A4", Sh.[A65500].End(xlUp)).Resize(, 9).Copy Destination:=.Offset() End With Range([A65500].End(xlUp).Offset(1), [B65500].End(xlUp).Offset(, -1)) = Sh.Name End If Next Sh Application.ScreenUpdating = True End Sub