Macro for copy row to new sheet based on text in cell, or create new sheet

Melanie1987

New Member
Joined
Jul 2, 2018
Messages
5
Good afternoon.

I've been struggling and searching the forum. But can't seem to find the correct macro for my problem.
I have a working data sheet (each week more data is added)
From this sheet I would like to copy the rows to different sheets, based on the text value in column K.

That part is no problem, when the value's stay the same.
But when new values are added in column K, which doesn't have a similar sheet, I would like to have the macro create a Sheet.

Is it possible to add something in the macro that checks if there is a sheet with the same name as the value in column K.
Then copy's the rows to this sheet.
And if there is no sheet, creates a sheet with the value name, and copy's the row?

As of this moment below is my current macro.

Sub Macro2()
Dim lr As Long, lr2 As Long, r As Long
lr = Sheets("data").Cells(Rows.Count, "A").End(xlUp).Row


For r = lr To 2 Step -1
Select Case Range("K" & r).Wrongel
Case Is = "Wrongel"
Rows(r).Copy Destination:=Sheets("Value").Range("A" & lr2 + 1)
lr2 = Sheets("Wrongel").Cells(Rows.Count, "A").End(xlUp).Row



End Select
Next r
End Sub


Thank you guys some much in advance.

Greetings,
Melanie
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
This macro assumes you have headers in row 1 and your data starts in row 2.
Code:
Sub CreateSheet()
    Application.ScreenUpdating = False
    Dim bottomA As Long
    bottomK = Sheets("data").Range("K" & Rows.Count).End(xlUp).Row
    Dim rng As Range
    Dim ws As Worksheet
    Dim rngUniques As Range
    Sheets("data").Range("K1:K" & bottomK).AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range _
        ("K1:K" & bottomK), Unique:=True
    Set rngUniques = Sheets("data").Range("K2:K" & bottomK).SpecialCells(xlCellTypeVisible)
    If Sheets("data").AutoFilterMode = True Then Sheets("data").AutoFilterMode = False
    For Each rng In rngUniques
        Set ws = Nothing
        On Error Resume Next
        Set ws = Worksheets(rng.Value)
        On Error GoTo 0
        If ws Is Nothing Then
            Worksheets.Add(After:=Sheets(Sheets.Count)).Name = rng.Value
            Sheets("data").Rows(1).Copy Cells(1, 1)
        End If
    Next rng
    For Each rng In rngUniques
        Sheets(rng.Value).UsedRange.Offset(1, 0).ClearContents
        Sheets("data").Range("K1:K" & bottomK).AutoFilter Field:=1, Criteria1:=rng
        Sheets("data").Range("K2:K" & bottomK).SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets(rng.Value).Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
        If Sheets("data").AutoFilterMode = True Then Sheets("data").AutoFilterMode = False
    Next rng
    Application.ScreenUpdating = True
 End Sub
 
Upvote 0
I'd be inclined to use a UDF

Code:
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">[COLOR=#101094][FONT=inherit]Function[/FONT][/COLOR][COLOR=#303336][FONT=inherit] SheetExists[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#303336][FONT=inherit]shtName [/FONT][/COLOR][COLOR=#101094][FONT=inherit]As[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#101094][FONT=inherit]String[/FONT][/COLOR][COLOR=#303336][FONT=inherit],[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#101094][FONT=inherit]Optional[/FONT][/COLOR][COLOR=#303336][FONT=inherit] wb [/FONT][/COLOR][COLOR=#101094][FONT=inherit]As[/FONT][/COLOR][COLOR=#303336][FONT=inherit] Workbook[/FONT][/COLOR][COLOR=#303336][FONT=inherit])[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#101094][FONT=inherit]As[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#101094][FONT=inherit]Boolean[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    [/FONT][/COLOR][COLOR=#101094][FONT=inherit]Dim[/FONT][/COLOR][COLOR=#303336][FONT=inherit] sht [/FONT][/COLOR][COLOR=#101094][FONT=inherit]As[/FONT][/COLOR][COLOR=#303336][FONT=inherit] Worksheet

     [/FONT][/COLOR][COLOR=#101094][FONT=inherit]If[/FONT][/COLOR][COLOR=#303336][FONT=inherit] wb [/FONT][/COLOR][COLOR=#101094][FONT=inherit]Is[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]Nothing[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#101094][FONT=inherit]Then[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#101094][FONT=inherit]Set[/FONT][/COLOR][COLOR=#303336][FONT=inherit] wb [/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#303336][FONT=inherit] ThisWorkbook
     [/FONT][/COLOR][COLOR=#101094][FONT=inherit]On[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#101094][FONT=inherit]Error[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#101094][FONT=inherit]Resume[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#101094][FONT=inherit]Next[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
     [/FONT][/COLOR][COLOR=#101094][FONT=inherit]Set[/FONT][/COLOR][COLOR=#303336][FONT=inherit] sht [/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#303336][FONT=inherit] wb[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]Sheets[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#303336][FONT=inherit]shtName[/FONT][/COLOR][COLOR=#303336][FONT=inherit])[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
     [/FONT][/COLOR][COLOR=#101094][FONT=inherit]On[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#101094][FONT=inherit]Error[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#101094][FONT=inherit]GoTo[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]0[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
     SheetExists [/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#101094][FONT=inherit]Not[/FONT][/COLOR][COLOR=#303336][FONT=inherit] sht [/FONT][/COLOR][COLOR=#101094][FONT=inherit]Is[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]Nothing[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
 [/FONT][/COLOR][COLOR=#101094][FONT=inherit]End[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#101094][FONT=inherit]Function[/FONT][/COLOR]</code>

You can then use an IF statement, something along the lines of
Code:
If SheetExists(your value in column K) = True then
     You know the sheet already exists
Else
     You know the sheet doesnt exist and you can create one if need be
End If
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top