Tạo sheet mới theo điều kiện? (1 người xem)

Liên hệ QC

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

Hoàng Nhật Phương

Thành viên gắn bó
Tham gia
5/11/15
Bài viết
1,895
Được thích
1,219
Xin chào các bạn
Có 1 vấn đề như sau, rất mong nhận được sự giúp đỡ của các bạn ạ:

Tôi có một tập tin, trong tập tin này có 2 sheet có tên "Data" và "Form"
Trong sheet Data tại vùng C2:C11 tôi nhập dữ liệu lần lượt "a,b,c,...,j"

Mong muốn làm sao đặt con trỏ chuột vào ô nào nếu ô đó có dữ liệu thì sẽ copy từ sheet "Form" có tên ô đó.

Ví dụ khi đặt con trỏ chuột vào (lựa chọn) các ô:
C4 có dữ liệu là c
C7 có dữ liệu là f
C4 có dữ liệu là j

Sau đó chạy code thì tạo ra 3 sheet có tên lần lượt là: c,f,j giống như sheet "Form"
 

File đính kèm

Upvote 0
Mã:
Public Sub GPE()
Dim Cll As Range
If TypeOf Selection Is Range Then
    For Each Cll In Selection
        Sheets("Form").Copy After:=Sheets(Sheets.Count)
        Sheets(Sheets.Count).Name = Cll.Value
    Next
End If
End Sub

Cảm ơn hpkhuong & anh huuthang_bd đã quan tâm.
Nếu trong trường hợp chọn 2 ô có dữ liệu và 1 ô rỗng (không có dữ liệu) thì code lỗi tạo được 3 sheet nhưng không đặt hết tên được cho cả 3 sheet vì có 1 ô rỗng.

Nhờ các bạn thêm giúp trường hợp loại bỏ lỗi này với ạ. :)
 
Upvote 0
Cảm ơn hpkhuong & anh huuthang_bd đã quan tâm.
Nếu trong trường hợp chọn 2 ô có dữ liệu và 1 ô rỗng (không có dữ liệu) thì code lỗi tạo được 3 sheet nhưng không đặt hết tên được cho cả 3 sheet vì có 1 ô rỗng.

Nhờ các bạn thêm giúp trường hợp loại bỏ lỗi này với ạ. :)
Chú ý vùng chọn trước khi chạy code nhé, chọn cả sheet Data là "điếc luôn".
PHP:
Public Sub GPE()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim Cll As Range, Ws As Worksheet, Myname As String
For Each Cll In Selection
    If Cll.Value <> Empty Then
        Myname = Cll.Value
        For Each Ws In Worksheets
            If Ws.Name = Myname Then Ws.Delete
        Next Ws
        Sheets("Form").Copy After:=Sheets(Sheets.Count)
        Sheets(Sheets.Count).Name = Myname
    End If
Next Cll
Sheets("Data").Select
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
 
Upvote 0
PHP:
Sub InsertSheets()
    Dim dicSheets As Scripting.Dictionary, dicObjSheet As Scripting.Dictionary
    Dim wsSource As Worksheet
    Dim rngSource As Range
    Dim objTemp As Object
    Dim rngTemp As Range
    Dim lngNumSheetBefor As Long, aSheets, lngIndex As Long
    Dim wsBefor As Object
    
    If Selection Is Nothing Then
        MsgBox "Khong tim thay vung danh sach"
    Else
        If TypeOf Selection Is Range Then
            Set rngSource = Selection
            
        Else
            MsgBox "Phai chon mot vung truoc khi chay macro"
            Exit Sub
        End If
    End If
    
    On Error Resume Next
    Set wsSource = ThisWorkbook.Sheets("Form")
    wsSource.Visible = xlSheetVisible
    If Err.Number <> 0 Then
        MsgBox "Khong tim thay sheet  nguon"
        Exit Sub
    End If
    
    On Error GoTo 0
    Set dicSheets = New Scripting.Dictionary 'danh sach ten cac sheet dang co và se duoc them
    Set dicObjSheet = New Scripting.Dictionary 'danh sách cac shet da co hien tai
    dicSheets.CompareMode = TextCompare
    
    For Each objTemp In ThisWorkbook.Sheets
        dicSheets.Add objTemp.Name, objTemp
        dicObjSheet.Add objTemp, ""
    Next
    lngNumSheetBefor = dicSheets.Count
    
    For Each rngTemp In rngSource.Cells
        If VarType(rngTemp.Value) = vbString Then
            If dicSheets.Exists(rngTemp.Value) Then
                MsgBox "Bi trung sheet ten: " & rngTemp.Value
                GoTo FreeMemony
            Else
                dicSheets.Add rngTemp.Value, Nothing
            End If
        End If
    Next
    
    If lngNumSheetBefor = dicSheets.Count Then
        MsgBox "Khong tim thay phan tu moi nao"
        GoTo FreeMemony
    End If
    
    aSheets = dicSheets.Keys
    Set wsBefor = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    For lngIndex = lngNumSheetBefor To dicSheets.Count - 1
        wsSource.Copy , wsBefor
        Set wsBefor = FindNewSheet(dicObjSheet, ThisWorkbook)
        dicObjSheet.Add wsBefor, ""
        wsBefor.Name = aSheets(lngIndex)
    Next
    
    
    
    
    
