• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Macro for Creating New Sheet as per condition

Hi All,

I have an excel sheet in sheet 1 is my source file, I want a macro that will create 2 sheet base on the given condition as per sheet 1 as source:

Condition 1 ): It will create a sheet 2 and do the sum and pivot for Total Zero sold, destination wise.

Condition 2: It will create a sheet 3 and do the sum and pivot for Total Count of number of code for that zero sold of sheet 2.

**Please note that the format of source sheet is quite different hence normal pivoting will not work and only macro will do.

Thanks in advance for those who will help...
 

Attachments

  • For Sum-Count - Pivot.xlsx
    10.4 KB · Views: 5
**Please note that the format of source sheet is quite different hence normal pivoting will not work and only macro will do.
One answer is to rearrange the source data. In the attached I've written a macro snippet to do that; it's called by the button on that source sheet labelled Transform. It makes a new table on that sheet. When you click that button, it asks you to select the data body of your source data (I've defaulted it at the moment to choose the range I mean from your sample file's data, so that you'll know what to select later when you try it with bigger data).

That's all the macro does.
Since I'm not sure how you wanted the pivot tables to be set up I haven't spent time writing code to do that. I have however placed 2 pivot tables on the other sheets. Curently they show nothing. Don't try to refresh them until you've transformed your data.

The transformed data is eminently suitable for making any pivot tables from.
 

Attachments

  • Chandoo39277For Sum-Count - Pivot.xlsm
    28.4 KB · Views: 4
Hi,

It is possible to put the rearrange item in new sheet rather than next to clicking button? Thank You so much...
 
Last edited by a moderator:
You sent me a sensitive document via private email. I will send one back with this macro in it. There's a button on Sheet1 (a new sheet) which runs this macro (it's hard-coded to your data as you have it).
Code:
Sub blah2()
Set rngSceData = Sheets("Item").Range("A1:DU96277")
ResultsRowCount = Application.CountA(Intersect(rngSceData, rngSceData.Offset(3, 15)))
ReDim Results(1 To ResultsRowCount / 2 + 1, 1 To 19)
Hdrs = Array("Group Name", "Dept Name", "Dept", "Supplier Name", "Vpn", "Phase Desc", "Item Code", "Diff 3", "Color", "Shade", "Item Desc", "Class Name", "Subclass", "Season Name", "First Trf Date", "Destination", "Location", "Sold Qty1", "Soh")
SceData = rngSceData.Value
'row 4,colm 16 is start of data
resultrow = 1
For i = 0 To UBound(Hdrs)
Results(resultrow, i + 1) = Hdrs(i)
Next i
For rw = 4 To UBound(SceData)
  For colm = 16 To UBound(SceData, 2) Step 2
    If Not (IsEmpty(SceData(rw, colm)) And IsEmpty(SceData(rw, colm + 1))) Then
      'Stop
      'rngSceData.Cells(rw, colm).Resize(, 2).Select
      resultrow = resultrow + 1
      For c = 1 To 15
        Results(resultrow, c) = SceData(rw, c)
      Next c
      Results(resultrow, 16) = SceData(1, colm)    'dest
      Results(resultrow, 17) = SceData(2, colm)    'locn
      'Debug.Assert SceData(3, colm) = "Sold Qty1"
      Results(resultrow, 18) = SceData(rw, colm)    'Sold Qty1
      'Debug.Assert SceData(3, colm + 1) = "Soh"
      Results(resultrow, 19) = SceData(rw, colm + 1)    'Soh
    End If
  Next colm
Next rw

With Sheets("Sheet1")
  .Range("A1").Resize(Rows.Count - 2, UBound(Results, 2)) = Results 'this one line takes about 30 secs!
