想复制工作表并从单元格值中为其命名,但是如果该名称已经存在,我想停止添加新工作表,但是我仍然会收到错误提示消息
Eroor 1004 该名称已被使用。尝试其他
Dim ws1 As Worksheet, ws2 As Worksheet
Dim rTarget As Range
Dim xWs As Worksheet
Dim ws As Worksheet
Dim wkSht As Worksheet
If IsEmpty(Range("E9")) = True Then
'Cell A2 is not blank
MsgBox "Please Add An Employee Number"
Else
For Each wkSht In Sheets
If ActiveSheet.Range("e9").Value = wkSht.Name Then
MsgBox "Employee Already Has A Record Added"
End If
Next
Set wh = Worksheets(ActiveSheet.Name)
ActiveSheet.Copy After:=Worksheets(Sheets.Count)
If wh.Range("E9").Value <> "" Then
ActiveSheet.Name = wh.Range("E9").Value
End If
wh.Activate
Set ws1 = Sheet1
Set ws2 = Sheet4
Set rTarget = ws2.Range("A65536").End(xlUp).Offset(1, 0)
With ws2
rTarget = ws1.Range("E7:G7").Value
rTarget.Offset(0, 1).Value = ws1.Range("E9:G9").Value
rTarget.Offset(0, 2).Value = ws1.Range("E9:G9").Value
rTarget.Offset(0, 3).Value = ws1.Range("K7:M7").Value
rTarget.Offset(0, 4).Value = ws1.Range("K9:M9").Value
rTarget.Offset(0, 5).Value = ws1.Range("K11:M11").Value
rTarget.Offset(0, 6).Value = ws1.Range("K16:M16").Value
rTarget.Offset(0, 7).Value = ws1.Range("K18:M18").Value
rTarget.Offset(0, 8).Value = ws1.Range("E16:G16").Value
rTarget.Offset(0, 9).Value = ws1.Range("E18:G18").Value
rTarget.Offset(0, 10).Value = ws1.Range("E20:G20").Value
rTarget.Offset(0, 11).Value = ws1.Range("E23").Value
rTarget.Offset(0, 12).Value = ws1.Range("f23").Value
rTarget.Offset(0, 13).Value = ws1.Range("g23").Value
rTarget.Offset(0, 14).Value = ws1.Range("E24").Value
rTarget.Offset(0, 15).Value = ws1.Range("f24").Value
rTarget.Offset(0, 16).Value = ws1.Range("g24").Value
rTarget.Offset(0, 17).Value = ws1.Range("E25").Value
rTarget.Offset(0, 18).Value = ws1.Range("f25").Value
rTarget.Offset(0, 19).Value = ws1.Range("g25").Value
rTarget.Offset(0, 20).Value = ws1.Range("E26").Value
rTarget.Offset(0, 21).Value = ws1.Range("f26").Value
rTarget.Offset(0, 22).Value = ws1.Range("g26").Value
rTarget.Offset(0, 23).Value = ws1.Range("E27").Value
rTarget.Offset(0, 24).Value = ws1.Range("f27").Value
rTarget.Offset(0, 25).Value = ws1.Range("g27").Value
rTarget.Offset(0, 26).Value = ws1.Range("E28").Value
rTarget.Offset(0, 27).Value = ws1.Range("f28").Value
rTarget.Offset(0, 28).Value = ws1.Range("g28").Value
rTarget.Offset(0, 29).Value = ws1.Range("E29").Value
rTarget.Offset(0, 30).Value = ws1.Range("f29").Value
rTarget.Offset(0, 31).Value = ws1.Range("g29").Value
rTarget.Offset(0, 32).Value = ws1.Range("k23").Value
rTarget.Offset(0, 33).Value = ws1.Range("l23").Value
rTarget.Offset(0, 34).Value = ws1.Range("m23").Value
rTarget.Offset(0, 35).Value = ws1.Range("k24").Value
rTarget.Offset(0, 36).Value = ws1.Range("l24").Value
rTarget.Offset(0, 37).Value = ws1.Range("m24").Value
rTarget.Offset(0, 38).Value = ws1.Range("k25").Value
rTarget.Offset(0, 39).Value = ws1.Range("l25").Value
rTarget.Offset(0, 40).Value = ws1.Range("m25").Value
rTarget.Offset(0, 41).Value = ws1.Range("k26").Value
rTarget.Offset(0, 42).Value = ws1.Range("l26").Value
rTarget.Offset(0, 43).Value = ws1.Range("m26").Value
rTarget.Offset(0, 44).Value = ws1.Range("k27").Value
rTarget.Offset(0, 45).Value = ws1.Range("l27").Value
rTarget.Offset(0, 46).Value = ws1.Range("m27").Value
rTarget.Offset(0, 47).Value = ws1.Range("k28").Value
rTarget.Offset(0, 48).Value = ws1.Range("l28").Value
rTarget.Offset(0, 49).Value = ws1.Range("m28").Value
rTarget.Offset(0, 50).Value = ws1.Range("k29").Value
rTarget.Offset(0, 51).Value = ws1.Range("l29").Value
rTarget.Offset(0, 52).Value = ws1.Range("m29").Value
rTarget.Offset(0, 53).Value = ws1.Range("E34").Value
rTarget.Offset(0, 54).Value = ws1.Range("f34").Value
rTarget.Offset(0, 55).Value = ws1.Range("g34").Value
rTarget.Offset(0, 56).Value = ws1.Range("E35").Value
rTarget.Offset(0, 57).Value = ws1.Range("f35").Value
rTarget.Offset(0, 58).Value = ws1.Range("g35").Value
rTarget.Offset(0, 59).Value = ws1.Range("E36").Value
rTarget.Offset(0, 60).Value = ws1.Range("f36").Value
rTarget.Offset(0, 61).Value = ws1.Range("g36").Value
rTarget.Offset(0, 62).Value = ws1.Range("E37").Value
rTarget.Offset(0, 63).Value = ws1.Range("f37").Value
rTarget.Offset(0, 64).Value = ws1.Range("g37").Value
rTarget.Offset(0, 65).Value = ws1.Range("E38").Value
rTarget.Offset(0, 66).Value = ws1.Range("f38").Value
rTarget.Offset(0, 67).Value = ws1.Range("g38").Value
rTarget.Offset(0, 68).Value = ws1.Range("E39").Value
rTarget.Offset(0, 69).Value = ws1.Range("f39").Value
rTarget.Offset(0, 70).Value = ws1.Range("g39").Value
rTarget.Offset(0, 71).Value = ws1.Range("E40").Value
rTarget.Offset(0, 72).Value = ws1.Range("f40").Value
rTarget.Offset(0, 73).Value = ws1.Range("g40").Value
rTarget.Offset(0, 74).Value = ws1.Range("k34").Value
rTarget.Offset(0, 75).Value = ws1.Range("l34").Value
rTarget.Offset(0, 76).Value = ws1.Range("m34").Value
rTarget.Offset(0, 77).Value = ws1.Range("k35").Value
rTarget.Offset(0, 78).Value = ws1.Range("l35").Value
rTarget.Offset(0, 79).Value = ws1.Range("m35").Value
rTarget.Offset(0, 80).Value = ws1.Range("k36").Value
rTarget.Offset(0, 81).Value = ws1.Range("l36").Value
rTarget.Offset(0, 82).Value = ws1.Range("m36").Value
rTarget.Offset(0, 83).Value = ws1.Range("k37").Value
rTarget.Offset(0, 84).Value = ws1.Range("l37").Value
rTarget.Offset(0, 85).Value = ws1.Range("m37").Value
rTarget.Offset(0, 86).Value = ws1.Range("k38").Value
rTarget.Offset(0, 87).Value = ws1.Range("l38").Value
rTarget.Offset(0, 88).Value = ws1.Range("m38").Value
rTarget.Offset(0, 89).Value = ws1.Range("k39").Value
rTarget.Offset(0, 90).Value = ws1.Range("l39").Value
rTarget.Offset(0, 91).Value = ws1.Range("m39").Value
rTarget.Offset(0, 92).Value = ws1.Range("k40").Value
rTarget.Offset(0, 93).Value = ws1.Range("l40").Value
rTarget.Offset(0, 94).Value = ws1.Range("m40").Value
rTarget.Offset(0, 95).Value = ws1.Range("c43:m48").Value
End With
Range("E7:G7,E9:G9,K7:M7,K9:M9,K11:M11,K16:M16,K18:M18,E16:G16,E18:G18,E20:G20" _
).Select
Range("E20").Activate
ActiveWindow.SmallScroll Down:=12
Range( _
"E7:G7,E9:G9,K7:M7,K9:M9,K11:M11,K16:M16,K18:M18,E16:G16,E18:G18,E20:G20,E23:G29,K23:M29,K34:M40,E34:G40" _
).Select
Range("E34").Activate
ActiveWindow.SmallScroll Down:=9
Range( _
"E7:G7,E9:G9,K7:M7,K9:M9,K11:M11,K16:M16,K18:M18,E16:G16,E18:G18,E20:G20,E23:G29,K23:M29,K34:M40,E34:G40,C43:M48" _
).Select
Range("C43").Activate
Selection.ClearContents
ActiveWindow.SmallScroll Down:=-24
Range("E7:G7").Select
End If
Exit Sub
no:
MsgBox "Employee Already Has A Record Added, Please Search To Find"
End Sub
如果名称与单元格值匹配,只需停止添加新工作表