FreeMemony:
    dicSheets.RemoveAll
    dicObjSheet.RemoveAll
    Set dicSheets = Nothing
    Set dicObjSheet = Nothing
    Set rngSource = Nothing
    Set rngTemp = Nothing
    
    Set wsBefor = Nothing
    Set wsSource = Nothing
    Set objTemp = Nothing
    
    
End Sub


Private Function FindNewSheet(dicListShOl As Scripting.Dictionary, wb As Workbook) As Object
    Dim objTemp As Object
    For Each objTemp In wb.Sheets
        If Not dicListShOl.Exists(objTemp) Then
            Set FindNewSheet = objTemp
            Exit Function
        End If
    Next
    Err.Raise vbObjectError + 10, , "Not found new sheet."
End Function
 

File đính kèm

Upvote 0
Upvote 0
PHP:
Sub InsertSheets()
    Dim dicSheets As Scripting.Dictionary, dicObjSheet As Scripting.Dictionary
    Dim wsSource As Worksheet
    Dim rngSource As Range
    Dim objTemp As Object
    Dim rngTemp As Range
    Dim lngNumSheetBefor As Long, aSheets, lngIndex As Long
    Dim wsBefor As Object
   
    If Selection Is Nothing Then
        MsgBox "Khong tim thay vung danh sach"
    Else
        If TypeOf Selection Is Range Then
            Set rngSource = Selection
           
        Else
            MsgBox "Phai chon mot vung truoc khi chay macro"
            Exit Sub
        End If
    End If
   
    On Error Resume Next
    Set wsSource = ThisWorkbook.Sheets("Form")
    wsSource.Visible = xlSheetVisible
    If Err.Number <> 0 Then
        MsgBox "Khong tim thay sheet  nguon"
        Exit Sub
    End If
   
    On Error GoTo 0
    Set dicSheets = New Scripting.Dictionary 'danh sach ten cac sheet dang co và se duoc them
    Set dicObjSheet = New Scripting.Dictionary 'danh sách cac shet da co hien tai
    dicSheets.CompareMode = TextCompare
   
    For Each objTemp In ThisWorkbook.Sheets
        dicSheets.Add objTemp.Name, objTemp
        dicObjSheet.Add objTemp, ""
    Next
    lngNumSheetBefor = dicSheets.Count
   
    For Each rngTemp In rngSource.Cells
        If VarType(rngTemp.Value) = vbString Then
            If dicSheets.Exists(rngTemp.Value) Then
                MsgBox "Bi trung sheet ten: " & rngTemp.Value
                GoTo FreeMemony
            Else
                dicSheets.Add rngTemp.Value, Nothing
            End If
        End If
    Next
   
    If lngNumSheetBefor = dicSheets.Count Then
        MsgBox "Khong tim thay phan tu moi nao"
        GoTo FreeMemony
    End If
   
    aSheets = dicSheets.Keys
    Set wsBefor = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    For lngIndex = lngNumSheetBefor To dicSheets.Count - 1
        wsSource.Copy , wsBefor
        Set wsBefor = FindNewSheet(dicObjSheet, ThisWorkbook)
        dicObjSheet.Add wsBefor, ""
        wsBefor.Name = aSheets(lngIndex)
    Next
   
   
   
   
   
FreeMemony:
    dicSheets.RemoveAll
    dicObjSheet.RemoveAll
    Set dicSheets = Nothing
    Set dicObjSheet = Nothing
    Set rngSource = Nothing
    Set rngTemp = Nothing
   
    Set wsBefor = Nothing
    Set wsSource = Nothing
    Set objTemp = Nothing
   
   
End Sub


Private Function FindNewSheet(dicListShOl As Scripting.Dictionary, wb As Workbook) As Object
    Dim objTemp As Object
    For Each objTemp In wb.Sheets
        If Not dicListShOl.Exists(objTemp) Then
            Set FindNewSheet = objTemp
            Exit Function
        End If
    Next
    Err.Raise vbObjectError + 10, , "Not found new sheet."
End Function
Vấn đề nhìn có vẽ đơn giản, nhưng với chuyên gia thì ... quá hay
 
