Create an Excel Worksheet for Each Unique Visible Value in Column K
Question details
Create one worksheet for each distinct visible value in column K without attempting to create duplicate worksheet names.

- Product
- Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- A filtered source table contains repeated values in column K, and a macro needs to copy each value's matching rows to a separate worksheet.
- Observed behavior
- The macro creates a worksheet for the first occurrence of a value, then encounters that value again and fails because the worksheet name is already taken. A blank worksheet may remain if it was added before the naming error occurred.
Confirm that the source table has headers in row 1, starts at A1, and includes column K; Field 11 refers to column K only when the filter range starts in column A. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm), and decide whether existing output sheets should be skipped, cleared, or replaced.
Collect unique visible values before creating worksheets
Use this VBA approach to process each visible column K value once and skip existing or invalid worksheet names before adding a sheet.
The example below assumes a contiguous table starting at A1 and uses late-bound Scripting.Dictionary, commonly available in Excel for Windows. It copies headers and filtered rows to new worksheets, skips existing worksheet names, and leaves the source filtered to the last processed value.
Worksheet names must be no more than 31 characters and cannot contain : \ / ? * [ ]. This example skips invalid names rather than silently changing them. Existing sheets are never cleared or deleted.
Open the workbook in Excel, select Developer > Visual Basic, and choose Insert > Module. If Developer is hidden, enable it through File > Options > Customize Ribbon > Developer.
Paste this code into the module: Option Explicit Sub CreateSheetsForVisibleK() Dim src As Worksheet, out As Worksheet Dim wb As Workbook, data As Range, visibleK As Range, c As Range Dim values As Object, key As Variant, badChar As Variant Dim lastRow As Long, nm As String, criterion As String Dim invalidName As Boolean, created As Long, skipped As Long Set src = ActiveSheet Set wb = src.Parent If wb.ProtectStructure Then MsgBox "Unprotect the workbook structure before creating sheets." Exit Sub End If Set data = src.Range("A1").CurrentRegion If data.Columns.Count < 11 Or data.Rows.Count < 2 Then MsgBox "Use a table starting at A1 with headers and column K." Exit Sub End If lastRow = data.Row + data.Rows.Count - 1 On Error Resume Next Set visibleK = src.Range("K2:K" & lastRow).SpecialCells(xlCellTypeVisible) On Error GoTo 0 If visibleK Is Nothing Then MsgBox "No visible data cells in column K." Exit Sub End If Set values = CreateObject("Scripting.Dictionary") values.CompareMode = vbTextCompare For Each c In visibleK.Cells If Not IsError(c.Value2) Then nm = CStr(c.Value2) If Len(Trim$(nm)) > 0 Then If Not values.Exists(nm) Then values.Add nm, nm End If End If Next c For Each key In values.Keys nm = CStr(key) invalidName = Len(nm) > 31 Or LCase$(nm) = "history" If Left$(nm, 1) = "'" Or Right$(nm, 1) = "'" Then invalidName = True For Each badChar In Array(":", "\", "/", "?", "*", "[", "]") If InStr(1, nm, CStr(badChar), vbBinaryCompare) > 0 Then invalidName = True Next badChar If invalidName Or SheetNameExists(wb, nm) Then skipped = skipped + 1 Else criterion = Replace(nm, "~", "~~") criterion = Replace(criterion, "*", "~*") criterion = Replace(criterion, "?", "~?") data.AutoFilter Field:=11, Criteria1:="=" & criterion Set out = wb.Worksheets.Add(After:=wb.Sheets(wb.Sheets.Count)) out.Name = nm data.SpecialCells(xlCellTypeVisible).Copy Destination:=out.Range("A1") created = created + 1 End If Next key MsgBox created & " worksheet(s) created; " & skipped & " name(s) skipped." End Sub Private Function SheetNameExists(ByVal wb As Workbook, ByVal nm As String) As Boolean Dim sh As Object For Each sh In wb.Sheets If StrComp(sh.Name, nm, vbTextCompare) = 0 Then SheetNameExists = True Exit Function End If Next sh End Function
Return to Excel, select the source worksheet, and apply the filters that define the values you want to process. Select Developer > Macros > CreateSheetsForVisibleK > Run. The macro captures the unique visible values before changing column K's filter.
Open the new worksheet tabs and verify that each contains the header and matching visible rows. If the completion message reports skipped names, compare the column K values with existing sheet tabs and Excel's worksheet-name restrictions. Reapply the original column K filter afterward if needed.

Choose an explicit policy for existing output worksheets
Use this when the macro needs to run repeatedly and existing output tabs should be refreshed rather than skipped.
Organize filtered Excel data with WPS Spreadsheet
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for Excel formats, making it easy to move routine filtering and worksheet organization to WPS Spreadsheet. It does not repair Excel VBA errors, and VBA execution depends on the WPS edition and environment.

Frequently Asked Questions
Why does the macro create a blank worksheet before reporting a duplicate name?
Worksheets.Add creates a sheet immediately. If the next statement assigns a name already used in the workbook, that assignment fails while the new blank sheet remains. Check for an existing name before calling Worksheets.Add.
Does SpecialCells(xlCellTypeVisible) return unique values?
No. It returns visible cells, including repeated values. Apply it to the data cells in column K, then add their values to a Dictionary or another unique-value collection before looping to create sheets.
When does AutoFilter Field:=11 refer to column K?
Field is relative to the left edge of the filter range. Field:=11 refers to column K when the range starts in column A. If the table starts in column B, column K is Field:=10.
What happens when I run the supplied macro again?
It skips names already used by any sheet in the workbook. Select the original source worksheet and restore your intended filters before running Developer > Macros > CreateSheetsForVisibleK again. To refresh existing outputs, implement a deliberate reuse or replacement policy.
Can I run this Excel VBA macro in WPS Spreadsheet?
Do not assume it will run unchanged. VBA support depends on the WPS edition and environment, and this example also uses Scripting.Dictionary. For a macro-free workflow, open the workbook in WPS Spreadsheet, use Data > Filter on column K, and copy each value's visible rows and headers to a separately named worksheet.