End With
End Sub
There's a problem with your data. There's a fair bit of it. In order to transform the data as before it would need 4,319,950 rows (that's ignoring all blank cells). I can halve this number by having separate columns for Stock and Sold, which I've done, but this still requires 2,159,976 which is more than a single Excel sheet will take (just over 1 million).
However, the full data set is computed in-memory, I only put the first million or so rows on the sheet, so the data is truncated, and it's useless.
Clicking that button will bring up that data (it's empty at the moment to keep the file size reasonable). It takes 40 seconds or so to run here, with most of that time (30 secs) taken up reading from and writing to the sheets.

There's a pivot table on sheet TASK 1 Summary which will need refreshing after the data have been transformed, but remember, it's working on incomplete data.

Power Pivot/Power Query might indeed be able to help you better but I don't know enough about these applications yet to advise you well.

Looking at the data, I strongly suspect that it's already pivoted data from a database; is that database available to you? It seems very likely indeed that querying or pivoting that database directly in Excel would be the lightest and easiest solution.

Attachment sent privately.
 
You sent me a sensitive document via private email. I will send one back with this macro in it. There's a button on Sheet1 (a new sheet) which runs this macro (it's hard-coded to your data as you have it).
Code:
Sub blah2()
Set rngSceData = Sheets("Item").Range("A1:DU96277")
ResultsRowCount = Application.CountA(Intersect(rngSceData, rngSceData.Offset(3, 15)))
ReDim Results(1 To ResultsRowCount / 2 + 1, 1 To 19)
Hdrs = Array("Group Name", "Dept Name", "Dept", "Supplier Name", "Vpn", "Phase Desc", "Item Code", "Diff 3", "Color", "Shade", "Item Desc", "Class Name", "Subclass", "Season Name", "First Trf Date", "Destination", "Location", "Sold Qty1", "Soh")
SceData = rngSceData.Value
'row 4,colm 16 is start of data
resultrow = 1
For i = 0 To UBound(Hdrs)
Results(resultrow, i + 1) = Hdrs(i)
Next i
For rw = 4 To UBound(SceData)
  For colm = 16 To UBound(SceData, 2) Step 2
    If Not (IsEmpty(SceData(rw, colm)) And IsEmpty(SceData(rw, colm + 1))) Then
      'Stop
      'rngSceData.Cells(rw, colm).Resize(, 2).Select
      resultrow = resultrow + 1
      For c = 1 To 15
        Results(resultrow, c) = SceData(rw, c)
      Next c
      Results(resultrow, 16) = SceData(1, colm)    'dest
      Results(resultrow, 17) = SceData(2, colm)    'locn
      'Debug.Assert SceData(3, colm) = "Sold Qty1"
      Results(resultrow, 18) = SceData(rw, colm)    'Sold Qty1
      'Debug.Assert SceData(3, colm + 1) = "Soh"
      Results(resultrow, 19) = SceData(rw, colm + 1)    'Soh
    End If
  Next colm
Next rw

With Sheets("Sheet1")
  .Range("A1").Resize(Rows.Count - 2, UBound(Results, 2)) = Results 'this one line takes about 30 secs!
End With
End Sub
There's a problem with your data. There's a fair bit of it. In order to transform the data as before it would need 4,319,950 rows (that's ignoring all blank cells). I can halve this number by having separate columns for Stock and Sold, which I've done, but this still requires 2,159,976 which is more than a single Excel sheet will take (just over 1 million).
However, the full data set is computed in-memory, I only put the first million or so rows on the sheet, so the data is truncated, and it's useless.
Clicking that button will bring up that data (it's empty at the moment to keep the file size reasonable). It takes 40 seconds or so to run here, with most of that time (30 secs) taken up reading from and writing to the sheets.

There's a pivot table on sheet TASK 1 Summary which will need refreshing after the data have been transformed, but remember, it's working on incomplete data.

Power Pivot/Power Query might indeed be able to help you better but I don't know enough about these applications yet to advise you well.

Looking at the data, I strongly suspect that it's already pivoted data from a database; is that database available to you? It seems very likely indeed that querying or pivoting that database directly in Excel would be the lightest and easiest solution.

Attachment sent privately.


HI p45cal,

It's more than enough, thank you so much for your great help, I done this in Power Query of excel but there is a limitation also while doing tranSpose as it is a huge data, but no problem, i will just limit some columns in the back end query to reduce data number rows and your macro will work, which is a big help. Once again VERY BIG THANKS your such a wonderful VBA expert, God Bless You always. THANK YOU SO MUCH.
 
Realise that limiting the columns A:O on the items sheet will not affect the number of rows required, you will have to limit (in pairs) the columns from P:DU.

So you do have the database to hand!


Yes I do have.

Its very fine now, but again need help, I sent a msg to your mail privately.

Thank You so much for your macro, such a big help. But one more help, I need a Macro based on your output macro that when a macro button click, it will pop up a msg box and will show the total of all S that Soh is less than or equal to 0 like the given below.

If msg box pop up is not possible either it will create a new excel sheet and output the total of all S that Soh is less than or equal to 0.

S Total =< 0 SOH
ABC 123
DEF 456


Thank You so much.
 
Back
Top