Upvote 0
chơi mấy cái count nguy hiểm lắm, dùng cái activesheet cho an tâm hơn.
https://stackoverflow.com/questions...eet-to-end-of-workbook-with-hidden-worksheets
Chỉ là viết theo thưc tế tình huống tác giả yêu cầu (chọn Range trong cột C)
Biết tới đâu làm tới đó. Có gì thì tác giả "la lên" tính tiếp.
Làm sao dự đoán hết các tình huống "nếu". Đã có "nếu có ô rỗng" rồi đó.
 
Upvote 0
PHP:
Sub InsertSheets()
    Dim dicSheets As Scripting.Dictionary, dicObjSheet As Scripting.Dictionary
    Dim wsSource As Worksheet
    Dim rngSource As Range
    Dim objTemp As Object
    Dim rngTemp As Range
    Dim lngNumSheetBefor As Long, aSheets, lngIndex As Long
    Dim wsBefor As Object
   
    If Selection Is Nothing Then
        MsgBox "Khong tim thay vung danh sach"
    Else
        If TypeOf Selection Is Range Then
            Set rngSource = Selection
           
        Else
            MsgBox "Phai chon mot vung truoc khi chay macro"
            Exit Sub
        End If
    End If
   
    On Error Resume Next
    Set wsSource = ThisWorkbook.Sheets("Form")
    wsSource.Visible = xlSheetVisible
    If Err.Number <> 0 Then
        MsgBox "Khong tim thay sheet  nguon"
        Exit Sub
    End If
   
    On Error GoTo 0
    Set dicSheets = New Scripting.Dictionary 'danh sach ten cac sheet dang co và se duoc them
    Set dicObjSheet = New Scripting.Dictionary 'danh sách cac shet da co hien tai
    dicSheets.CompareMode = TextCompare
   
    For Each objTemp In ThisWorkbook.Sheets
        dicSheets.Add objTemp.Name, objTemp
        dicObjSheet.Add objTemp, ""
    Next
    lngNumSheetBefor = dicSheets.Count
   
    For Each rngTemp In rngSource.Cells
        If VarType(rngTemp.Value) = vbString Then
            If dicSheets.Exists(rngTemp.Value) Then
                MsgBox "Bi trung sheet ten: " & rngTemp.Value
                GoTo FreeMemony
            Else
                dicSheets.Add rngTemp.Value, Nothing
            End If
        End If
    Next
   
    If lngNumSheetBefor = dicSheets.Count Then
        MsgBox "Khong tim thay phan tu moi nao"
        GoTo FreeMemony
    End If
   
    aSheets = dicSheets.Keys
    Set wsBefor = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    For lngIndex = lngNumSheetBefor To dicSheets.Count - 1
        wsSource.Copy , wsBefor
        Set wsBefor = FindNewSheet(dicObjSheet, ThisWorkbook)
        dicObjSheet.Add wsBefor, ""
        wsBefor.Name = aSheets(lngIndex)
    Next
   
   
   
   
   
FreeMemony:
    dicSheets.RemoveAll
    dicObjSheet.RemoveAll
    Set dicSheets = Nothing
    Set dicObjSheet = Nothing
    Set rngSource = Nothing
    Set rngTemp = Nothing
   
    Set wsBefor = Nothing
    Set wsSource = Nothing
    Set objTemp = Nothing
   
   
End Sub


Private Function FindNewSheet(dicListShOl As Scripting.Dictionary, wb As Workbook) As Object
    Dim objTemp As Object
    For Each objTemp In wb.Sheets
        If Not dicListShOl.Exists(objTemp) Then
            Set FindNewSheet = objTemp
            Exit Function
        End If
    Next
    Err.Raise vbObjectError + 10, , "Not found new sheet."
End Function

, post: 836063, member: 163936"]Chỉ là viết theo thưc tế tình huống tác giả yêu cầu (chọn Range trong cột C)
Biết tới đâu làm tới đó. Có gì thì tác giả "la lên" tính tiếp.
Làm sao dự đoán hết các tình huống "nếu". Đã có "nếu có ô rỗng" rồi đó.[/QUOTE]

Hihihi, thật tuyệt vời.
Cảm ơn các bạn, cảm ơn diễn đàn!
 
Upvote 0
Upvote 0
Tưởng bạn "la làng" thì tính tiếp chứ xong rồi thì thôi.
Cần lưu ý khi chọn các ô có giá trị là số hoặc có ký tự mà Excel không cho phép đặt tên sheet.

Dạ vâng Anh.
Đúng rồi ạ , với các ký tự đặc biệt thì có vấn đề ạ.

Hê anh huuthang_bd có thể bổ sung giúp Oanh Thơ phần còn lại này được không ạ.

Cảm ơn anh huuthang_bd nhiều. :)
 
Upvote 0
Upvote 0
Upvote 0
Web KT

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

Back
Top Bottom