Tự động copy 1 khối cell sau khi chọn 1 cell đã quy định (1 người xem)

Liên hệ QC

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

hoanglocphat

Thành viên thường trực
Tham gia
27/1/13
Bài viết
258
Được thích
30
Chào các bạn,
Do tính chất công việc thường xuyên lặp lại, nay nhờ các bạn viết code như sau:
Tôi có 2 khối ô mà thường xuyên copy như sau:
1/ K10:P10
2/ H16:M16
Nay tôi muốn khi chọn bất cứ 1 ô nào trong K10:P10 thì nó tự động copy K10:P10 hoặc chọn 1 ô trong H16:M16 thì nó tự động copy khối ô H16:M16
Sau khi copy thì tôi paste bất kỳ vào 1 khối cell nào đó
Và nếu được thì tôi muốn là paste value ở khối cell mà tôi muốn paste
Cảm ơn các bạn.
 
Chào các bạn,
Do tính chất công việc thường xuyên lặp lại, nay nhờ các bạn viết code như sau:
Tôi có 2 khối ô mà thường xuyên copy như sau:
1/ K10:p10
2/ H16:M16
Nay tôi muốn khi chọn bất cứ 1 ô nào trong K10:p10 thì nó tự động copy K10:p10 hoặc chọn 1 ô trong H16:M16 thì nó tự động copy khối ô H16:M16
Sau khi copy thì tôi paste bất kỳ vào 1 khối cell nào đó
Và nếu được thì tôi muốn là paste value ở khối cell mà tôi muốn paste
Cảm ơn các bạn.
Bạn dùng code này cho Sheet chứa dữ liệu cần copy nhé!
Mã:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Cls As Range
    If Not Intersect(Target, Range("K10:P10")) Is Nothing Then
        Set Cls = Application.InputBox("Chon o de Paste", "GPE", Type:=8)
        Range("K10:P10").Copy Cls
    ElseIf Not Intersect(Target, Range("H16:M16")) Is Nothing Then
        Set Cls = Application.InputBox("Chon o de Paste", "GPE", Type:=8)
        Range("H16:M16").Copy Cls
    End If
End Sub
 
Upvote 0
Bạn dùng code này cho Sheet chứa dữ liệu cần copy nhé!
Mã:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Cls As Range
    If Not Intersect(Target, Range("K10:P10")) Is Nothing Then
        Set Cls = Application.InputBox("Chon o de Paste", "GPE", Type:=8)
        Range("K10:P10").Copy Cls
    ElseIf Not Intersect(Target, Range("H16:M16")) Is Nothing Then
        Set Cls = Application.InputBox("Chon o de Paste", "GPE", Type:=8)
        Range("H16:M16").Copy Cls
    End If
End Sub
Cảm ơn bạn, mình muốn Paste value được không bạn!
 
Upvote 0
Cảm ơn bạn, mình muốn Paste value được không bạn!
Sửa thế này:
PHP:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Cls As Range
    If Not Intersect(Target, Range("K10:P10")) Is Nothing Then
        Set Cls = Application.InputBox("Chon o de Paste", "GPE", Type:=8)
        Range("K10:P10").Copy
        Cls.PasteSpecial Paste:=xlValues
    ElseIf Not Intersect(Target, Range("H16:M16")) Is Nothing Then
        Set Cls = Application.InputBox("Chon o de Paste", "GPE", Type:=8)
        Range("H16:M16").Copy
        Cls.PasteSpecial Paste:=xlValues
    End If
End Sub
 
Upvote 0
Sửa thế này:
PHP:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Cls As Range
    If Not Intersect(Target, Range("K10:P10")) Is Nothing Then
        Set Cls = Application.InputBox("Chon o de Paste", "GPE", Type:=8)
        Range("K10:P10").Copy
        Cls.PasteSpecial Paste:=xlValues
    ElseIf Not Intersect(Target, Range("H16:M16")) Is Nothing Then
        Set Cls = Application.InputBox("Chon o de Paste", "GPE", Type:=8)
        Range("H16:M16").Copy
        Cls.PasteSpecial Paste:=xlValues
    End If
End Sub
Bạn cho hỏi thêm
Khi hiện bảng "chon o de paste" nếu chọn Cancel thì nó báo lỗi "run time error '424' object required"
Vậy có cách nào để khắc phục lỗi này được không bạn
Cảm ơn các bạn!
 
Upvote 0
Bạn cho hỏi thêm
Khi hiện bảng "chon o de paste" nếu chọn Cancel thì nó báo lỗi "run time error '424' object required"
Vậy có cách nào để khắc phục lỗi này được không bạn
Cảm ơn các bạn!
Bạn thử:
PHP:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Cls
    If Not Intersect(Target, Range("K10:P10")) Is Nothing Then
        On Error GoTo Thoat
        Set Cls = Application.InputBox("Chon o de Paste", "GPE", Type:=8)
        Range("K10:P10").Copy
        Cls.PasteSpecial Paste:=xlValues
