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
 
this is strange !! . I thought can show error subscript out of range . this beacuse of not matched sheet name :unsure:

Ok this works . just curiosity can I ignore any item contains empty or zero value when split it . I mean deleting the whole items contains zero or empty value for sheets MONTH1,2. I know this doesn't mentioned in OP as I said just curiosity if you can't I will satisfy your answering and close the thread.

thanks again
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
No problem.
try this. (The added part is 4 lines from bottom, right before the last End IF)
VBA Code:
Option Explicit
Sub test()
Dim lc&, lr&, rng, cell As Range, item, ws As Worksheet
Application.ScreenUpdating = False
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
            lr = ws.Cells(Rows.Count, "B").End(xlUp).Row
            ws.Range("D2:D" & lr).Formula = "=1/(1/C2)"
            ws.Range("D2:D" & lr).SpecialCells(xlCellTypeFormulas, xlErrors).EntireRow.Delete
            ws.Columns(4).Delete
        End If
    Next
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
wow ! you're legend !

can I ask you ? you don't use dictionary & array and the code is very fast somtimes somebody using dictionary and array to make fast but I don't think will be fast like this . may you tell me what's the secret if you don't mind , which code line control the speed?
 
Upvote 0
OK.

Alternative way
1) Using array (or dictionary), to copy body range of sheet1 (base on condition of MONTH), then paste into each sheet MONTH
2) For each sheet MONTH, loop for each cell in column C then delete rows those are blank or = 0

My approach:
1) This just dupplicate Sheet1, not copy range base on condition. Then delete columns
2) Using column D as helper, paste a formula (in bulk, not loop) (this line: ws.Range("D2:D" & lr).Formula = "=1/(1/C2)"). Now, In any row of column C = 0 or empty, column D = "Div/0"
Using specialcells to delete all rows with error (in bulk, not loop)
This line:
ws.Range("D2:D" & lr).SpecialCells(xlCellTypeFormulas, xlErrors).EntireRow.Delete

To get better code speed, try to avoid using loop on worksheet range.
 
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