Hi friends,
This problem might be simple, but it's been tedious to figure. Say there are five different values in a range of 10 cells .e.g. in range("A1:A10") there are values valueA, valueB, valueC, valueD, and valueE. this means these values get to be repeated randomly in all 10cells.
I would like to identify, with a macro, the five different values in the range, and use these values to create new 5 sheets whose tab names are those 5 values discovered in the range. The idea then is to avoid the error that occurs when you rename one sheet with a sheet name already used by another sheet.
I appreciate the help.
Bryen Walt
Last edited by bryenwalt; 03-30-2010 at 07:19 AM.
Can the values in A1:A10 be sorted? If so you could then just loop through them checking if the cell you are on is the same as the one above and if so not create a sheet for it.
Dom
"May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."
Use code tags when posting your VBA code: [code] Your code here [/code]
Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.
Try this.
Sub Test() Dim SheetFound As Boolean Dim Cell As Range Dim Sheet As Worksheet For Each Cell In Range("A1:A10") SheetFound = False For Each Sheet In ActiveWorkbook.Sheets If Sheet.Name = Cell.Value Then SheetFound = True Exit For End If Next Sheet If SheetFound = False Then Sheets.Add after:=Sheets(Sheets.Count) ActiveSheet.Name = Cell.Value End If Next Cell End Sub
Martin
Eighty Twenty Spreadsheet Automation http://homepage.ntlworld.com/martin.rice1/ for all your Excel customisation and consulting needs.
If my solution has saved you time and/or money, please consider donating to Cancer Research UK.
Another way:
Sub x() Dim cell As Range For Each cell In Range("A1:A10") If Len(cell.Text) Then If Not SheetExists(cell.Text) Then Worksheets.Add after:=Sheets(Sheets.Count) ActiveSheet.Name = cell.Text End If Next cell End Sub Function SheetExists(sWks As String, Optional wkb As Workbook) As Boolean On Error Resume Next SheetExists = Not IIf(wkb Is Nothing, ActiveWorkbook, wkb).Sheets(sWks) Is Nothing End Function
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
MRICE, you code working brilliantly. SHG, you code does work, but produces the error "cannot rename a sheet to the same name as another sheet...". it's this error that was giving me alot of trouble. MRICE's code takes care of that error. Thanks guys, a burden has been lifted...
Sorry for the bug. Corrected for anyone who might be interested.
Sub x() Dim cell As Range For Each cell In Range("A1:A10") If Len(cell.Text) Then If Not SheetExists(cell.Text) Then Worksheets.Add after:=Sheets(Sheets.Count) ActiveSheet.Name = cell.Text End If End If Next cell End Sub Function SheetExists(sWks As String, Optional wkb As Workbook) As Boolean On Error Resume Next SheetExists = Not IIf(wkb Is Nothing, ActiveWorkbook, wkb).Sheets(sWks) Is Nothing End Function
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks