I have a template worksheet named "0MAX2RE" that I need to copy and then rename based on a list. My problem is that the list contains duplicates, which I need to show with the suffix (1), (2), Etc. The list also contains text that could be over 31 characters and have special characters. I am using inputbox for the user to let me know which items they want to copy a new sheet for. Here is what I am working with, and it works, but not always, and not always as intended
VBA Code:
Public Sub CopyOmaxsBidSheets()
Dim wks As Worksheet
Dim FindMU As String
Set wks = Worksheets("Bid Sheet")
Dim xcell, Xrg, Xrg1, xCOSTrg, xMUrg As Range
Dim k As Integer
On Error GoTo ErrorHandler
Set Xrg = Application.InputBox("Please select the items to create bid sheet for:", "Do It,", , , , , , 8)
If Xrg Is Nothing Then Exit Sub
For Each xcell In Xrg
'k = k + 1
If xcell.Value <> "" Then
On Error Resume Next
Sheets("0MAX2RE").Visible = True
Sheets("0MAX2RE").Copy After:=Worksheets(Sheets.Count)
ActiveSheet.Name = Left(xcell.Value, 29)
With ActiveSheet
Set Xrg1 = .Cells.Find(what:="MARK-UP")
Set xCOSTrg = Xrg1.Offset(-2, 2)
Set xMUrg = Xrg1.Offset(1, 1)
wks.Range(xcell.Address, xcell.Address).Offset(0, 4).Value = "='" & ActiveSheet.Name & "'!" & xCOSTrg.Address(RowAbsolute:=False, ColumnAbsolute:=False)
wks.Range(xcell.Address, xcell.Address).Offset(0, 5).Value = "='" & ActiveSheet.Name & "'!" & xMUrg.Address(RowAbsolute:=False, ColumnAbsolute:=False)
End With
If Err.Number = 1004 Then
k = k + 1
ActiveSheet.Name = Left(xcell.Value, 27) & "(" & k & ")"
With ActiveSheet
Set Xrg1 = .Cells.Find(what:="MARK-UP")
Set xCOSTrg = Xrg1.Offset(-2, 2)
Set xMUrg = Xrg1.Offset(1, 1)
wks.Range(xcell.Address, xcell.Address).Offset(0, 4).Value = "='" & ActiveSheet.Name & "'!" & xCOSTrg.Address(RowAbsolute:=False, ColumnAbsolute:=False)
wks.Range(xcell.Address, xcell.Address).Offset(0, 5).Value = "='" & ActiveSheet.Name & "'!" & xMUrg.Address(RowAbsolute:=False, ColumnAbsolute:=False)
End With
If Err.Number = 1004 Then
k = k + 1
ActiveSheet.Name = Left(xcell.Value, 27) & "(" & k & ")"
With ActiveSheet
Set Xrg1 = .Cells.Find(what:="MARK-UP")
Set xCOSTrg = Xrg1.Offset(-2, 2)
Set xMUrg = Xrg1.Offset(1, 1)
wks.Range(xcell.Address, xcell.Address).Offset(0, 4).Value = "='" & ActiveSheet.Name & "'!" & xCOSTrg.Address(RowAbsolute:=False, ColumnAbsolute:=False)
wks.Range(xcell.Address, xcell.Address).Offset(0, 5).Value = "='" & ActiveSheet.Name & "'!" & xMUrg.Address(RowAbsolute:=False, ColumnAbsolute:=False)
End With
End If
End If
End If
Next
ErrorHandler:
Sheets("0MAX2RE").Visible = False
End Sub