create new sheet based on cell value and copy data

Hasson

Active Member
Joined
Apr 8, 2021
Messages
390
Office Version
  1. 2016
Platform
  1. Windows
hello

I have sheet MAIN contains data from row 4 . the range is (A4:H) and B2= name customer . so what I want if the B2 is Hasson ,then should create new sheet's name is Hasson and copy data from sheet MAIN into sheet Hasson from row2 and if I return to copy data for sheet has already created , then should copy to the bottom and if the name is new based on B2 then should create new sheet and copy the data from row2 and so on
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of your sheet. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
 
Upvote 0
orginal data
OUTPUT.xlsm
ABCDEFGH
1name
2
3
4ITEMIDBRANDMANFACTREFBATCHORDERQTY
5BS-TA-113QQW-14 L/R EG CLA14 SS230DF/100RF-100FT-1AAR112
6BS-TA-117QQW-181 RRSDFBRIRF-101FT-2AAR2234
7BS-TA-118QQW-19 CLA19ITY CVRF-102FT-3AAR3400
8BS-TA-119QQW-20 KV/1**2CLA20 CVRF-103FT-4AAR4123
9BS-TA-107QQW-8 CLA8 UKIRF-104FT-5AAR5456
10BS-TA-108QQW-9 N CLA91BRRF-105FT-6AAR6600
main



if the B2 is filled
OUTPUT.xlsm
ABCDEFGH
1name
2Hasson
3
4ITEMIDBRANDMANFACTREFBATCHORDERQTY
5BS-TA-113QQW-14 L/R EG CLA14 SS230DF/100RF-100FT-1AAR112
6BS-TA-117QQW-181 RRSDFBRIRF-101FT-2AAR2234
7BS-TA-118QQW-19 CLA19ITY CVRF-102FT-3AAR3400
8BS-TA-119QQW-20 KV/1**2CLA20 CVRF-103FT-4AAR4123
9BS-TA-107QQW-8 CLA8 UKIRF-104FT-5AAR5456
10BS-TA-108QQW-9 N CLA91BRRF-105FT-6AAR6600
main


and create new sheet
OUTPUT.xlsm
ABCDEFGH
3ITEMIDBRANDMANFACTREFBATCHORDERQTY
4BS-TA-113QQW-14 L/R EG CLA14 SS230DF/100RF-100FT-1AAR112
5BS-TA-117QQW-181 RRSDFBRIRF-101FT-2AAR2234
6BS-TA-118QQW-19 CLA19ITY CVRF-102FT-3AAR3400
7BS-TA-119QQW-20 KV/1**2CLA20 CVRF-103FT-4AAR4123
8BS-TA-107QQW-8 CLA8 UKIRF-104FT-5AAR5456
9BS-TA-108QQW-9 N CLA91BRRF-105FT-6AAR6600
Hasson


and if run again with the same name then
OUTPUT.xlsm
ABCDEFGH
3ITEMIDBRANDMANFACTREFBATCHORDERQTY
4BS-TA-113QQW-14 L/R EG CLA14 SS230DF/100RF-100FT-1AAR112
5BS-TA-117QQW-181 RRSDFBRIRF-101FT-2AAR2234
6BS-TA-118QQW-19 CLA19ITY CVRF-102FT-3AAR3400
7BS-TA-119QQW-20 KV/1**2CLA20 CVRF-103FT-4AAR4123
8BS-TA-107QQW-8 CLA8 UKIRF-104FT-5AAR5456
9BS-TA-108QQW-9 N CLA91BRRF-105FT-6AAR6600
10BS-TA-113QQW-14 L/R EG CLA14 SS230DF/100RF-100FT-1AAR112
11BS-TA-117QQW-181 RRSDFBRIRF-101FT-2AAR2234
12BS-TA-118QQW-19 CLA19ITY CVRF-102FT-3AAR3400
13BS-TA-119QQW-20 KV/1**2CLA20 CVRF-103FT-4AAR4123
14BS-TA-107QQW-8 CLA8 UKIRF-104FT-5AAR5456
15BS-TA-108QQW-9 N CLA91BRRF-105FT-6AAR6600
Hasson


and if add different name then should create new sheet .
 
Upvote 0
Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your "main" sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Enter a name in B2 and press the RETURN key.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Intersect(Target, Range("B2")) Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    Dim srcWS As Worksheet
    Set srcWS = ActiveSheet
    If Not Evaluate("isref('" & Target.Value & "'!A1)") Then
        Sheets.Add(After:=Sheets(Sheets.Count)).Name = Target
        With ActiveSheet
            .Range("A1").Resize(, 8).Value = Array("EM", "ID", "BRAND", "MANFACT", "REF", "BATCH", "ORDER", "QTY")
            srcWS.UsedRange.Offset(4).Copy .Range("A2")
            .Columns.AutoFit
        End With
    Else
        With Sheets(Target.Value)
            ActiveSheet.UsedRange.Offset(4).Copy .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
        End With
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
great code ! just question if you don't mind . I know this is not my requirement in OP how can I mod the code by making replace data instead of copy to the bottom in new sheet,please?
 
Upvote 0
making replace data instead of copy to the bottom in new sheet,please
Please explain in detail what you mean by this using examples from your data and post updated sheets if necessary.
 
Upvote 0
see the last picture when run again shouldn't copy to the bottom repeatedly as in rows from 10 :15. it should delete it just copy from the first time

and not copy every time press into cell B2 for the same created new sheet .
 
Upvote 0
Try:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Intersect(Target, Range("B2")) Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    Dim srcWS As Worksheet
    Set srcWS = ActiveSheet
    If Not Evaluate("isref('" & Target.Value & "'!A1)") Then
        Sheets.Add(After:=Sheets(Sheets.Count)).Name = Target
        With ActiveSheet
            .Range("A1").Resize(, 8).Value = Array("EM", "ID", "BRAND", "MANFACT", "REF", "BATCH", "ORDER", "QTY")
            srcWS.UsedRange.Offset(4).Copy .Range("A2")
            .Columns.AutoFit
        End With
    Else
        With Sheets(Target.Value)
            .UsedRange.Offset(1).ClearContents
            ActiveSheet.UsedRange.Offset(4).Copy .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
        End With
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
excellent ! but I note when copy the headers it deletes the borders and formatting . it shouldn't. may you fix it ,please?
 
Upvote 0
Try:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Intersect(Target, Range("B2")) Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    Dim srcWS As Worksheet
    Set srcWS = ActiveSheet
    If Not Evaluate("isref('" & Target.Value & "'!A1)") Then
        Sheets.Add(After:=Sheets(Sheets.Count)).Name = Target
        With ActiveSheet
            .Range("A1").Resize(, 8).Value = Array("EM", "ID", "BRAND", "MANFACT", "REF", "BATCH", "ORDER", "QTY")
            srcWS.UsedRange.Offset(4).Copy .Range("A2")
            .Columns.AutoFit
            .Range("A1").Resize(, 8).Interior.ColorIndex = 33
            .UsedRange.Cells.Borders.LineStyle = xlContinuous
        End With
    Else
        With Sheets(Target.Value)
            .UsedRange.Offset(1).ClearContents
            ActiveSheet.UsedRange.Offset(4).Copy .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
        End With
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,353
Messages
6,124,462
Members
449,163
Latest member
kshealy

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