Thoat:
    ElseIf Not Intersect(Target, Range("H16:M16")) Is Nothing Then
        On Error GoTo abc
        Set Cls = Application.InputBox("Chon o de Paste", "GPE", Type:=8)
        If Cls = "" Then Exit Sub
        Range("H16:M16").Copy
        Cls.PasteSpecial Paste:=xlValues
abc:
    End If
End Sub
 
Upvote 0
Bạn thử:
PHP:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Cls
    If Not Intersect(Target, Range("K10:P10")) Is Nothing Then
        On Error GoTo Thoat
        Set Cls = Application.InputBox("Chon o de Paste", "GPE", Type:=8)
        Range("K10:P10").Copy
        Cls.PasteSpecial Paste:=xlValues
Thoat:
    ElseIf Not Intersect(Target, Range("H16:M16")) Is Nothing Then
        On Error GoTo abc
        Set Cls = Application.InputBox("Chon o de Paste", "GPE", Type:=8)
        If Cls = "" Then Exit Sub
        Range("H16:M16").Copy
        Cls.PasteSpecial Paste:=xlValues
abc:
    End If
End Sub
Em chạy thử thì thấy đúng với dòng 10 nhưng lại không copy được với dòng 16
Em sửa lại thế này thì được
Mã:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Cls As Range
    On Error Resume Next
    If Not Intersect(Target, Range("K10:P10")) Is Nothing Then
        Set Cls = Application.InputBox("Chon o de Paste", "GPE", Type:=8)
        If Err.Number = 424 Then
            Exit Sub
        Else
            Range("K10:P10").Copy
            Cls.PasteSpecial Paste:=xlValues
        End If
    ElseIf Not Intersect(Target, Range("H16:M16")) Is Nothing Then
        Set Cls = Application.InputBox("Chon o de Paste", "GPE", Type:=8)
        If Err.Number = 424 Then
            Exit Sub
        Else
            Range("H16:M16").Copy
            Cls.PasteSpecial Paste:=xlValues
        End If
    End If

End Sub
 
Upvote 0
Bạn thử:
PHP:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Cls
    If Not Intersect(Target, Range("K10:P10")) Is Nothing Then
        On Error GoTo Thoat
        Set Cls = Application.InputBox("Chon o de Paste", "GPE", Type:=8)
        Range("K10:P10").Copy
        Cls.PasteSpecial Paste:=xlValues
Thoat:
    ElseIf Not Intersect(Target, Range("H16:M16")) Is Nothing Then
        On Error GoTo abc
        Set Cls = Application.InputBox("Chon o de Paste", "GPE", Type:=8)
        If Cls = "" Then Exit Sub
        Range("H16:M16").Copy
        Cls.PasteSpecial Paste:=xlValues
abc:
    End If
End Sub
1 cách nữa anh ạ.
Mã:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Cls As Range, MyCol As New Collection
    
    If Not Intersect(Target, Range("K10:P10")) Is Nothing Then
        MyCol.Add Application.InputBox("Chon o de Paste", "GPE", Type:=8)
        If TypeOf MyCol(1) Is Range Then Set Cls = MyCol(1)
        
        If Cls Is Nothing Then
            Exit Sub
        Else
            Range("K10:P10").Copy
            Cls.PasteSpecial Paste:=xlValues
        End If
    ElseIf Not Intersect(Target, Range("H16:M16")) Is Nothing Then
        MyCol.Add Application.InputBox("Chon o de Paste", "GPE", Type:=8)
        If TypeOf MyCol(1) Is Range Then Set Cls = MyCol(1)
        
        If Cls Is Nothing Then
            Exit Sub
        Else
            Range("H16:M16").Copy
            Cls.PasteSpecial Paste:=xlValues
        End If
    End If
End Sub
 
Upvote 0
Tự động chọn vùng là ý tưởng điên rồ.
Ví dụ tôi thực sự chỉ muốn 1 ô trong vùng thì làm cách nào bây giờ?
Tự động copy thì lại càng vô duyên hơn.
Cứ mỗi lần tôi đi ngang vùng thì tôi lại phải trả lời 1 khối messages.

Nếu thực sự muốn làm thì phải có một tham số điều khiển. Ví dụ có một ô nào đó (A1 chẳng hạn) chứa địa chỉ vùng ảnh hưởng. Tôi có thể dùng ô này để điều chỉnh ý mình. Nếu không muốn dùng ô thì dùng một name.
 
Upvote 0
Sao thấy code lằng nhằng quá nhỉ
Tôi viết vầy:
Mã:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Dim rngCopy As Object, rngPaste As Object
  If Not Intersect(Target, Range("K10:P10")) Is Nothing Then
    Set rngCopy = Range("K10:P10")
  ElseIf Not Intersect(Target, Range("H16:M16")) Is Nothing Then
    Set rngCopy = Range("H16:M16")
  End If
  If TypeName(rngCopy) = "Range" Then
    On Error Resume Next
    Set rngPaste = Application.InputBox("Chon vùng Paste", Type:=8)
    On Error GoTo 0
    If TypeName(rngPaste) = "Range" Then rngPaste.Resize(rngCopy.Rows.Count, rngCopy.Columns.Count).Value = rngCopy.Value
  End If
End Sub
 
Upvote 0
Web KT

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

Back
Top Bottom