delete one of values colums by split data into two sheets

Alaa mg

Active Member
Joined
May 29, 2021
Messages
375
Office Version
  1. 2019
HI

I have about 6700 rows .I want split data sheet by keep one values column for each item for each sheet separately by create the sheets based on headers MONTH1,2 and every time will change and update data in first sheet ,then should update data in divided sheets by clearing data before bring data again with keep the same formatting and borders in divided sheets .
DATA.xlsm
ABCD
1ITEMIDMONTH1MONTH2
21MT ASGL-VEN SD-11210
32C-ZER MN/100 TR121300
43BB 2000MN TT GH900
54TRM 2000MN TT GH16
65BD 234 NH GG8090
76BR 111/33M ER HJ7090
88VB 145** SS TF HJK045
99DEWR 1222** SS TF HJK3430
sheet1


result
DATA.xlsm
ABC
1ITEMIDMONTH1
21MT ASGL-VEN SD-112
32C-ZER MN/100 TR12130
43BB 2000MN TT GH90
54TRM 2000MN TT GH
65BD 234 NH GG80
76BR 111/33M ER HJ70
88VB 145** SS TF HJK0
99DEWR 1222** SS TF HJK34
MONTH1



DATA.xlsm
ABC
1ITEMIDMONTH2
21MT ASGL-VEN SD-110
32C-ZER MN/100 TR120
43BB 2000MN TT GH0
54TRM 2000MN TT GH16
65BD 234 NH GG90
76BR 111/33M ER HJ90
88VB 145** SS TF HJK45
99DEWR 1222** SS TF HJK30
MONTH2
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
With your main sheet named "Sheet1"
try below code:
VBA Code:
Option Explicit
Sub test()
Dim lc&, rng, cell As Range, item, ws As Worksheet
Worksheets("Sheet1").Activate
    lc = Cells(1, Columns.Count).End(xlToLeft).Column
    rng = Range("C1", Cells(1, lc)).Value
    For Each item In rng
        For Each ws In Sheets
            If ws.Name Like item Then ws.Delete
        Next
        Worksheets("Sheet1").Copy after:=Worksheets(Sheets.Count)
        ActiveSheet.Name = item
    Next
    For Each ws In Sheets
        If ws.Name <> "Sheet1" Then
            For Each cell In ws.Range("C1", ws.Cells(1, lc))
                If Not cell.Value Like ws.Name Then cell.Value = "#N/A"
            Next
            ws.Range("C1", ws.Cells(1, lc)).SpecialCells(xlCellTypeConstants, xlErrors).EntireColumn.Delete
        End If
    Next
End Sub
 
Upvote 0
thank .can I keep the values columns in sheet1 without delete it please?
 
Upvote 0
thank .can I keep the values columns in sheet1 without delete it please?
Have you tested it? Did you find out that sheet 1 was changed?
I beleave with my code, Sheet1 does not change. Only delete columns in sheets "MONTH..."
 
Upvote 0
Have you tested it? Did you find out that sheet 1 was changed?
surely I tested . actually it deletes columns C,D values from sheet1
I beleave with my code, Sheet1 does not change. Only delete columns in sheets "MONTH..."
if it's right, I no know what I can say for you , but this is what happens for me . I tested repeatedly and gives the same result :confused:
 
Upvote 0
I am so confuse!
Try to test with this code. Hit F5 to see what is printed in Intermediate Window. Take a screenshot and post it.
VBA Code:
Option Explicit
Sub test()
Dim lc&, rng, cell As Range, item, ws As Worksheet
Worksheets("Sheet1").Activate
    lc = Cells(1, Columns.Count).End(xlToLeft).Column
    rng = Range("C1", Cells(1, lc)).Value
    For Each item In rng
        For Each ws In Sheets
            If ws.Name Like item Then ws.Delete
        Next
        Worksheets("Sheet1").Copy after:=Worksheets(Sheets.Count)
        ActiveSheet.Name = item
    Next
    For Each ws In Sheets
     Debug.print ws.Name
        If ws.Name <> "Sheet1" Then      Debug.print ws.Name & "|" & 2
 '           For Each cell In ws.Range("C1", ws.Cells(1, lc))
  '              If Not cell.Value Like ws.Name Then cell.Value = "#N/A"
   '         Next
    '        ws.Range("C1", ws.Cells(1, lc)).SpecialCells(xlCellTypeConstants, xlErrors).EntireColumn.Delete
     '   End If
    Next
End Sub
 
Upvote 0
So strange! Could you attach file via google drive or dropbox then send link to me (paste here or to my inbox)?
 
Upvote 0
here is the file
test.xlsm
notice: I note your code has problem . if I run the code more than one time will gives error because of the sheets names have already existed. as I said in OP
every time will change and update data in first sheet ,then should update data in divided sheets by clearing data before bring data again with keep the same formatting and borders in divided sheets .
this means if I add new data in sheet1 then automatically updated data in devided sheets(no need every time split sheets because of it split from the first time just update data )
thanks
 
Upvote 0
Its from case sensitive in Sheet1 name.
replace "sheet1" by "Sheet1" then it should works.
 
Upvote 0

Forum statistics

Threads
1,223,645
Messages
6,173,523
Members
452,520
Latest member
Pingaware

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