create new sheet based on cell value and copy data

Hasson

Active Member
Joined
Apr 8, 2021
Messages
387
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
 
this is what I got
mu.xlsm
ABCDEFGH
1EMIDBRANDMANFACTREFBATCHORDERQTY
2BS-TA-113QQW-14 L/R EG CLA14 SS230DF/100RF-100FT-1AAR112
3BS-TA-117QQW-181 RRSDFBRIRF-101FT-2AAR2234
4BS-TA-118QQW-19 CLA19ITY CVRF-102FT-3AAR3400
5BS-TA-119QQW-20 KV/1**2CLA20 CVRF-103FT-4AAR4123
6BS-TA-107QQW-8 CLA8 UKIRF-104FT-5AAR5456
7BS-TA-108QQW-9 N CLA91BRRF-105FT-6AAR6600
Hasson

but what I want based on the same thing into sheet main
1.PNG
 
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
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").Interior.ColorIndex = 23
            .Range("B1").Resize(, 7).Interior.ColorIndex = 33
            .UsedRange.Cells.Borders.LineStyle = xlContinuous
            .UsedRange.Offset(1).Cells.Interior.ColorIndex = 16
        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
Reverse the 23 and 33 in the code. That's pretty close to what you posted in Post #11. If the colours are not exactly what you want, you will have to find the colorindex numbers that correspond to the actual desired colours.
 
Upvote 0
I disabled these lines
VBA Code:
.UsedRange.Cells.Borders.LineStyle = xlContinuous
.UsedRange.Offset(1).Cells.Interior.ColorIndex = 16
and change from
Code:
            .Range("A1").Interior.ColorIndex = 23
to this
Code:
            .Range("A1").Interior.ColorIndex = 33
and I got almost what I want except the headers don't contain the same borders as in the rest of range, also doesn't autofit for
some columns B,C . how fix it?
1.PNG
 
Upvote 0
This line of code that you disabled would insert the borders in the whole usedrange:
VBA Code:
.UsedRange.Cells.Borders.LineStyle = xlContinuous
This line of code should autofit the columns:
VBA Code:
.Columns.AutoFit
Book1
ABCDEFGH
1EMIDBRANDMANFACTREFBATCHORDERQTY
2BS-TA-113QQW-14 L/R EG CLA14 SS230DF/100RF-100FT-1AAR112
3BS-TA-117QQW-181 RRSDFBRIRF-101FT-2AAR2234
4BS-TA-118QQW-19 CLA19ITY CVRF-102FT-3AAR3400
5BS-TA-119QQW-20 KV/1**2CLA20 CVRF-103FT-4AAR4123
6BS-TA-107QQW-8 CLA8 UKIRF-104FT-5AAR5456
7BS-TA-108QQW-9 N CLA91BRRF-105FT-6AAR6600
Hanson
 
Upvote 0
This line of code that you disabled would insert the borders in the whole usedrange:
VBA Code:
.UsedRange.Cells.Borders.LineStyle = xlContinuous
This line of code should autofit the columns:
VBA Code:
.Columns.AutoFit
this is strange ! but I would from you test it based on attached file and inform me what do you think .
mump.xlsm
 
Upvote 0
This version of the macro produces the result below:
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("ITEM", "ID", "BRAND", "MANFACT", "REF", "BATCH", "ORDER", "QTY")
            srcWS.UsedRange.Offset(4).Copy .Range("A2")
            .UsedRange.Cells.WrapText = False
            .Columns.AutoFit
            .Range("A1").Interior.ColorIndex = 23
            .Range("B1").Resize(, 7).Interior.ColorIndex = 33
           ' .UsedRange.Cells.Borders.LineStyle = xlContinuous
            '.UsedRange.Offset(1).Cells.Interior.ColorIndex = 16
        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
mump.xlsm
ABCDEFGH
1ITEMIDBRANDMANFACTREFBATCHORDERQTY
2BS-TA-113QQW-14 L/R EG CLA14 SS230DF/100RF-100FT-1AAR112
3BS-TA-117QQW-181 RRSDFBRIRF-101FT-2AAR2234
4BS-TA-118QQW-19 CLA19ITY CVRF-102FT-3AAR3400
5BS-TA-119QQW-20 KV/1**2CLA20 CVRF-103FT-4AAR4123
6BS-TA-107QQW-8 CLA8 UKIRF-104FT-5AAR5456
7BS-TA-108QQW-9 N CLA91BRRF-105FT-6AAR6600
Hanson
 
Upvote 0
are you sure? o_O
it's big mystery , buddy :rolleyes:
I copy the code again based on post#18 and this is what gives me
11.PNG

do you see the borders?
 
Upvote 0
Disable or delete these 2 rows of code:
VBA Code:
.UsedRange.Cells.Borders.LineStyle = xlContinuous
.UsedRange.Offset(1).Cells.Interior.ColorIndex = 16
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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