Option Explicit
Sub sbColumnsToSheets2()
' Purpose: Create a separate worksheet for each column in your selection
' the name of the sheet will be the name of the first row in each column.
' Usage:
' first select the cells then run this macro
Dim rngSelection As Range
Dim wsStart As Worksheet
Dim wsNew As Worksheet
Dim lRowLast As Long
Dim lCollast As Long
Dim lRowFirst As Long
Dim lColFirst As Long
Dim lCol As Long
Dim strSheetName As String
Dim fnUniqueSheetName
On Error GoTo sbColumnsToSheets_Error
Application.ScreenUpdating = False
Set wsStart = ActiveSheet
Set rngSelection = wsStart.Range("B4:AY10")
lRowFirst = rngSelection.Cells(1).Row
lColFirst = rngSelection.Cells(1).Column
lRowLast = rngSelection.Cells(rngSelection.Cells.Count).Row
lCollast = rngSelection.Cells(rngSelection.Cells.Count).Column
' loop through the columns
For lCol = lColFirst To lCollast
' add a new sheet at the end
ActiveWorkbook.Sheets.Add after:=Sheets(Sheets.Count), Type:=xlWorksheet
Set wsNew = ActiveSheet
' copy the column from the original sheet
wsStart.Activate
wsStart.Range("A1:A10").Copy
wsNew.Range("A1").PasteSpecial Paste:=xlPasteValues
wsNew.Paste
wsStart.Range(Cells(lRowFirst, lCol), Cells(lRowLast, lCol)).Copy
wsNew.Range("B4").PasteSpecial Paste:=xlPasteValues
wsNew.Paste
wsNew.Range("C4:C10").Value = wsStart.Range("AZ4:AZ10").Value
wsNew.Range("C4").Font.Bold = True
wsNew.Columns("A:F").AutoFit
Application.CutCopyMode = False
wsNew.Name = wsNew.Range("B4").Value
Next lCol
wsStart.Activate
Application.ScreenUpdating = True
Application.StatusBar = False
On Error GoTo 0
Exit Sub
sbColumnsToSheets_Error:
Application.ScreenUpdating = True
Application.StatusBar = False
MsgBox "Error " & Err.Number & " (" & Err.Description & ")" & _
"in procedure sbColumnsToSheets", vbCritical
End Sub
Private Function fnUniqueSheetName2(strName As String) As String
' Loop through all sheets and see if the name already exists
' if is does, add a number to the name and check again.
' Repeat until a unique name is found
' The maximum length for a sheetname is 31 characters.
Dim objSheet As Object
Dim strNewName As String
Dim i As Long
' Certain characters are not allowed in a sheet's namea:
strName = Replace(strName, ":", "_")
strName = Replace(strName, "\", "_")
strName = Replace(strName, "/", "_")
strName = Replace(strName, "?", "_")
strName = Replace(strName, "*", "_")
strName = Replace(strName, "[", "_")
strName = Replace(strName, "]", "_")
strNewName = strName
i = 1
If Len(strNewName) > 31 Then
strNewName = Left(strNewName, 21) & ".." & Right(strNewName, 8)
End If
Start:
For Each objSheet In ActiveWorkbook.Sheets
If objSheet.Name = strNewName Then
strNewName = strName & " (" & i & ")"
If Len(strNewName) > 31 Then
strNewName = Left(strNewName, 21) & ".." & Right(strNewName, 8)
End If
i = i + 1
GoTo Start
Exit For
End If
Next
fnUniqueSheetName = strNewName
End Function