Hi,
I need to check when one of the values in the column is equal to a worksheet that is currently in the excel file. Just a message box that says something like “A worksheet currently exists with the name of a value in this list. Please rename the current worksheet”. When the user gets the message box, they should be able to click “OK” and the function ends.
Is it possible to modify this code?
Option Explicit
Option Compare Text
Sub sheets_from_colvalues(control As IRibbonControl)
'Const sh1 As String = "Sheet1"
Dim sh1 As String
sh1 = ActiveSheet.Name
Dim a As Variant, x As Worksheet, sh As Worksheet
Dim rws&, cls&, p&, i&, rr&, b As Boolean
Dim cl&
cl = Selection.Column
Application.ScreenUpdating = False
Sheets(sh1).Activate
rws = Cells.Find("*", , , , xlByRows, xlPrevious).Row
cls = Cells.Find("*", , , , xlByColumns, xlPrevious).Column
Set x = Sheets.Add(After:=Sheets(sh1))
Sheets(sh1).Cells(1).Resize(rws, cls).Copy x.Cells(1)
Set a = x.Cells(1).Resize(rws, cls)
a.Sort a(1, cl), 2, Header:=xlYes
a = a.Resize(rws + 1)
p = 2
For i = p To rws + 1
If a(i, cl) <> a(p, cl) Then
b = False
For Each sh In Worksheets
If sh.Name = a(p, cl) Then b = True: Exit For
Next
If Not b Then
With Sheets.Add
.Name = a(p, cl)
x.Cells(1).Resize(, cls).Copy .Cells(1)
rr = .Cells.Find("*", , , , xlByRows, xlPrevious).Row + 1
x.Cells(p, 1).Resize(i - p, cls).Cut .Cells(rr, 1)
.Columns.AutoFit
End With
End If
p = i
End If
Next i
Application.DisplayAlerts = False
x.Delete
Application.DisplayAlerts = True
Sheets(sh1).Activate
Application.ScreenUpdating = True
End Sub
Thanks in advance!
Bookmarks